?? connectlisten.java
字號:
package cr;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
class ConnectListen extends Thread{
Socket socket;
BufferedReader reader;
PrintWriter writer;
ServerMain smaTemp;
boolean booKick;
ConnectListen(Socket sock,ServerMain smaTemp){
this.socket=sock;
this.smaTemp=smaTemp;
booKick=false;
}
public void run(){
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(),true);
String msg;
while((msg=reader.readLine())!=null){
String strObject=msg.substring(0, msg.indexOf("\1"));
String strContent=msg.substring(msg.indexOf("\1")+1);
String strName=smaTemp.ltaOnline.isName(smaTemp.cpoOnline.indesOf(socket)+1);
if (strObject.equals("所有人")){
smaTemp.cpoOnline.sendAll(strName+"對所有人說:"+strContent);
smaTemp.jtaChats.append(strName+"對所有人說:"+strContent+"\n");
}
else {
if (smaTemp.ltaOnline.isBeing(strObject)){
int intObject=smaTemp.ltaOnline.getIndex(strObject)-1;
smaTemp.cpoOnline.sendTo(intObject,strName+"對你說:"+strContent);
writer.println("你對"+strObject+"說:"+strContent);
smaTemp.jtaChats.append(strName+"對"+strObject+"說:"+strContent+"\n");
}
}
}
}
catch (IOException ex) {
}
finally{
if(booKick==false)
try {
String strName=smaTemp.ltaOnline.isName(smaTemp.cpoOnline.indesOf(socket)+1); //獲取本進程對象名字
smaTemp.cpoOnline.remove(socket); //在聯接池刪除自身
smaTemp.ltaOnline.delElement(strName); //在在線列表中刪除自己
smaTemp.ltaOnline.refurbish();
smaTemp.jlaOnlineSum.setText(""+smaTemp.ltaOnline.getSum()); //更新在線人數
smaTemp.jtaChats.append("系統廣播:"+strName+"走了!"+"\n"); //在服務器端聊天窗口進行提示
smaTemp.cpoOnline.sendAll("系統廣播:"+strName+"走了!"); //在全聊天室進行廣播
smaTemp.cpoOnline.sendAll("DEL");
smaTemp.cpoOnline.sendAll(strName); //通知所有客戶端的在在線列表中刪掉這個名字
if (reader != null) {reader.close(); }
if (writer!=null){writer.close();}
if (socket!=null){socket.close();}
reader=null;writer=null;socket=null;smaTemp=null;
}
catch (IOException ex1) {
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -