Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I sending an RST if my socket is connected and not closed?

I have an Android device that communicates wirelessly with a PC, using a java.net.Socket. Everything hums along fine, but if I do nothing (i.e., no network use) for exactly 1 minute then when the Android sends a packet of data to the PC the PC receives it and sends an ACK, but the Android responds with an RST.

From Wireshark ( 10.1.2.1 is the Android, 10.1.2.11 is the PC)...

356 0.112470 10.1.2.1 10.1.2.11 TCP 97 34360→181 [PSH, ACK] Seq=1 Ack=1 Win=4935 Len=31 TSval=156103571 TSecr=320673352

359 0.000011 10.1.2.11 10.1.2.1 TCP 66 181→34360 [ACK] Seq=1 Ack=32 Win=260 Len=0 TSval=320738236 TSecr=156103571

360 0.000304 10.1.2.1 10.1.2.11 TCP 60 34360→181 [RST] Seq=32 Win=0 Len=0

At this point if I interrogate the socket's member variables it says . . .

  • isConnected = true
  • isCreated = true
  • isInputShutdown = false
  • isOutputShutdown = false
  • isClosed = false
  • isBound = true

... which looks like I should still be receiving packets just fine. So how do I figure out why I'm sending RST?

N.B. - there are no settings to "sleep" or turn off the wifi or display or any other battery-saving features set enabled on this device.

like image 355
user316117 Avatar asked Jun 21 '17 20:06

user316117


People also ask

Why does TCP send RST?

In TCP, packets with the "Reset" (RST or R) flag are sent to abort a connection. Probably the most common reason you are seeing this is that an SYN packet is sent to a closed port. But RST packets may be sent in other cases to indicate that a connection should be closed.

What causes a RST ACK?

This is very simply that the port you are trying to connect to is not being listened to on the remote host. Either your service is not running on the host, or possibly it has been firewalled.

Do firewalls send RST packets?

RST flags are sent by the server's network stack, firewalls tend to drop packets silently if the port is filtered.

When TCP connection is reset?

TCP reset is an abrupt closure of the session; it causes the resources allocated to the connection to be immediately released and all other information about the connection is erased. TCP reset is identified by the RESET flag in the TCP header set to 1.


2 Answers

The 1 minute delay looks like a timeout. It may be the SO_TIMEOUT but this does not generate network activity on itself. Also the fact that the last packet sent contains 31 bytes of data seems to indicate that the application is involved. A possible scenario would be :

  • The android application times out (on its own or triggered by a socket's SO_TIMEOUT)
  • It sends a last chunk of data, e.g. by flushing an output stream.
  • It closes abruptly the socket, for example by using the setSoLinger(true, 0) socket option.
like image 106
bwt Avatar answered Sep 28 '22 09:09

bwt


None of those Socket methods returns the state of the connection. They are all about the internal state of the java.net.Socket object, as determined by which constructors and methods you have called on it. They don't magically start returning false if the peer drops the connection.

You will find when you go to use the socket for I/O that you will get an IOException: 'connection reset'.

Why the connection has been reset is another matter. The usual reason is that you had sent to a connection that had already been closed by the peer, or that the peer closed the connection without reading all the data that had already arrived. In other words, an application protocol error. There are other reasons, but these are the most common.

like image 24
user207421 Avatar answered Sep 28 '22 07:09

user207421