?? server.java
字號:
/* ****************************************************************
*
* Server.java
*
* Server 類 ,主機(jī)端運行, 負(fù)責(zé)監(jiān)聽及客戶端的管理工作
*
*************************************************************** */
import java.io.*;
import java.net.*;
import ConnectThread;
public class Server
{
ServerSocket s;
ConnectThread m_connect[];
byte m_count;
int MAXCLIENT;
String msou;
String mobj;
int mtype;
String mtxt;
public Server()
{
Socket socket;
socket = null;
m_count=0;
MAXCLIENT = 10;
m_connect = new ConnectThread[MAXCLIENT+1];
int i;
for(i=0;i<MAXCLIENT;i++)
{
m_connect[i]=null;
}
try
{
s = new ServerSocket(3000);
}
catch(Exception e)
{
System.out.println("Error in listener newing:"+e);
return;
}
System.out.println("Server is listening ...... ");
while(true)
{
try
{
socket = s.accept();
}
catch (Exception e)
{
System.out.println("Error in accepting : "+e);
}
if ( socket!= null )
{
InitConnect(socket);
}
else System.out.println("Error in initing ");
}
}
public String WriteMsg(int type,String obj,String txt)
{
String buf = new String("MSG");
Integer _type = new Integer(type);
buf +=_type.toString();
buf += '\\';
buf += obj;
buf += '\\';
buf += txt;
return buf;
}
public void DealInput(String str,ConnectThread sock)
{
System.out.print(" Receive Message:");
System.out.println(str);
String head = new String();
String obj ;
int type;
int i = 0;
String txt = new String();
int len = str.length();
//數(shù)據(jù)預(yù)處理
//消息結(jié)構(gòu)
// MSG /type (byte), /obj (String) , /0 ,/text (String)
type = 0;
obj = "";
txt = "";
char[] d = new char[str.length()];
str.getChars(0,str.length()-1,d,0);
i=0;
head = new String ();
if (len < 5 )
return;
head="";
head+=str.charAt(0);
head+=str.charAt(1);
head+=str.charAt(2);
if (!head.equals("MSG"))
{
System.out.print(" Receive Error Message : ");
System.out.println(str);
return;
}
i=3;
String _type = new String();
while(str.charAt(i) != '\\')
{
_type+=str.charAt(i);
i++;
}
i++;
type = (Integer.decode(_type)).intValue();
while(str.charAt(i) != '\\')
{
obj+=str.charAt(i);
i++;
}
i++;
while(i<len)
{
txt+=str.charAt(i);
i++;
}
// txt = str.substring(5,str.length());
// msg type
// 1 要求當(dāng)前所有登陸用戶名稱
// 2 發(fā)送當(dāng)前所有登陸用戶名稱
// 3 向主機(jī)發(fā)送自己的名字(新登陸)
// 4 向所有客戶發(fā)送新登陸著的名字
// 5 客戶斷開連接
// 6 通知所有客戶端已斷開的客戶名
// 7 發(fā)送挑戰(zhàn)請求 (執(zhí)黑)
// 8 發(fā)出挑戰(zhàn)請求 (執(zhí)白)
// 9 接受挑戰(zhàn)回應(yīng)
// 10 拒絕挑戰(zhàn)回應(yīng)
// 11 服務(wù)器已滿
// 12 新登陸名字沖突
// 13 不存在的用戶(已斷開連接)
// 20 chat
// 21 走棋
// 22 密談
String buf;
Integer _list;
{
System.out.println(str);
System.out.print(" Recieve msg , type : ");
System.out.println(type);
System.out.print("obj : ");
if (obj.equals ( "" )) System.out.println("null");
else System.out.println(obj);
System.out.print("正文 : ");
if (txt .equals("") ) System.out.println("null");
else System.out.println(txt);
}
String msg;
switch(type)
{
case 1:
{
//要求當(dāng)前所有登陸用戶名稱
buf= new String();
for(i=0;i<MAXCLIENT;i++)
{
if (m_connect[i]!=null)
{
_list = new Integer(i);
buf+=_list.toString();
buf+='\\';
buf+= m_connect[i].m_name;
buf+='\\';
}
}
msg = this.WriteMsg(2,"",buf.toString());
sock.Send(msg);
break;
}
case 2:
break;
case 3:
{
//接收到客戶端發(fā)送自己名字
for(i=0;i<MAXCLIENT;i++)
if (m_connect[i] != null )
if (m_connect[i].m_name.equals(txt))
{
//名字已被使用,拒絕連接
System.out.println("Name has been used , refuse connect");
msg = this.WriteMsg(12,"","");
sock.Send(msg);
sock.quit = true;
m_connect[sock.m_id] = null;
// sock.stop();
try
{
sock.m_input.close();
sock.m_output.close();
sock.m_socket.close();
}
catch(IOException e)
{
System.out.println("Error at close socket : " +e);
}
m_count--;
return;
}
m_connect[sock.m_id].m_name = txt;
//發(fā)送所有登陸著的名字
buf = "";
for(i=0;i<MAXCLIENT;i++)
{
if (m_connect[i]!=null && i != sock.m_id )
{
_list = new Integer(i);
buf+=_list.toString();
buf+='\\';
buf+= m_connect[i].m_name;
buf+='\\';
}
}
msg = this.WriteMsg(2,"",buf.toString());
sock.Send(msg);
// 將新名字發(fā)給所有用戶
buf = "";
_list = new Integer(sock.m_id);
buf+=_list.toString();
buf+='\\';
buf+=txt;
msg = WriteMsg(4,"",buf);
for(i=0;i<MAXCLIENT;i++)
{
if ( m_connect[i] != null && i != sock.m_id )
m_connect[i].Send(msg);
}
break;
}
case 5:
{
this.Disconnect(sock);
break;
}
case 10:
case 4:
case 6:
case 7:
case 8:
case 9:
case 20:
case 21:
default :
{ //轉(zhuǎn)發(fā)
if (obj.equals( "" ))
{
//向所有客戶端發(fā)送
System.out.println("Begin transport to all user");
for(i=0;i<MAXCLIENT ; i++)
{
if ( m_connect[i] != null && i != sock.m_id )
{
System.out.print(" Transport Data from ");
System.out.print(sock.m_name);
System.out.print (" to ");
System.out.print (m_connect[i].m_name);
System.out.print ( " : ");
System.out.println(str);
m_connect[i].Send(str);
}
}
}
else
for (i = 0; i<MAXCLIENT ; i++)
if(m_connect[i] != null)
if( obj.equals( m_connect[i].m_name ) )
{
System.out.println("Begin transport to current user");
System.out.print(" Transport Data from ");
System.out.print(sock.m_name);
System.out.print (" to ");
System.out.print (m_connect[i].m_name);
System.out.print ( " : ");
System.out.println(str);
m_connect[i].Send(str);
}
else
System.out.println("Invalid user : " + obj);
break;
}
}
}
public void InitConnect(Socket socket)
{
ConnectThread c = new ConnectThread(this,m_count,socket);
if(m_count>= MAXCLIENT)
{
//客戶數(shù)量已達(dá)到上限,拒絕連接
String msg = this.WriteMsg(11,"","");
c.Send(msg);
try
{
c.m_input.close();
c.m_output.close();
c.m_socket.close();
}
catch(IOException e)
{
System.out.println( " Error at closing socket : " +e);
return;
}
System.out.println(" A client connect to server , but server is full , refused .");
return;
}
int i;
for(i=0;i<MAXCLIENT;i++)
if(m_connect[i] == null )
{
m_connect[i] =c;
c.m_id = i;
break;
}
c.start();
System.out.print("Receive Connect ,list ");
System.out.println(i);
m_count++;
}
public static void main(String args[])
{
new Server();
}
public void Disconnect(ConnectThread sock)
{
int i;
sock.quit = true;
m_connect[sock.m_id] = null;
System.out.println("Disconnect with "+ sock.m_name);
// 通知其他客戶
Integer _id = new Integer(sock.m_id);
String buf =new String();
buf+= _id.toString();
String msg = this.WriteMsg(6,"",buf.toString());
for (i=0;i<MAXCLIENT;i++)
{
if (m_connect[i]!= null )
m_connect[i].Send(msg);
}
m_count --;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -