How to receive UDP packets from any ip and any port?

后端 未结 3 1786
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 03:32

I wanted to use C#\'s UdpClient to listen to any incomming UDP packets. I want to receive packets from any IP and any port.

I tried the following:

Ud         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 04:11

    Your best idea would be to identify specific ports you would like to listen to, and start listening on those. Depending on what is done with received datagrams, it might be best/simplest to create a new Thread for each port you are listening on, and process it there, or enqueue it on a synchonrised (with lock) queue or list, for processing on a central thread.

    You should limit the ports though; it would not be possible to listen to them all.

    That said you could use something like Wireshark or the Winpcap SDK/API to 'sniff' UDP packets right from the network adapter. I have had it working within a .NET application before without too much difficulty.

    Hope that helps.

提交回复
热议问题