单位毫秒,Connector从接受连接到提交URI的等待的时间。Tomcat在客户端连接后等待客户端请求的时间
以下是测试demo
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1",8080);
System.out.println(s.isConnected());
System.out.println(s.isClosed());
for(int i = 1 ; i<=20 ;i++){
try {
Thread.sleep(1000);
System.out.println(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(s.isConnected());
System.out.println(s.isClosed());
StringBuffer sb = new StringBuffer();
sb.append("GET /tomcattimeout/timeout HTTP/1.1\r\n");
sb.append("Host: localhost:8088\r\n");
sb.append("Connection: Keep-Alive\r\n");
sb.append("\r\n");
System.out.println("发送报文 : " + sb.toString());
write(s.getOutputStream(), sb.toString());
InputStream is = s.getInputStream();
String line = null;
int contentLength = 0;
do {
line = readLine(is, 0);
if (line.startsWith("Content-Length")) {
contentLength = Integer.parseInt(line.split(":")[1].trim());
}
System.out.print(line);
} while (!line.equals("\r\n"));
//--输消息的体
System.out.print("返回报文, "+readLine(is, contentLength));
//关闭流
is.close();
}
public static void write(OutputStream out, String content) throws UnsupportedEncodingException, IOException
{
out.write(content.getBytes("utf-8"));
out.flush();
}
@SuppressWarnings("unchecked")
private static String readLine(InputStream is, int contentLe) throws IOException {
ArrayList lineByteList = new ArrayList();
byte readByte;
int total = 0;
if (contentLe != 0) {
do {
readByte = (byte) is.read();
lineByteList.add(Byte.valueOf(readByte));
total++;
} while (total < contentLe);//消息体读还未读完
} else {
do {
readByte = (byte) is.read();
lineByteList.add(Byte.valueOf(readByte));
} while (readByte != 10);
}
byte[] tmpByteArr = new byte[lineByteList.size()];
for (int i = 0; i < lineByteList.size(); i++) {
tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue();
}
lineByteList.clear();
return new String(tmpByteArr, "utf-8");
}
}