基本信息
源码名称:socket长连接短连接,自己封装的
源码大小:0.28M
文件格式:.rar
开发语言:C#
更新时间:2021-05-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
Socket通讯,长连接短链接,包分 包头、指令、内容 包头一般2-4个字节 表示包的长度,指令2个字节 表示做什么操作,内容是具体数据
/// <summary>
/// 连接
/// </summary>
public void Con()
{
try
{
var serverInfo = new IPEndPoint((IPAddress.Parse(Ip)), int.Parse(Port));
// 生成一个TCP的socket
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//提供一个IP地址,指示服务器应侦听所有网络接口上的客户端活动
ServerSocket.Bind(serverInfo);//将SOCKET接口和IP端口绑定
ServerSocket.Listen(1000);//开始监听,并且挂起数为1000
_thread = new Thread(BeginReceive) { IsBackground = true };
_thread.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 线程同步接收数据
/// </summary>
private void BeginReceive()
{
while (true)
{
try
{
//开启异步监听socket
Socket handleSocket = ServerSocket.Accept();
Num ;
var stk = new StateObject { WorkSocket = handleSocket, Id = Num };
UserList.Add(Num, stk);
handleSocket.BeginReceive(stk.Buffer, 0, stk.Buffer.Length, 0, new AsyncCallback(RecieveCallBack), stk);
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
}
Socket通讯,长连接短链接,包分 包头、指令、内容 包头一般2-4个字节 表示包的长度,指令2个字节 表示做什么操作,内容是具体数据
/// <summary>
/// 连接
/// </summary>
public void Con()
{
try
{
var serverInfo = new IPEndPoint((IPAddress.Parse(Ip)), int.Parse(Port));
// 生成一个TCP的socket
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//提供一个IP地址,指示服务器应侦听所有网络接口上的客户端活动
ServerSocket.Bind(serverInfo);//将SOCKET接口和IP端口绑定
ServerSocket.Listen(1000);//开始监听,并且挂起数为1000
_thread = new Thread(BeginReceive) { IsBackground = true };
_thread.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 线程同步接收数据
/// </summary>
private void BeginReceive()
{
while (true)
{
try
{
//开启异步监听socket
Socket handleSocket = ServerSocket.Accept();
Num ;
var stk = new StateObject { WorkSocket = handleSocket, Id = Num };
UserList.Add(Num, stk);
handleSocket.BeginReceive(stk.Buffer, 0, stk.Buffer.Length, 0, new AsyncCallback(RecieveCallBack), stk);
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
}