Tcp connection at TIME_WAIT will not allow reconnecting, java

After connecting tcp to the server, I close the linux application and Socket.close () is called.

Checking netstat -pant, I see that the connection is in TIME_WAIT state.

This does not allow me to connect to the server immediately, since I use the same port to connect. Instead, I have to wait for a connection with a TIME_WAIT status timeout before I can reconnect again.

I played around - without knowing - using socket methods: set_so_timeout (), set_keepalive (), set_so_linger () and set_reuseaddr () - the exact spelling of the method may be incorrect in this message.

My question is: HOW do I get a connection from TIME_WAIT so that I can immediately make a connection again?

Please let me know.

Thanks JBU

0
source share
1 answer

The best way to get a connection from TIME_WAIT (surprisingly) is to wait :-)

How TCP / IP Works A session is identified by a tuple (sourceIP, sourcePort, destIP, destPort, protocol) , and the reason you cannot reuse it is because there might be packets still on the network.

The TIME_WAIT state is typically two times the maximum packet life, and you should not bother with it, as this may cause packets to be displayed from the previous session (which will ruin your current session).

Ideally, you need to connect from a different source port, after which you can immediately open a session.

Another thing you must observe is badly closed sessions. I always subscribed to the recommendation that the client should close the session (and completely close it). This minimizes the opportunity for long-lived half-closed sessions.

+5
source

Source: https://habr.com/ru/post/1400887/


All Articles