chaingun
chaingun

Reputation: 21

why C# UDPClient.client.Available alway equals 0?

Why UDPClient.client.Availabe == 0 while the Wireshark can catch the UDP package ? I used the UDPClient to receive UDP package from other endpoint like below:

socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
socket.Bind(new IPEndPoint(IPAddress.Any, 1020));
socket.ReceiveTimeout = 100;
_udpReceiver = new UdpClient
{
    Client = socket
};

while (!_stopReceiveEvent.WaitOne(1))
{`your text`
    if (_udpReceiver.Available <= 0)
    {
        continue;
    }

    byte[] rawBytes = _udpReceiver.Receive(ref endPoint); 
    
    //handle the rawBytes
    //....
}

Most of time it worked normally, but occasionally the _udpReceiver.Avaiable equaled 0 while the Wireshark can catch the UDP package on 1020 port.

Any information? Thanks a lot in advance.

Upvotes: 1

Views: 382

Answers (0)

Related Questions