Question

I have UDP network traffic arriving on my machine (OSC traffic from an iPad, to be exact) and I want two programs to be able to receive that traffic. The problem is that I can't bind to the same network port with two programs at once and I can't send to multiple ports with the iOS app I'm using. How can I solve this problem?

Was it helpful?

Solution

You can use the power of the command line for this. The following snippet uses socat (probably needs to be installed beforehand) and tee (should be preinstalled on any OS X or Linux).

socat -u UDP4-RECVFROM:8123,fork - | tee >(socat -u - UDP4-SENDTO:localhost:8223) | socat -u - UDP4-SENDTO:localhost:8323

Explanation: socat listens for traffic on UDP port 8123, pipes it to tee, which pipes it to two other instances of socat forwarding it to ports 8223 and 8323 on localhost respectively. With your two programs you need to listen to those ports on localhost.

OTHER TIPS

While the answer with using socat is elegant it is not clear for me, what you are trying to do:

  • both programs should receive all parts of the traffic and they will only receive and not reply. This can be done with the proposed socat way
  • both program should receive all parts of the traffic and there reply will be mixed together (how?)
  • each of the programs should only receive parts of the traffic, e.g. the one which the other did not get. This should be possible if both of your programs use SO_REUSEADDR, SO_REUSEPORT. Replies will then be mixed together.
  • or do you actually want to communicate with each of the programs seperatly - then you would have to use either multiple sockets in the iOS app (which you don't want to do) or built your own protocol which does multiplexing, e.g. each message is prefixed with there target app and on the target machine a demultiplexer application will receive all packets and forward them to the appropriate application and wrap the replies back in the multiplexing protocol.

In summary: please describe the problem your are trying to solve, not only one small technical detail of it.

The problem is that I can't bind to the same network port with two programs at once

Yes you can. Just set SO_REUSEADDR and maybe SO_REUSEPORT on both of them before you bind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top