C#实现简易多人聊天室
作者:docyard 时间:2023-02-06 06:09:47
本文实例为大家分享了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);
}
}
}
}
来源:https://www.cnblogs.com/wangkaipeng/p/7015930.html
标签:C#,聊天室
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot中使用redis并且执行调试lua脚本
2022-02-15 08:49:52
![](https://img.aspxhome.com/file/2023/1/61031_0s.png)
jar包运行时提示jar中没有主清单属性的解决
2023-11-23 19:04:10
![](https://img.aspxhome.com/file/2023/2/59532_0s.png)
基于Spring Boot应用ApplicationEvent案例场景
2023-08-17 22:38:12
![](https://img.aspxhome.com/file/2023/0/96120_0s.png)
详解Jackson 使用以及性能介绍
2023-02-21 00:08:31
![](https://img.aspxhome.com/file/2023/0/68570_0s.png)
ChatGPT介绍及Java API调用
2021-07-06 07:53:55
java中response对象用法实例分析
2023-08-23 18:51:04
springboot 整合 sa-token简介及入门教程
2023-03-24 01:10:45
![](https://img.aspxhome.com/file/2023/7/83457_0s.png)
java8 实现提取集合对象的每个属性
2023-10-17 19:37:27
java连接zookeeper实现zookeeper教程
2022-09-19 03:04:35
![](https://img.aspxhome.com/file/2023/7/77127_0s.png)
WPF Slider滑动条的颜色修改方法
2022-06-15 20:49:38
![](https://img.aspxhome.com/file/2023/7/81147_0s.png)
CentOS安装jdk的三种方法
2022-01-13 06:24:41
Java函数式编程(六):Optional
2021-07-04 21:44:05
Android输入框控件ClearEditText实现清除功能
2022-12-16 06:59:48
![](https://img.aspxhome.com/file/2023/1/137311_0s.jpg)
springboot使用JPA时间类型进行模糊查询的方法
2023-10-26 21:59:02
![](https://img.aspxhome.com/file/2023/5/81295_0s.png)
详解C语言实现猜数字游戏
2023-11-03 04:58:51
![](https://img.aspxhome.com/file/2023/7/105177_0s.jpg)
IntelliJ IDEA 15款超级牛逼插件推荐(自用,超级牛逼)
2023-10-10 05:51:22
![](https://img.aspxhome.com/file/2023/6/71166_0s.png)
C#中abstract的用法详解
2021-07-08 21:53:46
Android实现计步传感器功能
2021-09-29 21:54:41
![](https://img.aspxhome.com/file/2023/3/137913_0s.jpg)
基于Tomcat7、Java、WebSocket的服务器推送聊天室实例
2023-11-25 23:35:34
![](https://img.aspxhome.com/file/2023/0/60390_0s.jpg)
解决mybatis三表连接查询数据重复的问题
2023-09-13 15:24:25
![](https://img.aspxhome.com/file/2023/2/105912_0s.jpg)