浅析C#中的套接字编程(5) | |||
private void ReceiveChat() { bool keepalive = true; while (keepalive) { try { Byte[] buffer = new Byte[2048]; ns.Read(buffer,0,buffer.Length); string chatter = System.Text.Encoding.ASCII.GetString(buffer); string[] tokens = chatter.Split(new Char[]{'|'}); if (tokens[0] == "CHAT") { rtbChatIn.AppendText(tokens[1]); if(logging) logwriter.WriteLine(tokens[1]); } if (tokens[0] == "PRIV") { rtbChatIn.AppendText("Private from "); rtbChatIn.AppendText(tokens[1].Trim() ); rtbChatIn.AppendText(tokens[2] + "/r/n"); if(logging) { logwriter.Write("Private from "); logwriter.Write(tokens[1].Trim() ); logwriter.WriteLine(tokens[2] + "/r/n"); } } if (tokens[0] == "JOIN") { rtbChatIn.AppendText(tokens[1].Trim() ); rtbChatIn.AppendText(" has joined the Chat/r/n"); if(logging) { logwriter.WriteLine(tokens[1]+" has joined the Chat"); } string newguy = tokens[1].Trim(new char[]{'/r','/n'}); lbChatters.Items.Add(newguy); } if (tokens[0] == "GONE") { rtbChatIn.AppendText(tokens[1].Trim() ); rtbChatIn.AppendText(" has left the Chat/r/n"); if(logging) { logwriter.WriteLine(tokens[1]+" has left the Chat"); } lbChatters.Items.Remove(tokens[1].Trim(new char[]{'/r','/n'})); } if (tokens[0] == "QUIT") { ns.Close(); clientsocket.Close(); keepalive = false; statusBar1.Text = "服务器端已停止"; connected= false; btnSend.Enabled = false; btnDisconnect.Enabled = false; } } catch(Exception){} } } |
浅析C#中的套接字编程(5)
最新推荐文章于 2024-07-24 13:29:35 发布