Search code examples
javanetwork-programmingtcpdataoutputstream

Client doesn't receive output from server's DataOutputStream


I'm currently attempting to code my first client>server system to transmit packets containing strings back and forth through a network. However I'm having a problem in that the following is happening: The client sends message to the server, the server receives the message, processes the message, and then supposedly sends a reply to the client, but the client never receives this message and hangs waiting for a response from the server. Here is the code:

SERVER:

public static void handlePackets() throws Exception {
    String clientSentence;
    String returnToClient;
    ServerSocket welcomeSocket = new ServerSocket(1337);

         System.out.println("Packet receiver initialized!");
         while (run) {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received Packet: " + clientSentence);


            System.out.println("Compiling return to client.");
            returnToClient = "";
            if (clientSentence.startsWith("Handshake-")) {
                returnToClient = handleHandShake(clientSentence);
            }

            outToClient.writeBytes(returnToClient);
            System.out.println("Sent client response " + returnToClient);
         }
         welcomeSocket.close();
  }

CLIENT:

public static String sendTCP(String host, String content) {
    try {
        System.out.println("Packet sender sending TCP packet " + content);
        String serverResponse;
        Socket clientSocket = new Socket(host, 1337);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        outToServer.writeBytes(content + '\n');

        System.out.println("Send data to sever. Awaiting response.");

        serverResponse = inFromServer.readLine();
        clientSocket.close();

        System.out.println("Server response received " + serverResponse + " result was returned to caller.");

        return serverResponse;
    } catch (Exception e) {
        e.printStackTrace();
    }


    return "";
}

Solution

  • The client is calling readLine() but the server isn't writing a line, just bytes, so the client is waiting forever for the line terminator. Append '\n' to the server's reply. Also the server should close the accepted socket once it's finished with it. Do this by closing whatever writer or output stream you have wrapped around it, not by closing the socket itself.

    You should use BufferedOutputStream instead of DataOutputStream. It will work for simple data as is but you are liable to charset problems if you don't fix it sooner or later. In general you should always use symmetric input and output streams or readers.