81,122
社区成员




class AcceptThread extends Thread
{
public void run()
{
try
{
while(true)
{
ket=server.accept();
ReadMessageThread readThread = new ReadMessageThread(ket);
readThread.start();
}
}
catch(Exception e2)
{
liaotianchuangkou.append("用户连接服务器出错\n");
}
}
}
class ReadMessageThread extends Thread
{
Socket s;
public ReadMessageThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
cin=new BufferedReader(new InputStreamReader(s.getInputStream()));
cout=new PrintStream(s.getOutputStream());
}
catch(Exception e3)
{
liaotianchuangkou.append("输入输出异常_1\n");
}
String str="";
while(true)
{
try
{
str=cin.readLine();
if(null!=str && !str.equals("")){
ipc.ipc(str.substring(0,2), str.substring(3),cout,s);
}else{
System.out.println("str对象为NULL或者是空串");
}
}catch(Exception e){
liaotianchuangkou.append("输入输出异常_2\n");
e.printStackTrace();
return ;
}
if(null==str || str.equals("QUIT"))
{
try
{
ket.close();
}
catch(Exception e1)
{
liaotianchuangkou.append("套接字关闭异常\n");
}
break;
}
}
}
}
public void ipc(String systemFlag, String content, PrintStream cout, Socket s){
PrintStream pStream = null;
if("01".equals(systemFlag)){
sessionIdMap.put(content, s);
}else if("02".equals(systemFlag)){
cout.println("021" + content);
}else if("03".equals(systemFlag)){
String str = "";
str = systemFlag + "1" + content;
String receive = content.substring(10,20);
Socket nowKet = sessionIdMap.get(receive);
if(null != nowKet){
try {
pStream = new PrintStream(nowKet.getOutputStream());
pStream.println(str);
} catch (IOException e) {
System.out.println("发送错误...");
}
}else{
System.out.println("没有找到合适的对象发送消息...");
}
String returnStr = "021" + content.substring(0,10) + receive + content.substring(20);
System.out.println("回发..." + returnStr);
try{
PrintStream replyStream = new PrintStream(((Socket)sessionIdMap.get(content.substring(0,10))).getOutputStream());
replyStream.println(returnStr);
}catch(IOException e5){
System.out.println("回发出错...");
}
}else{
System.out.println("其他通道,其他...");
cout.println(content);
}
}