java - 从输入流 Java 读取时有没有超时的方法?

标签 java tcp timeout inputstream

<分区>

Possible Duplicate:
Is it possible to read from a Java InputStream with a timeout?

我注意到当我尝试阅读更多信息然后发送到我的服务器时,网络浏览器会卡住。我看到我的套接字卡住了,因为网络浏览器返回的信息少于它试图读取的信息。有没有办法在 Currtly 上设置超时?我正在使用输入流

public String ReadLine()
 {
    String out;
    out="";
     // read in one line
     try{
         request = new StringBuffer(1000);
        boolean f=true;
        while(true)
        {
            int c=in.read();
            if (c=='\r') 
                {
                // next should be a \n
  // Program freezed hear
                 c=in.read();
                if (f==true)
                    return "";
                 break;
                }
            f=false;
            out=out+(char)c;

            request.append((char)c);
                } // end while

     } catch(IOException ec)
        {
            System.out.println(ec.getMessage());    
        }

        System.out.println(request);


    return out; 
 } 

最佳答案

Socket.setSoTimeout()HttpURLConnection.setReadTimeout()

关于java - 从输入流 Java 读取时有没有超时的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829211/

相关文章:

java - 为什么图片不显示在 java 的 swing 中?

android - 操作超时

java - JDBC 中的连接是什么?

javascript - react 空闲定时器: Video Player Activity Detected as Idle

jquery - 为什么jquery中的同步ajax调用不支持超时?

java - 来自 Java 应用程序客户端的 Jersey POST 请求

java - PHP程序员想学习Spring

java - 在我的应用程序中检索地理位置远不如 Google map 应用程序在我的设备上同时显示的准确

C#——TcpListener.Start() 导致 SocketException,消息为 "Only one usage of each socket address"

linux - 模拟两台 ubuntu 服务器机器之间的慢速连接