使用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) {
}
}
}
行的时候有些线程报连接超时的异常
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) {
}
}
}
服务器还好,给你个CONNECTION TIMEROUT
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();
}
}
}
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();
} }}