如何在 Java 1.4 中设置 BufferedReader 和 PrintWriter 的超时?
如何在使用套接字连接创建的 BufferedReader 和 PrintWriter 上设置超时?下面是我现在的服务器代码,它一直工作到服务器或客户端崩溃为止:
while(isReceiving){
str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
while ((str = br.readLine()) != null){
System.out.println("Processing command " + str);
pw.println(client.message(str));
}
}
在该代码的范围之外,我施加了 1000 毫秒的套接字超时,这在等待初始连接时按预期工作。但程序在 (str = br.readLine()) 处阻塞。如果客户端挂起或崩溃,它永远不会停止阻塞,除非我终止进程(即使这样也并不总是有效)。
有问题的客户端代码与此非常相似,并且以类似的方式进行阻塞。
How does one set a timeout on a BufferedReader and a PrintWriter created using a socket connection? Here is the code I have for the server right now, which works until either the server or the client crashes:
while(isReceiving){
str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
while ((str = br.readLine()) != null){
System.out.println("Processing command " + str);
pw.println(client.message(str));
}
}
Outside the scope of this code I have imposed a socket timeout of 1000ms, which works as intended when waiting for the initial connection. But the program blocks at (str = br.readLine()). If the client hangs or crashes, it never stops blocking unless I terminate the process (which even then doesn't always work).
The client code in question is very similar to this, and is blocking in a similar fashion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用
Socket.setSoTimeout()
在套接字上设置读取超时。如果指定的读取超时到期,这将导致任何读取方法抛出SocketTimeoutException
。注意:读取超时不是在流上设置的,而是通过 Socket.setSoTimeout() 在底层 Socket 上设置的。TCP 中没有写入超时之类的东西.
You need to set a read timeout on the socket, with
Socket.setSoTimeout()
. This will cause any read method to throw aSocketTimeoutException
if the read timeout specified expires. NB Read timeouts are set not on the stream but on the underlyingSocket,
viaSocket.setSoTimeout().
There is no such thing as a write timeout in TCP.
您可以使用 SimpleTimeLimiter< /a> 来自 Google 的 Guava 库。
示例代码(Java 8):
You could use SimpleTimeLimiter from Google's Guava library.
Sample code (in Java 8):
答案在此 问题描述了一种使用
计时器
关闭连接的有趣方法。我不能百分百确定这在阅读过程中是否有效,但值得一试。从该答案复制:
isFinished
应该是一个boolean
变量,当您完成从流中的读取时,应将其设置为true
。An answer in this question describes an interesting method using a
Timer
to close the connection. I'm not 100% sure if this works in the middle of a read, but it's worth a shot.Copied from that answer:
isFinished
should be aboolean
variable that should be set totrue
when you're done reading from the stream.由于调用 socket.close() 似乎没有中断 br.readLine() 处的块,因此我做了一些解决方法。当断开客户端与服务器的连接时,我只需发送一个字符串“bye”,并告诉服务器在收到此命令时关闭套接字连接。
Since calling socket.close() did not seem to interrupt the block at br.readLine(), I did a little workaround. When disconnecting the client from the server, I merely send through a string "bye", and told the server to close the socket connection when it receives this command.