这篇文章主要为大家详细介绍了C#实现简易多人聊天室,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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);
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#实现简易多人聊天室
猜你喜欢
- Unity3D实现渐变颜色效果 2023-01-16
- user32.dll 函数说明小结 2022-12-26
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- Unity Shader实现模糊效果 2023-04-27
- .NET CORE DI 依赖注入 2023-09-27
- c# 模拟线性回归的示例 2023-03-14
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- 如何使用C# 捕获进程输出 2023-03-10
- Oracle中for循环的使用方法 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29