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)
edit
To elaborate:
We communicate with external hardware via UDP. When it receives comms from us, it replies to the source address - so we receive messages back in on the same port. This part is working fine.
However, if I try to emulate the external hardware using loopback (i.e. I send and receive through the same port "to myself"), I can only receive datagrams if I begin receiving before I begin transmitting. THat works fine - but if I transmit and then try to receive, I never receive any data. What I actually try to send is irrelevant in this case.
So I have two problems:
1) How to manage loopback that works reliably.
2) How to do (1) without breaking the external comms that is currently working fine!
As I've tried all kinds of combinations of 1 or 2 UdpClients and a multitude of different settings (to no avail), I was just wondering if anyone has managed to get UPD loopback working well, as that may give me a lead to a solution that I can get working in all cases.
Thanks for taking the time to think about this...