110,568
社区成员
发帖
与我相关
我的任务
分享
public void RecieveData()
{
IPAddress locip = IPAddress.Parse(LocalIp);
//IPEndPoint locEndPort = new IPEndPoint(locip, LocalPort);
IPEndPoint locEndPort = new IPEndPoint(locip, SendToPort);
if (RecvClient == null)
{
RecvClient = new UdpClient(locEndPort);
RecvClient.Client.ReceiveTimeout = 60000;
RecvClient.Connect(IPAddress.Parse(SendToIp), SendToPort);
}
Thread RecvThread = new Thread(ReceiveMessage);
RecvThread.Start();
}
private void ReceiveMessage()
{
IPEndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
//RemoteEndPoint = new IPEndPoint(IPAddress.Parse(LocalIp), SendToPort);
while (true)
{
try
{
//关闭UDPRecvClient会引发异常
byte[] receiveBytes = RecvClient.Receive(ref RemoteEndPoint);
string msg = Encoding.Unicode.GetString(receiveBytes);
string ShowMsg = RemoteEndPoint.ToString() + ":" + msg;
//消息容器添加加锁,防止容器信息出错
//lock (lockObj)
//{
// listMsg.Add(ShowMsg);
//}
if (msg == SendMsgs)
{
IsOperateSuccess = true;
}
}
catch
{
break;
}
}
}