KJaeg
KJaeg

Reputation: 686

No response from second client socket

I am working with Java sockets. I have a server socket and two client sockets. My problem is, that the first client socket submits its message to my server socket, and the message from the second client socket is not arriving at the server socket. That means, that for the first client socket the while loop is interrupted after a succesful message, and the second client ends in an infinite while loop. If I test each client socket seperately in a test class, each client socket is submitting its message correctly to my server socket. By watching TCPView I noticed, that the client socket does not respond, as long as my port is used.

I read, that the second client socket should still respond its message, even if the port was used. In my case, the second client socket should always respond about a second after the first one. But I can't get them to work one after another.

So, here is my code for the method, which is waiting for client messages:

public void listenToSocket()
{
    serverSocket = null;
    thread = null;
    SocketAddress adress = new InetSocketAddress(CommunicationValues.SOCKET_PORT);
    try {
        serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(adress);

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        System.exit(0);
    }




    while(true){
        try
        {
            Socket clientSocket = serverSocket.accept();
            thread = new SocketMessageThread(clientSocket);
            thread.start();         

        } catch (IOException e) {
            System.out.println("MyServerSocket caught an error: \n" + e.getMessage());
            e.printStackTrace();
        }
    }

}

This method is called in a thread. The structure looks like this:

EDIT Here is the code of my SocketMessageThread:

public class SocketMessageThread extends Thread{

private Socket clientSocket;
private static int nameCounter = 0;


public SocketMessageThread(Socket clientSocket) {
    this.clientSocket = clientSocket;
    this.setDaemon(true);
    this.setName("SocketMessageThread" + (nameCounter++));
}

public void run() {
    try (
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));)
    {
        while (in.ready())
        {
            String inLine = in.readLine();
            CommunicationValues.MESSAGE_MEMORIZER = inLine;
        }
        clientSocket.close();

    } catch (IOException e) {
        e.printStackTrace();
    }   
}}

EDIT 2 Both clients only communicate with one particular message. E.g. when a client is started up, it mentioned, that the startup was successful with a single message. There are no repeating messages coming from the client sockets until the server sockets catches them. So if the server socket doesn't catch that one message, it's gone, and it won't be sent again by the client socket.

Upvotes: 0

Views: 967

Answers (1)

user207421
user207421

Reputation: 310883

while (in.ready())
{
    // ...
}

Classic misuse of ready(). Exchange all this for:

String inLine;
while ((inLine = in.readLine()) != null)
{
    CommunicationValues.MESSAGE_MEMORIZER = inLine;
}

Presumably there is more code that you haven't shown us: otherwise all this will do is memorize the last line sent.

Upvotes: 1

Related Questions