16

如何在使用套接字连接创建的 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()) 处阻塞。如果客户端挂起或崩溃,它永远不会停止阻塞,除非我终止进程(即使那样也不总是有效)。

有问题的客户端代码与此非常相似,并且以类似的方式阻塞。

4

4 回答 4

20
  1. 您需要在套接字上设置读取超时,使用Socket.setSoTimeout(). SocketTimeoutException如果指定的读取超时到期,这将导致任何读取方法抛出一个。NB 读取超时设置不是在流上而是在底层Socket,通过Socket.setSoTimeout().

  2. TCP 中没有写超时之类的东西。

于 2011-07-23T08:48:28.897 回答
17

您可以使用Google 的 Guava 库中的SimpleTimeLimiter

示例代码(在 Java 8 中):

BufferedReader br = ...;
TimeLimiter timeLimiter = new SimpleTimeLimiter();

try {
    String line = timeLimiter.callWithTimeout(br::readLine, 10, TimeUnit.SECONDS);
} catch (TimeoutException | UncheckedTimeoutException e) {
    // timed out
} catch (Exception e) {
    // something bad happened while reading the line
}
于 2015-07-24T13:20:43.180 回答
5

这个问题的答案描述了一个有趣的方法,使用 aTimer来关闭连接。我不是 100% 确定这在阅读过程中是否有效,但值得一试。

从那个答案复制:

TimerTask ft = new TimerTask(){
   public void run(){
     if (!isFinished){
       socket.close();
     }
   }
};

(new Timer()).schedule(ft, timeout);

isFinished应该是一个变量,当您完成从流中读取时boolean应该设置为。true

于 2011-07-22T16:16:01.457 回答
2

由于调用 socket.close() 似乎并没有中断 br.readLine() 处的块,所以我做了一些解决方法。在断开客户端与服务器的连接时,我只发送一个字符串“bye”,并告诉服务器在收到此命令时关闭套接字连接。

while ((str = br.readLine()) != null){
    // If we receive a command of "bye" the RemoteControl is instructing
    // the RemoteReceiver to close the connection.
    if (str.equalsIgnoreCase("bye")){
        socket.close();
            break;
    }
    System.out.println("Processing command " + str);
    pw.println(client.message(str));
}
于 2011-07-22T19:31:42.607 回答