连续接收使用udpClient消息 [英] Receive messages continuously using udpClient

查看:424
本文介绍了连续接收使用udpClient消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找获得最佳的解决方案和处理消息通过 UdpClient C#。没有人有这方面的任何解决方案。

I was looking for the best solution to receive and process messages via UdpClient class in C#. Does anyone have any solutions for this?

推荐答案

试试这个代码:

//Client uses as receive udp client
UdpClient Client = new UdpClient(Port);

try
{
     Client.BeginReceive(new AsyncCallback(recv), null);
}
catch(Exception e)
{
     MessageBox.Show(e.ToString());
}

//CallBack
private void recv(IAsyncResult res)
{
    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 8000);
    byte[] received = Client.EndReceive(res, ref RemoteIpEndPoint);

    //Process codes

    MessageBox.Show(Encoding.UTF8.GetString(received));
    Client.BeginReceive(new AsyncCallback(recv), null);
}

这篇关于连续接收使用udpClient消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆