C#Socket通信基础(异步Socket通信UDP)
一、通信原理参考https://blog.csdn.net/xiaochenXIHUA/article/details/83446031。
异步通信解决同步通信的缺点可以双向发送和接收信息;异步通信思路是引入多线程机制,在一个进程中使用两个线程,分别处理接收线程与发送线程,这种机制称为异步Socket。
二、编写异步UDP通信方法
<1>信息发送接收代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test_AsynCommunication
{
class UDPCommunication
{
bool sendingFlag = true;
bool receiveingFlag = true;
IPEndPoint iep = null;
IPEndPoint iep_Receive = null;
Socket socketClient = null;
Socket socketServer = null;
byte[] byteSendingArray = null;
byte[] byteReceiveArray = null;
static void Main(string[] args)
{
UDPCommunication obj = new UDPCommunication();
Console.WriteLine("---异步通信,A---");
obj.OpenSendAndReceiveThread();
}
/// <summary>
/// 分别开启“接收”与“发送”线程
/// </summary>
private void OpenSendAndReceiveThread()
{
Thread thSending = new Thread(SendingData);
Thread thReveive = new Thread(ReceiveData);
thSending.Start();
thReveive.Start();
}
/// <summary>
/// 发送线程的方法
/// </summary>
private void SendingData()
{
Thread.Sleep(500);//休眠0.5秒
if (sendingFlag)
{
byteSendingArray = new byte[100];
//定义网络地址
iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),1002);
socketClient = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
sendingFlag = false;
}
//发送数据
Console.WriteLine("请输入发送的数据:");
EndPoint ep = (EndPoint)iep;
while (true)
{
string strMsg = Console.ReadLine();
//字节转换
byteSendingArray = Encoding.Default.GetBytes(strMsg);
socketClient.SendTo(byteSendingArray,ep);
if (strMsg=="exit")
{
break;
}
}
socketClient.Shutdown(SocketShutdown.Both);
socketClient.Close();
}
/// <summary>
/// 接收线程的方法
/// </summary>
private void ReceiveData()
{
if (receiveingFlag)
{
byteReceiveArray = new byte[100];
iep_Receive = new IPEndPoint(IPAddress.Parse("127.0.0.1"),1001);
socketServer = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
socketServer.Bind(iep_Receive);
receiveingFlag = false;
}
//接受收据
EndPoint ep = (EndPoint)iep_Receive;
while(true)
{
int intReceiveLenght = socketServer.ReceiveFrom(byteReceiveArray,ref ep);
string strReceiveStr = Encoding.Default.GetString(byteReceiveArray,0,intReceiveLenght);
Console.WriteLine(strReceiveStr);
}
}
}//class_end
}
<2>信息发送接收代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test_AsynCommunicationB
{
class UDPCommunication
{
bool sendingFlag = true;
bool receiveingFlag = true;
IPEndPoint iep = null;
IPEndPoint iep_Receive = null;
Socket socketClient = null;
Socket socketServer = null;
byte[] byteSendingArray = null;
byte[] byteReceiveArray = null;
static void Main(string[] args)
{
UDPCommunication obj = new UDPCommunication();
Console.WriteLine("---异步通信,B---");
obj.OpenSendAndReceiveThread();
}
/// <summary>
/// 分别开启“接收”与“发送”线程
/// </summary>
private void OpenSendAndReceiveThread()
{
Thread thSending = new Thread(SendingData);
Thread thReveive = new Thread(ReceiveData);
thSending.Start();
thReveive.Start();
}
/// <summary>
/// 发送线程的方法
/// </summary>
private void SendingData()
{
Thread.Sleep(500);//休眠0.5秒
if (sendingFlag)
{
byteSendingArray = new byte[100];
//定义网络地址
iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1001);
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sendingFlag = false;
}
//发送数据
Console.WriteLine("请输入发送的数据:");
EndPoint ep = (EndPoint)iep;
while (true)
{
string strMsg = Console.ReadLine();
//字节转换
byteSendingArray = Encoding.Default.GetBytes(strMsg);
socketClient.SendTo(byteSendingArray, ep);
if (strMsg == "exit")
{
break;
}
}
socketClient.Shutdown(SocketShutdown.Both);
socketClient.Close();
}
/// <summary>
/// 接收线程的方法
/// </summary>
private void ReceiveData()
{
if (receiveingFlag)
{
byteReceiveArray = new byte[100];
iep_Receive = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1002);
socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketServer.Bind(iep_Receive);
receiveingFlag = false;
}
//接受收据
EndPoint ep = (EndPoint)iep_Receive;
while (true)
{
int intReceiveLenght = socketServer.ReceiveFrom(byteReceiveArray, ref ep);
string strReceiveStr = Encoding.Default.GetString(byteReceiveArray, 0, intReceiveLenght);
Console.WriteLine(strReceiveStr);
}
}
}//class_end
}
三、运行测试
注:本内容来自《Unity3D/2D游戏开发从0到1》28章