Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTTP1.1连接:keepalive使用java实现发生在java.net.SocketTimeoutException:读取超时_Java_Sockets_Server_Http 1.1 - Fatal编程技术网

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

HTTP1.1连接:keepalive使用java实现发生在java.net.SocketTimeoutException:读取超时,java,sockets,server,http-1.1,Java,Sockets,Server,Http 1.1,我正在使用java套接字编程实现版本为1.1的http服务器。我使用的是1.0版的示例代码,我希望通过不关闭套接字来添加持久连接功能,并向服务器发送“connection:close”。然而,在我的浏览器上输入“localhost:8080/xxxx”后,我得到了“java.net.SocketTimeoutException:Read timeout”信息,并且在使用客户端程序测试时没有收到任何信息。代码太长了,我在下面提到了这件事!你能帮我找到问题吗,谢谢 ////////here is t

我正在使用java套接字编程实现版本为1.1的http服务器。我使用的是1.0版的示例代码,我希望通过不关闭套接字来添加持久连接功能,并向服务器发送“connection:close”。然而,在我的浏览器上输入“localhost:8080/xxxx”后,我得到了“java.net.SocketTimeoutException:Read timeout”信息,并且在使用客户端程序测试时没有收到任何信息。代码太长了,我在下面提到了这件事!你能帮我找到问题吗,谢谢

////////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
//这是使用线程池技术的服务器部件
//Web服务器类
受保护的静态属性props=新属性();
/*工作线程处于空闲状态的位置*/
静态向量线程=新向量();
公共静态void main(字符串[]a)引发异常{
int端口=8080;
如果(a.长度>0){
port=Integer.parseInt(a[0]);
}
loadProps();
printProps();
/*启动工作线程*/
对于(int i=0;i=WebServer.workers){
/*线程太多,请退出此线程*/
试一试{
如果(s!=null)
s、 close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回;
}否则{
如果(!headAttri.getPersistConnec())
池。附录(本);
}
} 
}
}
//在handle client中,我在这里提到了套接字句柄(s是套接字)
void handleClient()引发IOException{
//...
s、 设置超时(WebServer.timeout);
s、 setTcpNoDelay(真);
//...
试一试{
//…处理客户端的请求和响应
//...
}最后{
//如果发现头部信息“连接:关闭”,则关闭插座
if(headAttri.getPersistConnec()){
s、 setKeepAlive(真);
}
否则{
s、 close();
}
}
}
//////////终端服务器部件
//////这是客户端部分
公共SimpleCheckClient()
{
字符串testServerName=“localhost”;
int端口=8080;
尝试
{
//打开插座
套接字=openSocket(testServerName,端口);
//对套接字进行写入和读取。
//在本例中,只需向web服务器写入一个简单的命令。
字符串结果=writeToAndReadFromSocket(socket,request_str[1]);
//打印出我们从服务器得到的结果
系统输出打印项次(结果);
//合上插座,我们就完成了
socket.close();
}
捕获(例外e)
{
e、 printStackTrace();
}
}
私有套接字openSocket(字符串服务器,int端口)引发异常
{
插座;
//创建具有超时的套接字
尝试
{
InetAddress inteAddress=InetAddress.getByName(服务器);
SocketAddress SocketAddress=新的InetSocketAddress(inteAddress,端口);
//创建一个套接字
套接字=新套接字();
//此方法的阻止时间不超过超时毫秒。
int timeoutims=10*1000;//10秒
socket.connect(socketAddress,timeoutims);
返回插座;
} 
捕捉(SocketTimeoutException ste)
{
System.err.println(“等待套接字超时”);
ste.printStackTrace();
投掷ste;
}
}
私有字符串writeToAndReadFromSocket(套接字套接字,字符串writeTo)引发异常
{
尝试
{
//将文本写入套接字
BufferedWriter BufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream());
bufferedWriter.write(writeTo);
bufferedWriter.flush();
//试验
//write(“GET src/WebServer.java HTTP/1.1\r\nHost:localhost\r\n连接:close”);
//bufferedWriter.flush();
//从套接字读取文本
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
StringBuilder sb=新的StringBuilder();
//字符串处理代码
字符串str;
而((str=bufferedReader.readLine())!=null)
{
sb.追加(str+“\n”);
}
//关闭读卡器,并返回res
//close socket if head info "Connection: close" is found
            if(headAttri.getPersistConnec()){
                s.setKeepAlive(true);