Java网络服务器和TIME_WAIT [英] Java network server and TIME_WAIT

查看:45
本文介绍了Java网络服务器和TIME_WAIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络服务器出现问题,该服务器从我公司生产的设备接收信号.该设备有时会重用它刚使用过的源端口.这将导致服务器丢弃SYN.然后,设备重试,直到旧的套接字脱离服务器上的TIME_WAIT.然后服务器进行SYN-ACK.

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 used. This causes the SYN to be dropped by the server. The device then retries until the old socket falls out of TIME_WAIT on the server. The server then SYN-ACKs.

服务器是用Java编写的.不幸的是,修改该设备以正确地循环端口是不可行的,因为该领域中有很多设备,而更新现有设备则不是一个选择.旧软件是用C ++编写的,并且以某种方式从Windows TCP堆栈的列表中删除了TIME_WAIT端口.

The server is written in Java. Unfortunately, modifying the device to cycle ports correctly is not an option, as there are many in the field, and updating the existing units is not an option. The old software was written in C++ and somehow expunged the TIME_WAIT port from the list in the Windows TCP stack.

有人可以向我提供任何有关如何在Windows上通过Java避开TIME_WAIT的建议吗?

Can anyone offer me any advice on how to circumvent TIME_WAIT from Java on Windows?

我确实在Wireshark中确认该设备正在重新使用最近使用的端口.

I have indeed confirmed in Wireshark that the device is reusing a recently used port.

在服务器套接字上,我使用以下选项:

On the server socket I am using the following options:

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 the client socket has the following set after receiving:

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

我尝试将SO_LINGER设置为true和false,并具有完全相同的跟踪结果.CLIENT_READ_TIMEOUT设置为10秒.

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

推荐答案

以尼古拉的答案为基础

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

在Java中将是等效的.

would be the equivalent in Java.

您可能想看的另一件事是setReuseAddress(true);

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

这篇关于Java网络服务器和TIME_WAIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆