12

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..

Mike Pennington
  • 38,579
  • 16
  • 126
  • 167
Roddy
  • 63,052
  • 38
  • 156
  • 264
  • 1
    You could use raw sockets, that will usually allow you to see the same packet multiple times. But you'll need to do some more decoding yourself (depending on the platform UDP or IP+UDP or even ethernet+IP+UDP). – KillianDS Nov 09 '12 at 12:19
  • @KillianDS - Thanks. Hopefully that should be straightforward as I'll just use Raw sockets for unpicking the broadcasts.... – Roddy Nov 09 '12 at 12:33
  • possible duplicate of [Is there a way for multiple processes to share a listening socket?](http://stackoverflow.com/questions/670891/is-there-a-way-for-multiple-processes-to-share-a-listening-socket) –  Nov 09 '12 at 12:40

1 Answers1

12

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.

Community
  • 1
  • 1
Rich Amos
  • 249
  • 2
  • 4
  • Much to my surprise, this seems to be - mostly - working. I thought that SO_REUSEADDR was primarily needed for restarting TCP servers when the old binding was stuck in a timed-wait state. – Roddy Nov 09 '12 at 13:06
  • 2
    Is it guaranteed each listener on the end-point will get a copy of a packet If I have multiple listeners? – PaulK Dec 09 '13 at 15:01
  • UDP is not a guaranteed protocol – Mladen Mihajlovic Jul 22 '14 at 11:55
  • 1
    @PaulK I think the previous answer is confusing "Reliable" as used in the TCP/IP specification with what you are asking. Each listener app at the same computer should get the same copy of the received UDP packet, whether it has errors in it or not. – Jim Jan 31 '15 at 08:02