這篇文章主要介紹了c#實現多線程局域網聊天系統的相關代碼,有此方面需求的小伙伴可以參考下。
覺得好有點幫助就頂一下啦。
socke編程,支持多客戶端,多線程操作避免界面卡死。
開啟socket
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 private void button1_Click(object sender, EventArgs e) { try { int port = int.Parse(txt_port.Text); string host = txt_ip.Text; //創建終結點 IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); //創建Socket並開始監聽 newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //創建一個Socket對象,如果用UDP協議,則要用SocketTyype.Dgram類型的套接字 newsock.Bind(ipe); //綁定EndPoint對象 newsock.Listen(0); //開始監聽 //為新建立的連接創建新的Socket acceptClientThread = new Thread(new ThreadStart(AcceptClient)); acceptClientThread.Start(); SetText("開始監聽"); } catch (Exception exp) { CommonFunction.WriteLog(exp, exp.Message); } }監控端口,接收客戶端
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /// <summary> /// 接受客戶端,可接受多個客戶端同時連入,並對連入的客戶端注冊到客戶端列表 /// </summary> public void AcceptClient() { try { while (true) { Socket client = newsock.Accept(); ip = client.Handle; RegeistUser(client.Handle, client); Thread clientThread = new Thread(new ParameterizedThreadStart(ReceiveData)); object o = client; clientThread.Start(o); } } catch (Exception exp) { CommonFunction.WriteLog(exp, exp.Message); } }接收客戶端數據並廣播數據
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /// <summary> /// 接收客戶端數據並,轉發到目標客戶端。 /// </summary> public void ReceiveData(object o) { try { while (true) { Socket client = (Socket)o; string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes; bytes = client.Receive(recvBytes, recvBytes.Length, 0); //從客戶端接受消息 recvStr = Encoding.UTF8.GetString(recvBytes, 0, bytes); SendMessage(client, recvStr); SetText(recvStr); CommonFunction.WriteErrorLog(recvStr); } } catch (Exception exp) { CommonFunction.WriteLog(exp, exp.Message); } }判斷是用戶注冊還是發送消息
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /// <summary> /// 判斷是用戶注冊還是發送消息 /// </summary> /// <param name="p_strMessage"></param> public void SendMessage(Socket client,string p_strMessage) { if (p_strMessage.StartsWith("@")) { RegeistUser(p_strMessage, client); } else if (p_strMessage.StartsWith(">")) { DeleteClident(p_strMessage); } else { //SendMessageToTarget(p_strMessage); SendAllMessage(p_strMessage); } }將socket注冊為指定用戶名
?
1 2 3 4 5 6 7 8 9 10 11 12 /// <summary> /// 將socket注冊為指定用戶名 /// </summary> /// <param name="user"></param> /// <param name="ss"></param> public void RegeistUser(string user, Socket ss) { user = user.Remove(0, 1); userSocketDict.Add(user, ss); SendOneMessage(ss, "歡迎" + user + "連入!"); RefreshClient(); }從客戶端字典中移除客戶端
?
1 2 3 4 5 6 7 8 9 10 /// <summary> /// 從客戶端字典中移除客戶端 /// </summary> /// <param name="p_strMessage"></param> public void DeleteClident(string p_strMessage) { p_strMessage = p_strMessage.Remove(0, 1); userSocketDict.Remove(p_strMessage); RefreshClient(); }群發消息
?
1 2 3 4 5 6 7 8 9 10 11 12 13 /// <summary> /// 群發消息 /// </summary> /// <param name="p_strsend"></param> public void SendAllMessage(string p_strsend) { //MessageBox.Show(p_strsend); foreach (string item in userSocketDict.Keys) { byte[] bs = Encoding.UTF8.GetBytes(p_strsend); userSocketDict[item].Send(bs, bs.