扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:王凯明 来源:论坛 2007年11月14日
关键字:
using System; using System.Threading; namespace ChatServer { using System.Net.Sockets; using System.Net; /// /// Client 的摘要说明。 /// public class Client { private Thread clthread; private EndPoint endpoint; private string name; private Socket sock; public Client(string _name, EndPoint _endpoint, Thread _thread, Socket _sock) { // TODO: 在此处添加构造函数逻辑 clthread = _thread; endpoint = _endpoint; name = _name; sock = _sock; } public override string ToString() { return endpoint.ToString()+ " : " + name; } public Thread CLThread { get{return clthread;} set{clthread = value;} } public EndPoint Host { get{return endpoint;} set{endpoint = value;} } public string Name { get{return name;} set{name = value;} } public Socket Sock { get{return sock;} set{sock = value;} } } } |
private void ServiceClient() { Socket client = clientsocket; bool keepalive = true; while (keepalive) { Byte[] buffer = new Byte[1024]; client.Receive(buffer); string clientcommand = System.Text.Encoding.ASCII.GetString(buffer); string[] tokens = clientcommand.Split(new Char[]{'|'}); Console.WriteLine(clientcommand); if (tokens[0] == "CONN") { for(int n=0; n { Client cl = (Client)clients[n]; SendToClient(cl, "JOIN|" + tokens[1]); } EndPoint ep = client.RemoteEndPoint; Client c = new Client(tokens[1], ep, clientservice, client); clients.Add(c); string message = "LIST|" + GetChatterList() +"\r\n"; SendToClient(c, message); lbClients.Items.Add(c); } if (tokens[0] == "CHAT") { for(int n=0; n { Client cl = (Client)clients[n]; SendToClient(cl, clientcommand); } } if (tokens[0] == "PRIV") { string destclient = tokens[3]; for(int n=0; n { Client cl = (Client)clients[n]; if(cl.Name.CompareTo(tokens[3]) == 0) SendToClient(cl, clientcommand); if(cl.Name.CompareTo(tokens[1]) == 0) SendToClient(cl, clientcommand); } } if (tokens[0] == "GONE") { int remove = 0; bool found = false; int c = clients.Count; for(int n=0; n { Client cl = (Client)clients[n]; SendToClient(cl, clientcommand); if(cl.Name.CompareTo(tokens[1]) == 0) { remove = n; found = true; lbClients.Items.Remove(cl); } } if(found) clients.RemoveAt(remove); client.Close(); keepalive = false; } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者