简体   繁体   English

Java Linux非阻塞套接字超时行为

[英]Java Linux Nonblocking Socket Timeout Behavior

I have a Java nonblocking server that keeps track of all the socket channels in a selector. 我有一个Java非阻塞服务器,可以跟踪选择器中的所有套接字通道。 I then establish 500 connections to the server and send data regularly. 然后我建立了500个与服务器的连接并定期发送数据。 Every piece of data the server receives is echoed back to the client. 服务器接收的每条数据都会回显给客户端。

The problem comes where the test works wonderfully for a couple of hours and then all of the sudden gradually all of the sockets the server is managing throw a Connection timed out IOException when attempting to read data. 问题来自于测试工作几个小时,然后所有突然间服务器正在管理的所有套接字在尝试读取数据时抛出一个连接超时IOException。

I've looked into whether or not the client thread was being starved (and not sending data), but I am yielding to the client thread that iterates through all the sockets and writes out data. 我已经研究过客户端线程是否被饥饿(而不是发送数据),但我正在向客户端线程进行迭代,该线程遍历所有套接字并写出数据。 Traffic seems to be constantly flowing properly, but after a while it just all dies out. 交通似乎在不断流动,但过了一段时间它才会消失。 Any ideas what could be causing this behavior? 什么可能导致这种行为?

I'm running on a Linux platform with the latest iteration of Java 6. My application launches two threads, one for the server, and one for all the clients. 我正在使用最新版本的Java 6在Linux平台上运行。我的应用程序启动两个线程,一个用于服务器,一个用于所有客户端。 Thanks in advance! 提前致谢!

Extra: The issue is dealing with Linux and not my code. 额外:问题是处理Linux而不是我的代码。 When I run the exact same setup on a Windows box (on the same hardware) it never times out, but after several hours they start to occur on Linux. 当我在Windows机器上运行完全相同的设置(在相同的硬件上)它永远不会超时,但几个小时后它们开始在Linux上发生。 It must be some kind of TCP setting in Linux that's causing it to happen. 它必须是Linux中某种导致它发生的TCP设置。 Thanks for the suggestion. 谢谢你的建议。

The issue is dealing with Linux and not my code. 问题是处理Linux而不是我的代码。 When I run the exact same setup on a Windows box (on the same hardware) it never times out, but after several hours they start to occur on Linux. 当我在Windows机器上运行完全相同的设置(在相同的硬件上)它永远不会超时,但几个小时后它们开始在Linux上发生。 It must be some kind of TCP setting in Linux that's causing it to happen. 它必须是Linux中某种导致它发生的TCP设置。 Thanks for the suggestion. 谢谢你的建议。

The -doCloseWithReadPending option in Java and JRE versions 1.5 or 5.0 allows one thread to close a socket when there is a read pending on the same socket from another thread. Java和JRE版本1.5或5.0中的-doCloseWithReadPending选项允许一个线程在另一个线程的同一套接字上有读取挂起时关闭套接字。

When close() is called on a socket which has an outstanding read call from another thread, the close() by default blocks the socket until the read call completes. 当在具有来自另一个线程的未完成读取调用的套接字上调用close()时,close()默认会阻塞套接字,直到读取调用完成。

With the -doCloseWithReadPending option, the socket close() call closes the socket and in the context of the thread with the pending read, a SocketException with the message "Socket closed" is thrown. 使用-doCloseWithReadPending选项,socket close()调用将关闭套接字,并在具有挂起读取的线程的上下文中,抛出带有“Socket closed”消息的SocketException。

I don't know if this is the root cause of your issue without seeing the code, but I thought I would add this here incase it affects your issue. 如果没有看到代码,我不知道这是否是您问题的根本原因,但我想我会在此处添加此内容,因为它会影响您的问题。

So in both the case that works (Windows with recent JVM) and the case that doesn't (Linux with recent JVM), both the server and client are on the same machine in the same JVM? 因此,在有效的情况下(带有最新JVM的Windows)和不带有最新JVM的Linux(带有最新JVM的Linux),服务器和客户端都在同一个JVM中的同一台机器上?

Can you clarify what "all of the sudden gradually" means? 你能澄清一下“所有的突然”是什么意思吗? Like, after a few hours -- and always the same number of hours -- then within a few seconds all server-side sockets throw exceptions? 比如,几个小时后 - 总是相同的小时数 - 然后在几秒钟内所有服务器端套接字抛出异常?

You don't mention the client thread reading the data that comes back. 您没有提到客户端线程读取返回的数据。 Perhaps it's stopped and you haven't noticed. 也许它停了,你没有注意到。 (What is the client thread doing when the server thread encounters the 500 rapid exceptions? Try a few stack dumps in succession to see.) (当服务器线程遇到500个快速异常时,客户端线程正在做什么?连续尝试几个堆栈转储以查看。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM