时间:2022-12-28 09:39:30 | 栏目:.NET代码 | 点击:次
本文实例为大家分享了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); } } } }