Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can UDP broadcasts be received by multiple apps on the same computer?

As an example, suppose I have a 'smart' thermometer that broadcasts the current temperature as a UDP datagram every N seconds.

Now, I can write a client that listens for those messages and displays them graphically, and I can have that client running on multiple computers simultaneously. No problem so far.

But, when I try to run two instances of the client on the same Windows computer, I get errors about attempting to "bind to a port already in use".

Is this:-

  • A: Just the way it is with UDP broadcasts, on all operating systems?
  • B: a limitation of the Windows networking stack?
  • C: or, probably a bug in the way I'm reading the datagrams?

If A or B, is there any way round it.

If C, then I'll post some code..

like image 701
Roddy Avatar asked Nov 09 '12 12:11

Roddy


1 Answers

On Windows you can have multiple processes bind to the same socket by using the

SocketOptionName.ReuseAddress

option (see this answer Is there a way for multiple processes to share a listening socket?). Broadcasting a packet should force Windows to give a copy of that packet to each listener on that end-point.

In answer to Roddy, yes, SO_REUSEADDR works this way on *nix too.

For in-depth answer, refer to https://stackoverflow.com/a/14388707/705086.

like image 74
Rich Amos Avatar answered Oct 26 '22 11:10

Rich Amos