Willy Wonka
Willy Wonka

Reputation: 459

TCP packet comes in to the server, but not to the listening program

So I have an Android app and a socket server. The communication seems fine when sending the first packet from the client. I get the response fine.

However, from the second time on, when I send a packet, nothing happens. No Logcat nor anything in the server. I tried printing everything that comes into the server.

So I tried to catch the packets with Wireshark. I saw that on the second packet, this happens.

wireshark image

The packet seems to be coming in, but I don't get anything in the server program running.

I tried running another app after one gets stuck like above, and the second app gets the response fine the first time, then the same thing.

So I guess the server program isn't stuck anywhere or something.

Here's the listening part of my code.

serverSocket = new ServerSocket(port);
while (true) { // socket
    Socket clientSocket = serverSocket.accept();

    out = new DataOutputStream(clientSocket.getOutputStream());
    in = new DataInputStream(clientSocket.getInputStream());

    byte[] read = new byte[512];
    int readcount = in.read(read);
    System.out.println(bytesToHex(read)); // doesn't print from the second one

    if (readcount != -1) {
        if (read[0] == something) {
            HandleMsg doIt = new HandleMsg(connection, out, Arrays.copyOfRange(read, 1, readcount));
            doIt.start();
        }
        else {
            System.out.println("Got Something Unknown!");
            System.out.println(bytesToHex(Arrays.copyOfRange(read, 0, readcount)));
        }
    }
}

connection is a DB connection.

Also, when I restart the app or the connection, it works fine the first time, and get stuck again.

Thank you for your help.

Upvotes: 0

Views: 118

Answers (1)

user207421
user207421

Reputation: 311050

You're only doing one read from each accepted socket, so of course the second packet from the same source doesn't get printed. You have to loop.

Upvotes: 1

Related Questions