相关疑难解决方法(0)

如何在循环中使用UdpClient.BeginReceive

我想做这个

for (int i = 0; i < 100; i++ )
{
    Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
}
Run Code Online (Sandbox Code Playgroud)

UdpClient.Receive我不得不使用,而不是使用UdpClient.BeginReceive.问题是,我该怎么做?没有很多样本使用BeginReceive,MSDN示例根本没有帮助.我应该使用BeginReceive,还是只在一个单独的线程下创建它?

我一直都是ObjectDisposedException例外.我只收到第一个发送的数据.下一个数据将抛出异常.

public class UdpReceiver
{
    private UdpClient _client;
    public System.Net.Sockets.UdpClient Client
    {
        get { return _client; }
        set { _client = value; }
    }
    private IPEndPoint _endPoint;
    public System.Net.IPEndPoint EndPoint
    {
        get { return _endPoint; }
        set { _endPoint = value; }
    }
    private int _packetCount;
    public int PacketCount
    {
        get …
Run Code Online (Sandbox Code Playgroud)

c# udpclient beginreceive

8
推荐指数
2
解决办法
2万
查看次数

等待UDPClient.ReceiveAsync超时

我正在使用如下的UDPClient

dim c = New UDPClient(port)
client.CLient.ReceiveTimeout = 1
await client.ReceiveAsync()
Run Code Online (Sandbox Code Playgroud)

但是,即使我设置了超时,await也不会终止或抛出.这是正常的行为吗?

.net timeout udpclient async-await

4
推荐指数
2
解决办法
5919
查看次数

标签 统计

udpclient ×2

.net ×1

async-await ×1

beginreceive ×1

c# ×1

timeout ×1