简体   繁体   English

Java中的套接字空闲超时异常

[英]Socket Idle timeout Exception In java

Basically am a newbie to socket programing. 基本上是套接字编程的新手。 i would like to know about how to close a socket if stays idle for a specified time interval. 我想知道如果在指定的时间间隔内保持空闲状态,如何关闭套接字。 i searched on net about this ,i found that function which is used to close the socket after the specified interval. 我在网上搜索了一下,发现该函数用于在指定间隔后关闭套接字。 but here in my case , i would like to close the socket only when it is stays Idle for more than the specified interval 但是在我的情况下,我只想在空闲状态超过指定时间间隔时关闭套接字

I searched on net about this 我在网上搜索了此内容

Why? 为什么? The Javadoc exists. Javadoc存在。 No searching necessary. 无需搜索。

I found that function which is used to close the socket after the specified interval 我发现了用于在指定间隔后关闭套接字的函数

There is no such method. 没有这种方法。

I saw about setSoTimeOut(2000) function which closes the socket after the specifed time interval 我看到了setSoTimeOut(2000)函数,该函数在指定的时间间隔后关闭套接字

No it doesn't. 不,不是。 It doesn't close the socket at all, and it causes read methods to throw a SocketTimeoutException if no data arrives within the timeout period. 它根本不会关闭套接字,并且如果在超时时间内没有数据到达,它将导致读取方法抛出SocketTimeoutException

but I would like to close only if the socket remains idle for the specified interval 但是我只想在套接字在指定间隔内保持空闲状态时关闭

Socket.setSoTimeout() is exactly what you need. Socket.setSoTimeout()正是您所需要的。

the client establishes the connection with the server and then later after sometime it the client close the socket connection at its side after performing the required task and creates a new connection the next time when it pings, where as my server does not close the connection and it keeps on listening to that client 客户端建立与服务器的连接,然后稍后,客户端在执行所需的任务后关闭其一侧的套接字连接,并在下次ping时创建新的连接,因为我的服务器不关闭连接,它一直在听那个客户

In other words your server is ignoring end of stream on the socket. 换句话说,您的服务器将忽略套接字上的流结束。 Don't do that. 不要那样做 Close the socket if you get end of stream from a read method. 如果从read方法获得流的结尾,请关闭套接字。

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

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