使用jdk1.5的线程池每5秒并发40个工作线程(代码如下),但是执
行的时候有些线程报连接超时的异常
Connection timed out: connect
不知为什么?????????????????????????????
我的感觉是:并发的建了40个连接同一台主机的socket出现的,但
不知道为什么?请高手指点
    public class TestThread extends Thread{
int i;
TestThread(int i)
{
this.i=i;
}
        public void run()
        {
             String response = "";
Socket tempSocket=null;
try
{
tempSocket = new Socket("192.168.0.33", 6666);
System.out.println(i+"-----------ok");
}
catch (Exception e)
{
        System.out.println(i+"--------------------------------存在异常");
        System.out.println(e.getMessage());
}
finally
{
try {
if(tempSocket!=null)
{
    tempSocket.close();
}    
} catch (IOException e) {
}
}
    }

解决方案 »

  1.   

    我就想写一个测试某台主机的连通性(包括ip和端口),于是用了new socket的办法,但是抛异常,所以请大家给指点一下.先谢了
      

  2.   

    你应该让40个线程相隔比如500ms一个个去连,同时并发的话服务端如果代码写得不好或是机器的cpu一下子不够就塞住了
      

  3.   

    由于你是客户端并主动发起socket连接,所以所有的压力都落在服务端上。一次并发40个线程去连接服务端好像是在做服务端的测试。如果没有办法确定服务端的并发性能的话,我觉得你除了减少并发连接数量外还是无能为力的。
      

  4.   

    我都看得出服务器被你玩死了,拿牛拉火车,牛最多给你拉坨屎
    服务器还好,给你个CONNECTION TIMEROUT
      

  5.   

    强人~~~java敢这样写吗~~~
      

  6.   

    package qqserver;
    import java.net.*;
    import java.io.*;
    public class oneServerThread extends Thread{//发送数据
        private Socket s;
        private QQServerFrame QQ;
        public oneServerThread(Socket s,QQServerFrame QQ) {
            this.s=s;
            this.QQ=QQ;
        }
        public void run(){
            try{
                String info=QQ.txtUserSend.getText();
                BufferedReader in=new BufferedReader(new InputStreamReader(System.in,info));
                PrintWriter out=new PrintWriter(s.getOutputStream());
                while(true){
                    String str=in.readLine();
                    out.println(str);
                    QQ.txtInfoShow.append(str+"\n");
                    if(str.equals("bye")){
                        break;
                    }
                }
             in.close();
             out.close();
            }catch(Exception se){
                se.printStackTrace();
            }
        }
    }
      

  7.   

    package chatqq;import java.net.*;
    import java.io.*;
    public class ClientThread extends Thread{//接Server 数据
        private PrintWriter out = null;
        private Socket s = null;
        private BufferedReader in;
        private String str=null;
        private QQClientFrame QQ=null;
        public ClientThread(Socket s,QQClientFrame QQ){
            this.s = s;
            this.QQ=QQ;
        }
        public void run(){
            try {
                    in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    out = new PrintWriter(s.getOutputStream(),true);
                    out.println(str);
                    while(true){
                        str=in.readLine();
                        if(str.equals("bye")){
                            str="client from Server1"+str;
                            out.println(str);
                            QQ.txtInfoShow.append(str+"\n");
                            break;
                        }else{
                            str="client from Server2"+str;
                            out.println(str);
                            QQ.txtInfoShow.append(str+"\n");
                        }
                        this.stop();
                    }
                    in.close();
                    out.close();
                    s.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }    }}