Java Client-Server application pipe not working on child thread
<p>I have a client and a server. The client binds a socket on a specific port, the server sends back a new port to the client and the client should bind a new socket on the new port number.</p>

<p>From the main server thread, I start a thread that sends a message to the client once the server is ready and is listening to the new port, so that the client can attempt to connect to the new port. The pipe from the child thread is not sending the message to the client.
So both client and server just freeze, it seems like a deadlock, but im not sure. This line of code in the client: System.out.println("FROM SERVER: " + inMsg_rport); is not executing.</p>

<p>Server Code:</p>

<pre><code>class server
            {
                public static void main(String argv[]) throws Exception
                {
                    String newPort;
                    ServerSocket serverSocket = null;
                    Socket clientSocket = null;
                    try
                    {
                        serverSocket = new ServerSocket(5555);
                        clientSocket = serverSocket.accept();

                        DataOutputStream serverOut =
                        new DataOutputStream(clientSocket.getOutputStream());
                        int r_port = 5556;
                        Thread appThread = new Thread(new serverApp(serverOut, r_port));
                        appThread.start();
                    }
                    catch(IOException e)
                    {
                        System.out.println(e);
                    }
                }

                static class serverApp implements Runnable
                {
                    DataOutputStream serverOut;
                    int nPort;
                    public serverApp(DataOutputStream servO, int r_port)
                    {
                        this.serverOut = servO;
                        this.nPort = r_port;
                    }
                    @Override
                    public void run()
                    {
                        ServerSocket serverSocket = null;
                        Socket clientSocket = null;

                        try
                        {
                            serverSocket = new ServerSocket(nPort);
                            serverOut.writeBytes(sr_port);
                            clientSocket = serverSocket.accept();
                        }
                        catch(IOException e)
                        {
                            System.out.println(e);
                        }
                    }
                }
            }
</code></pre>

<p>Client code:</p>

<pre><code>            class client {
                public static void main(String argv[]) throws Exception
                {
                    String serverIp = argv[0]; 
                    String msg = argv[2];
                    int port = Integer.parseInt(argv[1]);

                    Socket clientSocket = new Socket(InetAddress.getByName(serverIp), port);

                    BufferedReader clientIn =
                    new BufferedReader(new
                    InputStreamReader(clientSocket.getInputStream()));

                    String inMsg_rport = clientIn.readLine();
                    System.out.println("FROM SERVER: " + inMsg_rport);
                    int r_port = Integer.parseInt(inMsg_rport);
                    clientSocket.close();
                    System.out.println("Closed connection");

                   Socket new_clientSocket = new Socket(InetAddress.getByName(serverIp), r_port);

                }
            }
</code></pre>
