Java TCP Client listening and writing to TCP Server
<p>I have one TCP-Client and one TCP-Server written in Java. The Server is waiting for instructions from the Client but should also be able to send instructions to the client. 
I can make the Client send something to the Server and wait for a reply. But I can not make it like waiting for a message without sending something before.</p>

<p>TCP-Client</p>

<pre><code>    public class TCPClient {

    static DataOutputStream toServer;
    static BufferedReader fromServer;
    static Socket socket;

    public static void main(String[] args) throws Exception {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("*************************");
        System.out.println("*       Client          *");
        System.out.println("*************************");
        System.out.println("INSTRUCTION       | EFFECT");
        System.out.println("aktiv             | ready to do something");
        System.out.println("exit              | disconnect");
        System.out.println();
        System.out.print("Please enter the IP-Address of the Server: ");
        String ip = input.readLine();
        System.out.println();

        try {
            socket = new Socket(ip, 9999);
        } catch (Exception e) {
            System.out.println("Can not connect to Server!");
        }

        toServer = new DataOutputStream(socket.getOutputStream());  // Datastream FROM Server 
        fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));     
        while (sendRequest()) {              
            receiveResponse();                 
        }
        socket.close();
        toServer.close();
        fromServer.close();
    }

    private static boolean sendRequest() throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String line;
        boolean holdTheLine = true;          // Connection exists

        System.out.print("&gt; ");
        line = input.readLine();

        switch (line) {
            case "aktiv":
                toServer.writeBytes("active" + '\n');
                break;
            case "exit":
                holdTheLine = false;
                break;
            default:
                break;
        }

        return holdTheLine;
    }

    private static void receiveResponse() throws IOException {
        System.out.println("Server: " + fromServer.readLine() + '\n');
    }
}
</code></pre>

<p>TCP-Server</p>

<pre><code>public class TCPServer {
    static boolean connected = true;
    public static void main(String[] args) throws Exception {
        System.out.println("********************************");
        System.out.println("*         Server               *");
        System.out.println("********************************");
        System.out.println("INSTRUCTION | EFFECT");
        System.out.println("ok          | send an ok to client");

        ServerSocket listenSocket = new ServerSocket(9999);

        while (true) {
            final Socket client = listenSocket.accept();

            Thread newClientThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    multithreadedServer(client);
                }
            });
            newClientThread.start();
        }
    }

    public static void multithreadedServer(Socket client) {
        String line;
        final BufferedReader fromClient;
        final DataOutputStream toClient;
        Thread cmdForClient;

        try {
            fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
            toClient = new DataOutputStream(client.getOutputStream());

            while (connected) {
                cmdForClient = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String line = fromClient.readLine();
                            System.out.println("Client: " + line);
                            if (line.equals("exit")) {
                                connected = false;
                            }
                        } catch (Exception e) {
                            System.out.println(e);
                        }
                    }
                });
                cmdForClient.start();

                final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

                try {
                    String reply = input.readLine();
                    if (reply.equals("ok")) {
                            toClient.writeBytes("OK." + '\n');
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
            fromClient.close();
            toClient.close();
            client.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}
</code></pre>