_tcpl.Start();
Console.WriteLine("服务器已启动,正在监听...\n");
Console.WriteLine(string.Format("服务器IP:{0}\t端口号:{1}\n", _ip, _port));
while (true)
{
byte[] packetBuff = new byte[_maxPacket];
Socket newClient = _tcpl.AcceptSocket();
newClient.Receive(packetBuff);
string userName = Encoding.Unicode.GetString(packetBuff).TrimEnd('\0');
//验证是否为唯一用户
if (_transmit_tb.Count != 0 && _transmit_tb.ContainsKey(userName))
{
newClient.Send(Encoding.Unicode.GetBytes("cmd::Failed"));
continue;
}
else
{
newClient.Send(Encoding.Unicode.GetBytes("cmd::Successful"));
}
//将新连接加入转发表并创建线程为其服务
_transmit_tb.Add(userName, newClient);
string svrlog = string.Format("[系统消息]新用户 {0} 在 {1} 已连接... 当前在线人数: {2}\r\n\r\n", userName, DateTime.Now, _transmit_tb.Count);
Console.WriteLine(svrlog);
Thread clientThread = new Thread(new ParameterizedThreadStart(ThreadFunc));
clientThread.Start(userName);
//向所有客户机发送系统消息 C#+Socket即时通信程序设计(9):http://www.751com.cn/jisuanji/lunwen_8502.html