Mike G
Mike G

Reputation: 4959

Java Client-Server application pipe not working on child thread

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.

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.

Server 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);
                        }
                    }
                }
            }

Client 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);

                }
            }

Upvotes: 1

Views: 530

Answers (1)

Brian Roach
Brian Roach

Reputation: 76908

readLine() in your client is a blocking call, waiting for an end-of-line character

http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()

You aren't sending an end of line character. You're using a DataOutputStream in your server and sending raw bytes.

Don't use a DataOutputStream in your server; I don't think that's really what you're looking for. Just send the port number as text with an end of line character and be done with it.

Upvotes: 1

Related Questions