【文件属性】:
文件名称:QQ Server 端代码
文件大小:804B
文件格式:JAVA
更新时间:2014-06-21 18:57:43
QQ Server
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.HashMap;
public class ServerThread extends Thread{
private HashMap clients = null;
private Socket s1 = null;
private String name = null;
private String id = null;
public ServerThread(HashMap clients, Socket s1, String name,String id) {
this.clients = clients;
this.s1 = s1;
this.name = name;
this.id = id;
}
public void run(){
BufferedReader rebuf = null;
try {
while(true){
rebuf = new BufferedReader(new InputStreamReader(s1.getInputStream()));
String message = rebuf.readLine();
if(message.equalsIgnoreCase("exit")){
clients.remove(id);
break;
}
String[] str = message.split(":@#");
if(clients.get(str[0]) == null){
BufferedWriter wrbuf = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
wrbuf.write("false\n");
wrbuf.flush();
}else{
Socket s2 = clients.get(str[0]);
BufferedWriter wrbuf = new BufferedWriter(new OutputStreamWriter(s2.getOutputStream()));
wrbuf.write(name+":"+str[1]+"\n");
wrbuf.flush();
}
}
s1.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
}
}
}