HTTP1.1连接:使用java发生的keepalive实现withjava.net.SocketTimeoutException:读取超时

时间:2016-10-13 05:53:01

标签: java sockets server http-1.1

我使用java套接字编程实现了带有version1.1的http服务器。我使用版本1.0示例代码,我希望通过不关闭套接字实用程序来添加持久连接功能"连接:关闭"发送到服务器。但是,我遇到了" java.net.SocketTimeoutException:读取超时"输入后的信息,例如" localhost:8080 / xxxx"在我的浏览器上,并在使用客户端程序测试时没有收到任何内容代码太长了,我提到的事情部分吼叫!你能找到我的问题,谢谢!

////////here is the server part using thread pool techs
//Webserver class
 protected static Properties props = new Properties();
/* Where worker threads stand idle */
static Vector threads = new Vector();
 public static void main(String[] a) throws Exception {
        int port = 8080;
        if (a.length > 0) {
            port = Integer.parseInt(a[0]);
        }
        loadProps();
        printProps();
        /* start worker threads */
        for (int i = 0; i < workers; ++i) {
            Worker w = new Worker();
            (new Thread(w, "worker #"+i)).start();
            threads.addElement(w);
        }
        ServerSocket ss = new ServerSocket(port);
        while (true) {
            Socket s = ss.accept();
            Worker w = null;
            synchronized (threads) {
                if (threads.isEmpty()) {
                    Worker ws = new Worker();
                    ws.setSocket(s);
                    (new Thread(ws, "additional worker")).start();
                } else {
                    w = (Worker) threads.elementAt(0);
                    threads.removeElementAt(0);
                    w.setSocket(s);
                }
            }
       }
}

//Worker class inherit from Webserver class
    byte[] buf;
    Worker() {
        buf = new byte[BUF_SIZE];
        s = null;
    }

    synchronized void setSocket(Socket s) {
        this.s = s;
        notify();
    }

    public synchronized void run() {
        while(true) {
            if (s == null) {
                /* nothing to do */
                try {
                    wait();
                } catch (InterruptedException e) {
                    /* should not happen */
                    continue;
                }
            }
            try {
                handleClient();
            } catch (Exception e) {
                e.printStackTrace();
            }
            /* go back in wait queue if there's fewer
             * than numHandler connections.
             */
            if(!headAttri.getPersistConnec())
                s = null;
            //
            Vector pool = WebServer.threads;
            synchronized (pool) {
                if (pool.size() >= WebServer.workers) {
                    /* too many threads, exit this one */
                    try{
                        if(s != null)
                            s.close();
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                    return;
                } else {
                    if(!headAttri.getPersistConnec())
                        pool.addElement(this);
                }
            } 
        }
    }

//in handle client I mention the socket handles here(s is the socket)
void handleClient() throws IOException {
//...
s.setSoTimeout(WebServer.timeout);
s.setTcpNoDelay(true);

//...
try{
//...handle request and response the client
//...
}finally{
//close socket if head info "Connection: close" is found
            if(headAttri.getPersistConnec()){
                s.setKeepAlive(true);
            }
            else{
                s.close();
            }

}

}
//////////end server part

//////here is the client part
public SimpleSocketClient()
  {
    String testServerName = "localhost";
    int port = 8080;
    try
    {
      // open a socket
      Socket socket = openSocket(testServerName, port);

      // write-to, and read-from the socket.
      // in this case just write a simple command to a web server.
      String result = writeToAndReadFromSocket(socket, request_str[1]);

      // print out the result we got back from the server
      System.out.println(result);

      // close the socket, and we're done
      socket.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

private Socket openSocket(String server, int port) throws Exception
  {
    Socket socket;

    // create a socket with a timeout
    try
    {
      InetAddress inteAddress = InetAddress.getByName(server);
      SocketAddress socketAddress = new InetSocketAddress(inteAddress, port);

      // create a socket
      socket = new Socket();

      // this method will block no more than timeout ms.
      int timeoutInMs = 10*1000;   // 10 seconds
      socket.connect(socketAddress, timeoutInMs);

      return socket;
    } 
    catch (SocketTimeoutException ste) 
    {
      System.err.println("Timed out waiting for the socket.");
      ste.printStackTrace();
      throw ste;
    }
  }

private String writeToAndReadFromSocket(Socket socket, String writeTo) throws Exception
  {
    try 
    {
      // write text to the socket
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
      bufferedWriter.write(writeTo);
      bufferedWriter.flush();
      //test
      //bufferedWriter.write("GET src/WebServer.java HTTP/1.1\r\nHost: localhost\r\nConnection: close");
      //bufferedWriter.flush();

      // read text from the socket
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      StringBuilder sb = new StringBuilder();

      //string handling code
      String str;
      while ((str = bufferedReader.readLine()) != null)
      {
        sb.append(str + "\n");
      }

      // close the reader, and return the results as a String
      bufferedReader.close();
      return sb.toString();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
      throw e;
    }
  }
////end client part

2 个答案:

答案 0 :(得分:0)

//close socket if head info "Connection: close" is found
            if(headAttri.getPersistConnec()){
                s.setKeepAlive(true);

很难从你的代码中判断出你在做什么但基于这段代码片段看起来你正在混淆HTTP keep alive(即Connection: keep-alive处理,单个TCP连接中的多个请求) TCP保持活动状态(检测TCP连接断开)。有关差异的说明,请参阅Relation between HTTP Keep Alive duration and TCP timeout durationHTTP Keep Alive and TCP keep alive

答案 1 :(得分:0)

  

我希望通过不关闭套接字工具来添加持久连接功能“连接:关闭”发送到服务器

这不是你怎么做的。您必须自己关闭连接,

  • 收到带有Connection: close标头的请求后您已发送回复

  • 当读取下一个请求的套接字上出现读取超时时。

    读取超时的长度完全取决于您,因为您可以自行保护自己免受DOS攻击等。

NB调用Socket.setKeepAlive(true)绝对没有任何关系。

注意2您应该查看java.util.concurrent.Executor而不是实现自己的线程池。