Search code examples
javasocketsclient-serverdatainputstreamdataoutputstream

Server Socket reads Client message indefinitely in Java


In my simple Java Client-Server program, when sending a message to the server and reading it there, readInt() reads indefinitely, making the program stick there.

I made sure I was only sending and receiving an int, nothing else, as you can tell by the code (I also tried with and without appending \n to the message sent to see if it would end):

Relevant Client Code

Socket server = new Socket("127.0.0.1", 2424);
DataOutputStream outputStream = new DataOutputStream(server.getOutputStream());
DataInputStream inputStream = new DataInputStream(server.getInputStream());
outputStream.writeInt(Protocol.Message.HANDSHAKE);
outputStream.write('\n'); // I tried with and without this
outputStream.flush();

Relevant Server Code

ServerSocket socket = new ServerSocket(2424);
System.out.println("Listening on port 2424");

while (connected) {
    Socket client = socket.accept();
    System.out.println("SERVER: Going to read a message"); // This shows
    int messageType = (new DataInputStream(client.getInputStream())).readInt();
    System.out.println("SERVER: Received a message (" + messageType + ")"); // This does not
    commands.execute(messageType);
}

The message that should be printed after readInt() is never seen. I thought it would since I was only sending an int and receiving an int (4 bytes), it's not like I was sending more data than expected.

How should I go about making the readInt() end? Do I have to send a null byte or something else?

EDIT: Actual Server code (using Threads).

ServerSocket socket = new ServerSocket(2424);
System.out.println("Listening on port 2424");

while (connected) {
    Socket client = socket.accept();
    Worker worker = new Worker(client);
    worker.start();
}

Worker Thread

public class Worker extends Thread {
    private final Socket client;
    private final Commands commands;

    private final DataOutputStream outputStream;
    private final DataInputStream inputStream;

    public Worker(Socket client) throws IOException {
        System.out.println("SERVER: Handling client message");
        this.client = client;

        outputStream = new DataOutputStream(client.getOutputStream());
        inputStream = new DataInputStream(client.getInputStream());

        commands = new Commands();
        commands.addCommand(Protocol.Message.HANDSHAKE, new HandshakeCommand());
        //commands.addCommand(Protocol.Message.RECEIVE_FILE, new ReceiveFileCommand());
    }

    @Override
    public void run() {
        System.out.println("SERVER: Running thread for client message");
        try {
            int messageType = inputStream.readInt();

            System.out.println("SERVER: Received a message (ID " + messageType + ")");
            commands.execute(messageType);
        } catch (IOException | UnknownCommandException ex) {
            System.out.println(ex);
        }
    }
}

Solution

  • The reason it was never reading is because nothing was being sent, as xander said. And it was my fault I didn't include the actual client code, just the server code and a minimized version of the client code.

    I was trying to send the message after the while() loop in the client (it also waits for messages from the server).

    The solution was to delegate the the client's listening part into another thread so it didn't block the main thread that needed to send the message to the server.