Java network server and TIME_WAIT

后端 未结 3 1494
你的背包
你的背包 2021-01-15 20:17

I have run into a problem with a network server that receives signals from devices my company produces. The device will occasionally reuse the source port that it had just

相关标签:
3条回答
  • 2021-01-15 21:00

    Building on Nikolai's answer,

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

    would be the equivalent in Java.

    EDIT: Another thing you might like to look at would be setReuseAddress(true);

    0 讨论(0)
  • 2021-01-15 21:17

    The old not-recommended trick to avoid TIME_WAIT is to set SO_LINGER socket option to { 1, 0 } - the close then sends RST instead of doing normal flush/four-way exchange sequence, thus avoiding the TIME_WAIT all together (be warned - you might lose tail of what you still have in the send buffer.) I can't comment on whether this could be done in Java though.

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

    0 讨论(0)
  • 2021-01-15 21:20

    The server is ignoring the SYN packets from the client because it cannot distinguish between those for a new session using the old source port and retransmissions from the old session. If you circumvent the TIME_WAIT state on the server, by setting the system timer interval for aging out TIME_WAIT state entries in the control block table, then how will your server properly ignore SYN retransmissions for sessions that have already terminated?

    0 讨论(0)
提交回复
热议问题