C#實現的簡易多人聊天室


只有一個群聊的功能

服務端

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace FinalChatRoomClient { public partial class Client : Form { //客戶端負責接收服務端發來的數據消息的線程
        Thread threadClient = null; //創建客戶端套接字,負責連接服務器
        Socket socketClient = null; public Client() { InitializeComponent(); //關閉對文本框跨線程操作的檢查
            TextBox.CheckForIllegalCrossThreadCalls = false; } private void start_Click(object sender, EventArgs e) { //獲得文本框中的IP地址對象
            IPAddress address = IPAddress.Parse(txtIp.Text.Trim()); //創建包含IP和端口的網絡節點對象
            IPEndPoint endPoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim())); //創建客戶端套接字,負責連接服務器
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //客戶端連接到服務器
 socketClient.Connect(endPoint); ShowMsg("客戶端連接服務器成功"); } catch (SocketException ex) { ShowMsg("客戶端連接服務器發生異常:" + ex.Message); } catch (Exception ex) { ShowMsg("客戶端連接服務器發生異常:" + ex.Message); } threadClient = new Thread(ReceiveMsg); threadClient.IsBackground = true; threadClient.Start(); } private void btnSend_Click(object sender, EventArgs e) { string strMsg = txtMsg.Text.Trim(); //將字符串轉成方便網絡傳送的二進制數組
            byte[] arrMsg = Encoding.UTF8.GetBytes(strMsg); byte[] arrMsgSend = new byte[arrMsg.Length + 1]; arrMsgSend[0] = 0;//設置標識位,0代表發送的是文字
            Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length); try { socketClient.Send(arrMsgSend); //清空發送消息文本框中的消息
                this.txtMsg.Text = ""; } catch (SocketException ex) { ShowMsg("客戶端發送消息時發生異常:" + ex.Message); } catch (Exception ex) { ShowMsg("客戶端發送消息時發生異常:" + ex.Message); } } private void ShowMsg(string msg) { txtRecord.AppendText(msg + "\r\n"); } private void ReceiveMsg() { while (true) { //定義一個接收消息用的字節數組緩沖區(2M大小)
                byte[] arrMsgRev = new byte[1024 * 1024 * 2]; //將接收到的數據存入arrMsgRev,並返回真正接收到數據的長度
                int length = -1; try { length = socketClient.Receive(arrMsgRev); } catch (SocketException ex) { ShowMsg("客戶端接收消息時發生異常:" + ex.Message); break; } catch (Exception ex) { MessageBox.Show("客戶端接收消息時發生異常:" + ex.Message); break; } //此時是將數組的所有元素(每個字節)都轉成字符串,而真正接收到只有服務端發來的幾個字符
                string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 0, length); Console.WriteLine(strMsgReceive); ShowMsg(strMsgReceive); } } } }

 

客戶端

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace FinalChatRoomClient { public partial class Client : Form { //客戶端負責接收服務端發來的數據消息的線程
        Thread threadClient = null; //創建客戶端套接字,負責連接服務器
        Socket socketClient = null; public Client() { InitializeComponent(); //關閉對文本框跨線程操作的檢查
            TextBox.CheckForIllegalCrossThreadCalls = false; } private void start_Click(object sender, EventArgs e) { //獲得文本框中的IP地址對象
            IPAddress address = IPAddress.Parse(txtIp.Text.Trim()); //創建包含IP和端口的網絡節點對象
            IPEndPoint endPoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim())); //創建客戶端套接字,負責連接服務器
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //客戶端連接到服務器
 socketClient.Connect(endPoint); ShowMsg("客戶端連接服務器成功"); } catch (SocketException ex) { ShowMsg("客戶端連接服務器發生異常:" + ex.Message); } catch (Exception ex) { ShowMsg("客戶端連接服務器發生異常:" + ex.Message); } threadClient = new Thread(ReceiveMsg); threadClient.IsBackground = true; threadClient.Start(); } private void btnSend_Click(object sender, EventArgs e) { string strMsg = txtMsg.Text.Trim(); //將字符串轉成方便網絡傳送的二進制數組
            byte[] arrMsg = Encoding.UTF8.GetBytes(strMsg); byte[] arrMsgSend = new byte[arrMsg.Length + 1]; arrMsgSend[0] = 0;//設置標識位,0代表發送的是文字
            Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length); try { socketClient.Send(arrMsgSend); //清空發送消息文本框中的消息
                this.txtMsg.Text = ""; } catch (SocketException ex) { ShowMsg("客戶端發送消息時發生異常:" + ex.Message); } catch (Exception ex) { ShowMsg("客戶端發送消息時發生異常:" + ex.Message); } } private void ShowMsg(string msg) { txtRecord.AppendText(msg + "\r\n"); } private void ReceiveMsg() { while (true) { //定義一個接收消息用的字節數組緩沖區(2M大小)
                byte[] arrMsgRev = new byte[1024 * 1024 * 2]; //將接收到的數據存入arrMsgRev,並返回真正接收到數據的長度
                int length = -1; try { length = socketClient.Receive(arrMsgRev); } catch (SocketException ex) { ShowMsg("客戶端接收消息時發生異常:" + ex.Message); break; } catch (Exception ex) { MessageBox.Show("客戶端接收消息時發生異常:" + ex.Message); break; } //此時是將數組的所有元素(每個字節)都轉成字符串,而真正接收到只有服務端發來的幾個字符
                string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 0, length); Console.WriteLine(strMsgReceive); ShowMsg(strMsgReceive); } } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM