ServerAgentThread.java
~~~
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;
public class ServerAgentThread extends Thread {
Server father;//聲明Server的引用
Socket sc;//聲明Socket的引用
DataInputStream din;//聲明數據輸入流與輸出流的引用//z互相發送信息
DataOutputStream dout;
boolean flag=true;//控制線程的標志位
public ServerAgentThread(Server father,Socket sc)
{
this.father=father;
this.sc=sc;
try
{
din=new DataInputStream(sc.getInputStream());//創建數據輸入流
dout=new DataOutputStream(sc.getOutputStream());//創建數據輸出流
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
while(flag)
{
try
{
String msg=din.readUTF().trim();//接收客戶端傳來的信息
if(msg.startsWith("<#NICK_NAME#>"))//收到新用戶的信息
{
this.nick_name(msg);
}
else if(msg.startsWith("<#CLIENT_LEAVE#>")){//收到用戶離開的信息
this.client_leave(msg);
}
else if(msg.startsWith("<#TIAO_ZHAN#>")) {//收到用戶發出的挑戰信息
this.tiao_zhan(msg);
}
else if(msg.startsWith("<#TONG_YI#>")){//受到接受挑戰的信息
this.tong_yi(msg);
}
else if(msg.startsWith("<#BUTONG_YI#>")){//受到拒絕挑戰的信息
this.butong_yi(msg);
}
else if(msg.startsWith("<#BUSY#>")){//收到被挑戰者忙的信息
this.busy(msg);
}
else if(msg.startsWith("<#MOVE#>")){//收到走棋的信息
this.move(msg);
}
else if(msg.startsWith("<#RENSHU#>")){//收到某用戶認輸的信息
this.renshu(msg);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//收到新用戶的信息
public void nick_name(String msg)
{
try
{
String name=msg.substring(13);//獲得用戶的昵稱
this.setName(name);//用該昵稱給該線程取名
Vector v=father.onlineList;//獲得在線用戶列表
boolean isChongMing=false;
int size=v.size();//獲得用戶列表的大小
for(int i=0;i<size;i++)
{//遍歷列表,查看是否已經有該用戶名
ServerAgentThread tempSat=(ServerAgentThread)v.get(i);
if(tempSat.getName().equals(name))
{
isChongMing=true;//有重名,將標志位設為true
break;
}
}
if(isChongMing==true)//如果重名
{
dout.writeUTF("<#NAME_CHONGMING#>");//將重名信息發送給客戶端
din.close();//關閉數據輸入流
dout.close();//關閉數據輸出流
sc.close();//關閉Socket
flag=false;//終止該服務器代理線程
}
else//如果不重名
{
v.add(this);//將該線程添加到在線列表
father.refreshList();//刷新服務器在線信息列表
String nickListMsg="";
size=v.size();//獲得在線列表大小
for(int i=0;i<size;i++)
{
ServerAgentThread tempSat=(ServerAgentThread)v.get(i);//z這時才分配代理線程
nickListMsg=nickListMsg+"|"+tempSat.getName();
}//將在線列表內容住組織成字符串
nickListMsg="<#NICK_LIST#>"+nickListMsg;
Vector tempv=father.onlineList;
size=tempv.size();
for(int i=0;i<size;i++)
{//遍歷在線列表
ServerAgentThread satTemp=(ServerAgentThread)tempv.get(i);
satTemp.dout.writeUTF(nickListMsg);//將最新的列表信息發送到各個客戶端
if(satTemp!=this)
{//給其他客戶端發送新用戶上線的信息
satTemp.dout.writeUTF("<#MSG#>"+this.getName()+"上線了...");
}
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
//收到用戶離開的信息
public void client_leave(String msg){
try{
Vector tempv=father.onlineList;//獲得在線列表
tempv.remove(this);//移除該用戶
int size=tempv.size();
String nl="<#NICK_LIST#>";
for(int i=0;i<size;i++){//遍歷在線列表
ServerAgentThread satTemp=(ServerAgentThread)tempv.get(i);
//向各個客戶端發送用戶離線信息
satTemp.dout.writeUTF("<#MSG#>"+this.getName()+"離線了...");
//組織信息的在線用戶列表
nl=nl+"|"+satTemp.getName();
}
for(int i=0;i<size;i++){//將最新的列表信息發送到各個客戶端
ServerAgentThread satTemp=(ServerAgentThread)tempv.get(i);
satTemp.dout.writeUTF(nl);
}
this.flag=false;//終止該服務器代理線程
father.refreshList();//更新服務器在線用戶列表
}
catch(IOException e){e.printStackTrace();}
}
//收到用戶發出的挑戰信息
public void tiao_zhan(String msg)
{
try
{
String name1=this.getName();//獲得發出挑戰信息用戶的名字
String name2=msg.substring(13);//獲得被挑戰的用戶名字
Vector v=father.onlineList;//獲得在線用戶列表
int size=v.size();//獲得在線用戶列表的大小
for(int i=0;i<size;i++)
{//遍歷列表,搜索被挑戰的用戶
ServerAgentThread satTemp=(ServerAgentThread)v.get(i);
if(satTemp.getName().equals(name2))
{//向該用戶發送挑戰信息,附帶提出挑戰用戶的名字
satTemp.dout.writeUTF("<#TIAO_ZHAN#>"+name1);
break;
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
//受到接受挑戰的信息
public void tong_yi(String msg){
try{
String name=msg.substring(11);//獲得提出挑戰的用戶的名字
Vector v=father.onlineList;//獲得在線用戶列表
int size=v.size();//獲得在線用戶列表的大小
for(int i=0;i<size;i++){//遍歷列表,搜索提出挑戰的用戶
ServerAgentThread satTemp=(ServerAgentThread)v.get(i);
if(satTemp.getName().equals(name)){//向該用戶發送對方接受挑戰的信息
satTemp.dout.writeUTF("<#TONG_YI#>");
break;
}
}
}
catch(Exception e){e.printStackTrace();}
}
//受到拒絕挑戰的信息
public void butong_yi(String msg){
try{
String name=msg.substring(13);//獲得提出挑戰的用戶的名字
Vector v=father.onlineList;//獲得在線用戶列表
int size=v.size();//獲得在線用戶列表的大小
for(int i=0;i<size;i++)
{//遍歷列表,搜索提出挑戰的用戶
ServerAgentThread satTemp=(ServerAgentThread)v.get(i);
if(satTemp.getName().equals(name)){//向該用戶發送對方拒絕挑戰的信息
satTemp.dout.writeUTF("<#BUTONG_YI#>");
break;
}
}
}
catch(IOException e){e.printStackTrace();}
}
//收到被挑戰者忙的信息
public void busy(String msg){
try{
String name=msg.substring(8);//獲得提出挑戰的用戶的名字
Vector v=father.onlineList;//獲得在線用戶列表
int size=v.size();//獲得在線用戶列表的大小
for(int i=0;i<size;i++){//遍歷列表,搜索提出挑戰的用戶
ServerAgentThread satTemp=(ServerAgentThread)v.get(i);
if(satTemp.getName().equals(name)){//向該用戶發送對方正在忙的信息
satTemp.dout.writeUTF("<#BUSY#>");
break;
}
}
}
catch(IOException e){e.printStackTrace();}
}
//收到走棋的信息
public void move(String msg){
try{
String name=msg.substring(8,msg.length()-4);//獲得接收方的名字
Vector v=father.onlineList;//獲得在線用戶列表
int size=v.size();//獲得在線用戶列表的大小
for(int i=0;i<size;i++){//遍歷列表,搜索接收方
ServerAgentThread satTemp=(ServerAgentThread)v.get(i);
if(satTemp.getName().equals(name)){//將該信息轉發給接收方
satTemp.dout.writeUTF(msg);
break;
}
}
}
catch(IOException e){e.printStackTrace();}
}
//收到某用戶認輸的信息
public void renshu(String msg){
try{
String name=msg.substring(10);//獲得接收方的名字
Vector v=father.onlineList;//獲得在線用戶列表
int size=v.size();//獲得在線用戶列表的大小
for(int i=0;i<size;i++){//遍歷列表,搜索接收方
ServerAgentThread satTemp=(ServerAgentThread)v.get(i);
if(satTemp.getName().equals(name)){//將該信息轉發給接收方
satTemp.dout.writeUTF(msg);
break;
}
}
}
catch(IOException e){e.printStackTrace();}
}
}
~~~