ᴘᴀɴᴀʏɪᴏᴛɪs
ᴘᴀɴᴀʏɪᴏᴛɪs

Reputation: 7519

What could cause a Java TCP socket to close?

I'm trying to use an implementation of an MQTT client specifically Paho Java client in Android, and trying to trace where I get an EOFException error.

Specifically this happens on in.readByte();.

public final byte readByte() throws IOException {
    int temp = in.read();
    if (temp < 0) {
        throw new EOFException();
    }
    return (byte) temp;
}

Having looked at this exception, it seems to be triggered when I'm attempting to read from a socket that has been closed. This seems to be a network error, that I fail to locate.

My question is what could cause a socket to close? I haven't yet ruled out the chance of it being closed programmatically but in case of an external factor what are the usual causes of a TCP socket closing? Perhaps any more specific reason relating to an Android device?

Upvotes: 2

Views: 1666

Answers (1)

Warren Dew
Warren Dew

Reputation: 8928

Typically the things that cause a socket to close are:

  • the client closes the socket
  • the server closes the socket, possibly due to a timeout
  • the server shuts down and issues a reset, either before shutdown or after restart, which closes the socket
  • a firewall times the connection out and effectively closes the socket by issuing a subsequent reset

Without more details on your specific symptoms, it's hard to say which of these is most likely.

Upvotes: 2

Related Questions