?? communicatethread.java
字號(hào):
package chatserver2;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class CommunicateThread extends Thread{//保持連接線程
protected Socket clientSock;
protected BufferedReader in=null;
protected PrintWriter out;
ChatRoomServer chatFrame;
boolean isTrue=true;//run()
java.util.Vector inforStack;
int index2;//
public CommunicateThread(Socket Sock,ChatRoomServer cFrame,int index){
clientSock=Sock;
chatFrame=cFrame;
index2=index;
inforStack=new java.util.Vector();
try{
in=new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
out=new PrintWriter(clientSock.getOutputStream());
}catch(IOException ei){
try{
clientSock.close();
}catch(IOException ei2){ }
chatFrame.processMsg(ei.toString());
return;
}
this.start();
}
public void run(){
String infor;
try{
while(isTrue){
infor=in.readLine();
if(infor.equals("Client exit!")){
writeInformation(infor);//把信息寫到信息棧,以倍廣播出去
stopRun();
}else if(infor!=null){
writeInformation(infor);
}//else break;
try{
Thread.sleep(100);//version2
}catch(InterruptedException ex){}
}
}catch(IOException e){ ;}
finally{
try{
in.close();
out.close();
clientSock.close();
chatFrame.clients.remove(index2);//在clients中清除本線程序
ChatRoomServer.index--;//
}catch(IOException ei){;}
}
}
public void writeInformation(String infor){//寫信息棧
inforStack.add(infor);
}
private void stopRun(){//終止線程
isTrue=false;
}
public void sendInformation(String str){//發(fā)送信息
try{
out.println(str);
out.flush();
}catch(Exception e){;}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -