Why won't my client nor server send messages to each other?

58 Views Asked by At

For my networking course, I am tasked with turning a OneWayMesg (Client sends messages to Server) program to a TwoWayMesg (Client sends messages to Server and Server sends messages to Client).

In theory, this should be no more difficult than merely tweaking the C2S code, including the ClientReader to ServerReader and the Client print line to a Server print line. I did so, and while my code throws no errors, in the console I am unable to make the client nor server talk to each other.

Where did I mess up in my code?

// Client.java
            BufferedReader fromUserReader = new BufferedReader(
                    new InputStreamReader(System.in));


                    // Prepare to read from server
            BufferedReader fromServerReader = new BufferedReader(
                new InputStreamReader(sock.getInputStream()));

            // Keep doing till we get EOF from user
            while (true) {
                // Read a line from the keyboard
                String line = fromUserReader.readLine();

                // If we get null, it means user is done
                if (line == null) {
                    System.out.println("Closing connection");
                    break;
                }

                // Send the line to the server
                toServerWriter.println(line);

                // Read a message from the client
                String message = fromServerReader.readLine();

                // Display the message
                System.out.println("Server: " + message);
            }
//Server.java
try {
            // Create a server socket with the given port
            ServerSocket serverSock = new ServerSocket(serverPort);
            System.out.println("Waiting for a client ...");

            // Wait to receive a connection request
            Socket clientSock = serverSock.accept();
            System.out.println("Connected to a client at ('" +
                                                ((InetSocketAddress) clientSock.getRemoteSocketAddress()).getAddress().getHostAddress()
                                                + "', '" +
                                                ((InetSocketAddress) clientSock.getRemoteSocketAddress()).getPort()
                                                + "')"
                                                );

            // No other clients, close the server socket
            serverSock.close();

            // Prepare to read from client
            BufferedReader fromClientReader = new BufferedReader(
                    new InputStreamReader(clientSock.getInputStream()));

            // Keep serving the client
            while (true) {
                // Read a message from the client
                String message = fromClientReader.readLine();

                // Read a line from the keyboard
                String line = fromClientReader.readLine();

                // If we get null, it means client sent EOF
                if (message == null) {
                    System.out.println("Client closed connection");
                    clientSock.close();
                    break;
                }

                // Display the message
                System.out.println("Client: " + message);
            }

The Client console The Server console

Thanks

2

There are 2 best solutions below

2
Arfur Narf On

Commenting "read from keyboard" when using a client reader does not make it read from a keyboard. Writing client code to expect data from the server does not cause the server to send data.

What you apparently want is a client that sends a line then waits to receive a line; and a matching server that waits to receive a line, then sends a line. But your code does not do that.

I diagnose a case of not reading what you've actually written. That is 90% of debugging code: seeing what is there, not what you want to be there.

Also, printing out "closing connection" does not close the connection. Maybe the client code exits immediately after, which will do the job for you; but we don't have that code.

0
MadProgrammer On

So, in your server code you are doing...

String message = fromClientReader.readLine();

// Read a line from the keyboard
String line = fromClientReader.readLine();

This is trying to read from the client twice, the second call is going to block, preventing any additional code from been executed.

In the client, you're also doing...

String message = fromServerReader.readLine();

towards the end, which would then also block, as the server never writes anything.

You will also need to add...

toServerWriter.flush();

after toServerWriter.println(line);

You should also read through All About Sockets and The try-with-resources Statement