




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第C#实现简易多人聊天室本文实例为大家分享了C#实现简易多人聊天室的具体代码,供大家参考,具体内容如下
只有一个群聊的功能
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceFinalChatRoomClient
publicpartialclassClient:Form
{
//客户端负责接收服务端发来的数据消息的线程
ThreadthreadClient=null;
//创建客户端套接字,负责连接服务器
SocketsocketClient=null;
publicClient()
{
InitializeComponent();
//关闭对文本框跨线程操作的检查
TextBox.CheckForIllegalCrossThreadCalls=false;
}
privatevoidstart_Click(objectsender,EventArgse)
{
//获得文本框中的IP地址对象
IPAddressaddress=IPAddress.Parse(txtIp.Text.Trim());
//创建包含IP和端口的网络节点对象
IPEndPointendPoint=newIPEndPoint(address,int.Parse(txtPort.Text.Trim()));
//创建客户端套接字,负责连接服务器
socketClient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
//客户端连接到服务器
socketClient.Connect(endPoint);
ShowMsg("客户端连接服务器成功");
}
catch(SocketExceptionex)
{
ShowMsg("客户端连接服务器发生异常:"+ex.Message);
}
catch(Exceptionex)
{
ShowMsg("客户端连接服务器发生异常:"+ex.Message);
}
threadClient=newThread(ReceiveMsg);
threadClient.IsBackground=true;
threadClient.Start();
}
privatevoidbtnSend_Click(objectsender,EventArgse)
{
stringstrMsg=txtMsg.Text.Trim();
//将字符串转成方便网络传送的二进制数组
byte[]arrMsg=Encoding.UTF8.GetBytes(strMsg);
byte[]arrMsgSend=newbyte[arrMsg.Length+1];
arrMsgSend[0]=0;//设置标识位,0代表发送的是文字
Buffer.BlockCopy(arrMsg,0,arrMsgSend,1,arrMsg.Length);
try
{
socketClient.Send(arrMsgSend);
//清空发送消息文本框中的消息
this.txtMsg.Text="";
}
catch(SocketExceptionex)
{
ShowMsg("客户端发送消息时发生异常:"+ex.Message);
}
catch(Exceptionex)
{
ShowMsg("客户端发送消息时发生异常:"+ex.Message);
}
}
privatevoidShowMsg(stringmsg)
{
txtRecord.AppendText(msg+"\r\n");
}
privatevoidReceiveMsg()
{
while(true)
{
//定义一个接收消息用的字节数组缓冲区(2M大小)
byte[]arrMsgRev=newbyte[1024*1024*2];
//将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度
intlength=-1;
try
{
length=socketClient.Receive(arrMsgRev);
}
catch(SocketExceptionex)
{
ShowMsg("客户端接收消息时发生异常:"+ex.Message);
break;
}
catch(Exceptionex)
{
MessageBox.Show("客户端接收消息时发生异常:"+ex.Message);
break;
}
//此时是将数组的所有元素(每个字节)都转成字符串,而真正接收到只有服务端发来的几个字符
stringstrMsgReceive=Encoding.UTF8.GetString(arrMsgRev,0,length);
Console.WriteLine(strMsgReceive);
ShowMsg(strMsgReceive);
}
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceFinalChatRoomClient
publicpartialclassClient:Form
{
//客户端负责接收服务端发来的数据消息的线程
ThreadthreadClient=null;
//创建客户端套接字,负责连接服务器
SocketsocketClient=null;
publicClient()
{
InitializeComponent();
//关闭对文本框跨线程操作的检查
TextBox.CheckForIllegalCrossThreadCalls=false;
}
privatevoidstart_Click(objectsender,EventArgse)
{
//获得文本框中的IP地址对象
IPAddressaddress=IPAddress.Parse(txtIp.Text.Trim());
//创建包含IP和端口的网络节点对象
IPEndPointendPoint=newIPEndPoint(address,int.Parse(txtPort.Text.Trim()));
//创建客户端套接字,负责连接服务器
socketClient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
//客户端连接到服务器
socketClient.Connect(endPoint);
ShowMsg("客户端连接服务器成功");
}
catch(SocketExceptionex)
{
ShowMsg("客户端连接服务器发生异常:"+ex.Message);
}
catch(Exceptionex)
{
ShowMsg("客户端连接服务器发生异常:"+ex.Message);
}
threadClient=newThread(ReceiveMsg);
threadClient.IsBackground=true;
threadClient.Start();
}
privatevoidbtnSend_Click(objectsender,EventArgse)
{
stringstrMsg=txtMsg.Text.Trim();
//将字符串转成方便网络传送的二进制数组
byte[]arrMsg=Encoding.UTF8.GetBytes(strMsg);
byte[]arrMsgSend=newbyte[arrMsg.Length+1];
arrMsgSend[0]=0;//设置标识位,0代表发送的是文字
Buffer.BlockCopy(arrMsg,0,arrMsgSend,1,arrMsg.Length);
try
{
socketClient.Send(arrMsgSend);
//清空发送消息文本框中的消息
this.txtMsg.Text="";
}
catch(SocketExceptionex)
{
ShowMsg("客户端发送消息时发生异常:"+ex.Message);
}
catch(Exceptionex)
{
ShowMsg("客户端发送消息时发生异常:"+ex.Message);
}
}
privatevoidShowMsg(stringmsg)
{
txtRecord.AppendText(msg+"\r\n");
}
privatevoidReceiveMsg()
{
while(true)
{
//定义一个接收消息用的字节数组缓冲区(2M大小)
byte[]arrMsgRev=newbyte[1024*1024*2];
//将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度
intlength=-1;
try
{
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业行业种植技术试题
- 生产关系与经济发展试题及答案
- 酒店客房服务流程与标准手册
- 电力工程电气设备安装调试练习题
- 货物运输服务合同协议内容
- 公共关系在社会责任方面的表现试题及答案
- 深入探讨公共关系学考试试题及答案
- 钓鱼赛道测试题及答案
- 公益活动的安保人员配置计划
- 2025年现代项目管理方法论试题及答案
- 教师专业发展第6章 教师教育对教师专业发展的全程规划
- 甘肃旅游旅行
- 2024哈尔滨幼儿师范高等专科学校教师招聘考试笔试试题
- 中华人民共和国:各省份对应的地级市与县级市一览表
- 孕期艾梅乙实验室检测培训
- 毒蛇、毒虫咬伤的急诊救治
- MOOC 人力资源管理-暨南大学 中国大学慕课答案
- 事业单位工作人员调动申报表
- 《审计实务》第6讲 函证程序(下)
- 神经病学题库及神经病学试题题库
- 糖尿病酮症完整版本
评论
0/150
提交评论