Bidirectional/Loopback UDP in .net

Posted by Jason Williams on Stack Overflow See other posts from Stack Overflow or by Jason Williams
Published on 2010-05-17T11:37:37Z Indexed on 2010/05/17 11:40 UTC
Read the original article Hit count: 302

Filed under:
|
|

I've got an app that needs to transmit and receive on the same port. This can happen in two cases:

  • Where the PC is talking to a piece of remote hardware. It "replies to sender", so the datagrams come back in to my PC via the sending port.

  • Where the PC is talking to itself (loopback mode) for testing and demoing (a test app feeds fake data into our main app via UDP).

This only seems to fail when trying to achieve loopback. The only way I can get it working is to ensure that the receiver is set up first - something I cannot guarantee.

Can anyone help narrow down my search by suggesting a "correct" way to implement the UdpClient(s) to handle the above situations reliably?

(The only solution that I've found to work reliably with the remote hardware is to use a single UdpClient in a bidirectional manner, although I'm working with legacy code that may be influencing that finding. I've tried using two UdpClients, but they step on each others toes - In some cases, once one client is started up, the other client cannot connect. With ExclusiveAddressUse/ReuseAddress set up to allow port sharing, I can almost get it to work, apart from the receiver having to start first)

© Stack Overflow or respective owner

Related posts about c#

Related posts about udp