ing(yaoshuode.substring(4));//悄悄地对为4个字符s3=s1.concat(s2);for(i=0;i
}//客户端线程类FirstThreadclassFirstThreadextendsThread{protectedSocketclient;//line读取来自客户端的消息;name格式为:PEOPLE+名字+[性别]Stringline,name;protectedDataOutputStreamfirstout,out;//定义网络数据输出流protectedChatServerserver;protectedDataInputStreamin;//定义网络数据输出流
//FirstThread初始化线程publicFirstThread(ChatServerserver,Socketclient){this.server=server;this.client=client;try{in=newDataInputStream(client.getInputStream());out=newDataOutputStream(client.getOutputStream());firstout=newDataOutputStream(client.getOutputStream());}catch(IOExceptione){
try{server.connections.removeElement(this);client.close();}catch(IOExceptione2){System.err.println("有问题哦"+e);return;}}if(this.client==null){server.broadcast("QUIT"+this.name);this.name=null;}}publicvoidrun(){try{//客户端线程初始化运行读取服务端的已有聊天室人员列表信息inti;for(i=0;i try{firstout.writeUTF(c.name+"*");}catch(IOExceptione){}
}}}catch(ArrayIndexOutOfBoundsExceptione){}catch(NullPointerExceptione){}
//客户端线程始终在监听的操作方法try{while(true){line=in.readUTF();//line读取来自客户端线路的信息//线路信息前端为PEOPLE表明有人进入了聊天室if(line.startsWith("PEOPLE")){try{//获取当前对象的线程FirstThreadd=(FirstThread)(server.connections.elementAt(server.connections.indexOf(this)));
if(d.name==null){d.name=line;}//有人在一个窗口中换用另一个人的名字登陆elseif(d.name!=null){//写入QUIT+PEOPLE+名字+[性别]信息server.broadcast("QUIT"+this.name);d.name=line;//使原来的人离开聊天室}}catch(ArrayIndexOutOfBoundsExceptione){}catch(NullPointerExceptione){}finally{server.broadcast(line);}//向聊天室所有人员发送有人进入聊天室信息}//线路信息前端QUIT表明有人离开了聊天室elseif(line.startsWith("QUIT")){server.broadcast("QUIT"+this.name);server.connections.removeElement(this);this.in.close();
this.out.close();this.firstout.close();//关闭输入输出流this.client.close();this.yield();}//线路信息前端为MSG表明接收到的是普通聊天话语信息elseif(line.startsWith("YAOSHUODE")){server.broadcast(line);}//