tomcat connectionTimeout 附测试demo

单位毫秒,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");  
    }
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值