不知道你说的是什么问题,我测试了你的代码,可以工作,但是服务器端会出现异常,原因是你的客户端在发送了“你好”之后就关闭了Socket,而服务器却尝试继续监听已经关闭的Socket,当然会出错了。
还有,
while(true)
{ try{ server=new ServerSocket(4331);}
//该端口下的ServerSocket只能有一个,当生成一个以后再生成第二个就会出错,
catch(IOException el) {System.out.println("正在监
听"+"ERRO1:"+el);}
try{ you=server.accept();}
//you这个变量已经实例化了吗?
catch (IOException e)
{System.out.println("正在等待客户");}
if(you!=null)
{new Server_thread(you).start(); }
else {continue;}
}
应该是这样:
try{
server=new ServerSocket(4331);
while(true){
try{
you=server.accept();
new Server_thread(you).start();
}catch(IOException e){....}
}
}catch(Exception e){....}
还有,
while(true)
{ try{ server=new ServerSocket(4331);}
//该端口下的ServerSocket只能有一个,当生成一个以后再生成第二个就会出错,
catch(IOException el) {System.out.println("正在监
听"+"ERRO1:"+el);}
try{ you=server.accept();}
//you这个变量已经实例化了吗?
catch (IOException e)
{System.out.println("正在等待客户");}
if(you!=null)
{new Server_thread(you).start(); }
else {continue;}
}
应该是这样:
try{
server=new ServerSocket(4331);
while(true){
try{
you=server.accept();
new Server_thread(you).start();
}catch(IOException e){....}
}
}catch(Exception e){....}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货