c#代码    
/// <summary>
/// 开始监听
/// </summary>
private void StartListening()
{ IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
//IPAddress ipAddress = IPAddress.Parse("192.168.0.132");
label1.Text=ipAddress.ToString();
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, listenport); listener = new TcpListener(ipLocalEndPoint);
listener.Start();
while (true) 
{
try
{
Socket s = listener.AcceptSocket();//接收一个套接字
clientsocket = s;//赋值给clientsocket
clientservice = new Thread(new ThreadStart(ServiceClient));//为新进client服务建立线程
clientservice.Start();//线程开始
}
catch(Exception e)//如果出现异常则打控制台打印到屏幕
{
Console.WriteLine(e.ToString() );
}
}
} /// <summary>
/// 给一个客户提供服务
/// </summary>
private void ServiceClient()
{
Socket client = clientsocket;//赋值给client
bool keepalive = true;
bool s;
while (keepalive)
{ Byte[] buffer = new Byte[1024];//一个1024bits的缓存区
try
{
client.Receive(buffer);

catch(SocketException ex) 

//客户端退出 
//MessageBox.Show(ex.ToString());
string leaveName="";
int remove = 0;
bool found = false;
int c = clients.Count;
//从client的ArrayList中查找有没有相符的client
//有的话就作好删掉的准备
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
if(cl.Host==client.RemoteEndPoint)
{
leaveName = cl.Name;
remove = n;
found = true;
lbClients.Items.Remove(cl);//List控件中删除这个client
break;
}
} if(found)
{
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
//MessageBox.Show( "GONE|"+leaveName);
SendToClient(cl, "GONE|"+leaveName);
}

clients.RemoveAt(remove);//从clients的ArrayList里面删掉要当前要退出的client
client.Close();//关闭套接口
keepalive = false;//keepalive=false则这个线程服务完毕
} } string clientcommand = System.Text.Encoding.UTF8.GetString(buffer);//把得到的数据用ASCII的编码形式读出解决中文的显示问题 label1.Text=clientcommand;
label2.Text=clientcommand.Length.ToString();
// //加入安全策略
// if(clientcommand.Substring(0,22) =="<policy-file-request/>")
// {
// String xml = " <cross-domain-policy> <allow-access-from domain='localhost' to-ports='*'/> </cross-domain-policy>\0"  ; 
 ==================
请各位指点一下这个应该怎样写 

}
else
{
string[] tokens = clientcommand.Split(new Char[]{'|'});//以|号划分的命令数据
Console.WriteLine(clientcommand); if (tokens[0] == "CONN")//连接命令消息
{
//给现有的client发送新进一个client的消息
for(int n=0; n<clients.Count; n++) 
{
Client cl = (Client)clients[n];
SendToClient(cl, "JOIN|" + tokens[1]);
}

//新加一个client
EndPoint ep = client.RemoteEndPoint;
Client c = new Client(tokens[1], ep, clientservice, client);
clients.Add(c);

//给每个client发一个当前所有client的列表消息
//string message = "LIST|" + GetChatterList(); //new byte(0)
//byte b = 0; string message = "LIST|"+"asdasd";
//MessageBox.Show(message.Length +"="+message);
SendToClient(c, message); //MessageBox.Show(message); //服务器List控件新加这个client
lbClients.Items.Add(c);

}
else
if (tokens[0] == "CHAT")//聊天命令消息
{
//给每个client发送聊天消息
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
}
}
else
if (tokens[0] == "PRIV") //私聊命令消息
{

string destclient = tokens[2];//目标client
for(int n=0; n<clients.Count; n++) 
{
Client cl = (Client)clients[n];
if(cl.Name.CompareTo(tokens[2]) == 0)//给目标client发聊天消息
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)//给自己发聊天消息
SendToClient(cl, clientcommand);
}
}
else
if (tokens[0] == "GONE")//离开命令消息
{
int remove = 0;
bool found = false;
int c = clients.Count;
//从client的ArrayList中查找有没有相符的client
//有的话就作好删掉的准备
for(int n=0; n<c; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, clientcommand);
if(cl.Name.CompareTo(tokens[1]) == 0)
{
remove = n;
found = true;
lbClients.Items.Remove(cl);//List控件中删除这个client
}
}
if(found)
clients.RemoveAt(remove);//从clients的ArrayList里面删掉要当前要退出的client
client.Close();//关闭套接口
keepalive = false;//keepalive=false则这个线程服务完毕
}
else
{
//MessageBox.Show(clientcommand);
for(int n=0; n<clients.Count; n++)
{
Client cl = (Client)clients[n];
SendToClient(cl, "-");
}
}

// } }