Java Network Server and TIME_WAIT

I have a problem with a network server that receives signals from devices that my company produces. The device will sometimes use the source port that it just used. This results in a SYN reset by the server. The device then retries until the old server drops out of TIME_WAIT on the server. Then the SYN-ACK server.

The server is written in Java. Unfortunately, changing the device for the correct port loop is not an option, since there are many, and updating existing blocks is not an option. The old software was written in C ++ and somehow removed the TIME_WAIT port from the list on the Windows TCP stack.

Can anyone offer me any tips on how to get around TIME_WAIT from Java on Windows?

EDIT: I really confirmed in Wireshark that the device is reusing the recently used port.

In the server socket, I use the following parameters:

socket = new ServerSocket(); socket.setPerformancePreferences(3, 2, 1); socket.setReuseAddress(true); socket.setSoTimeout(CLIENT_READ_TIMEOUT); socket.bind(new InetSocketAddress(group.getPort()), MAX_TCP_BACKLOG); 

And after that, the client socket has the following set:

 Socket client = server.accept(); client.setKeepAlive(false); client.setSoLinger(true, 0); client.setReuseAddress(true); client.setTcpNoDelay(true); client.setSoTimeout(CLIENT_READ_TIMEOUT); 

I tried SO_LINGER both true and false, with the same trace results. CLIENT_READ_TIMEOUT set to 10 seconds.

+1
source share
3 answers

Based on Nikolai’s answer,

 Socket s; ... s.setSoLinger(true,0); 

will be the equivalent in Java.

EDIT: Another thing you might want to see is setReuseAddress (true);

+4
source

The old non-recommended trick to avoid TIME_WAIT is to set the SO_LINGER socket option to { 1, 0 } - close , and then send RST instead of the usual flash / four to avoid TIME_WAIT all together (be warned - you may lose the tail of what you have there is still in the send buffer.) I can not comment on whether this can be done in Java.

EDIT: Can you confirm with tcpdump that clients are really reusing source port numbers? If not, this might just be the classic case for SO_REUSEADDR listening on a socket, as John pointed out.

+3
source

The server ignores SYN packets from the client because it cannot distinguish between those used for the new session, using the old source port and retransmissions from the old session. If you bypass the TIME_WAIT state on the server by setting the system timer interval for aging TIME_WAIT state records in the control unit table, how will your server properly ignore SYN retransmissions for already completed sessions?

0
source

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


All Articles