Ethernet broadcast

Would anyone happen to have a snippet of code that sets up a UDP socket for
broadcasting and broadcasts a message?

Frank Kolnick <fkolnick@home.com> wrote:

Would anyone happen to have a snippet of code that sets up a UDP socket for
broadcasting and broadcasts a message?

Check out the following site for sample network programming source code:
http://www.kohala.com/start/unpv12e.html

Barry

Thank you. I have the book, and it’s slowly starting to make sense :slight_smile:

“Operating System for Tech Supp” <os@qnx.com> wrote in message
news:9pf2ao$5jv$1@nntp.qnx.com

Frank Kolnick <> fkolnick@home.com> > wrote:
Would anyone happen to have a snippet of code that sets up a UDP socket
for
broadcasting and broadcasts a message?

Check out the following site for sample network programming source code:
http://www.kohala.com/start/unpv12e.html

Barry

As it turns out, the code I have works fine with an older version of Socket
but fails {“host unreachable”} with Socket 4.25. The code is pretty simple:
open a socket with SO_BROADCAST and then call sendto() with a host address
structure filled-in with INADDR_BROADCAST.

Can anyone shed any light on why this works with Socket 4.20 but not 4.25?


“Frank Kolnick” <fkolnick@home.com> wrote in message
news:9pf5qt$aq0$1@inn.qnx.com

Thank you. I have the book, and it’s slowly starting to make sense > :slight_smile:

“Operating System for Tech Supp” <> os@qnx.com> > wrote in message
news:9pf2ao$5jv$> 1@nntp.qnx.com> …
Frank Kolnick <> fkolnick@home.com> > wrote:
Would anyone happen to have a snippet of code that sets up a UDP
socket
for
broadcasting and broadcasts a message?

Check out the following site for sample network programming source code:
http://www.kohala.com/start/unpv12e.html

Barry

I don’t have the code in front of me, but you should be able to create the
socket of type SOCK_DGRAM, use INADDR_BROADCAST, make sure you set a port
number to broadcast to (use htons!) and use setsockopt to enable broadcast
ability (like you said SO_BROADCAST). That should work in 4.25, I’ve done
it I’m pretty sure.


“Frank Kolnick” <fkolnick@home.com> wrote in message
news:9pg82r$t7v$1@inn.qnx.com

As it turns out, the code I have works fine with an older version of
Socket
but fails {“host unreachable”} with Socket 4.25. The code is pretty
simple:
open a socket with SO_BROADCAST and then call sendto() with a host address
structure filled-in with INADDR_BROADCAST.

Can anyone shed any light on why this works with Socket 4.20 but not 4.25?


“Frank Kolnick” <> fkolnick@home.com> > wrote in message
news:9pf5qt$aq0$> 1@inn.qnx.com> …
Thank you. I have the book, and it’s slowly starting to make sense > :slight_smile:

“Operating System for Tech Supp” <> os@qnx.com> > wrote in message
news:9pf2ao$5jv$> 1@nntp.qnx.com> …
Frank Kolnick <> fkolnick@home.com> > wrote:
Would anyone happen to have a snippet of code that sets up a UDP
socket
for
broadcasting and broadcasts a message?

Check out the following site for sample network programming source
code:
http://www.kohala.com/start/unpv12e.html

Barry
\

Thanks. That pretty much sums up what I’m doing. The error I get is “host
unreachable”, suggesting that there’s something wrong with my ‘host’
argument to sendto(). As I mentioned, the same code works fine with an
earlier version of Socket. Interestingly, if I replace INADDR_BROADCAST with
the explicit broadcast address (i.e., the combination of IP address and
netmask), then the broadcast works. This seems to imply that Socket isn’t
recognizing INADDR_BROADCAST and translating into the appropriate broadcast
address(?!)

“R B Adler” <request@qnx.com> wrote in message
news:9pgepl$3mb$1@inn.qnx.com

I don’t have the code in front of me, but you should be able to create the
socket of type SOCK_DGRAM, use INADDR_BROADCAST, make sure you set a port
number to broadcast to (use htons!) and use setsockopt to enable broadcast
ability (like you said SO_BROADCAST). That should work in 4.25, I’ve done
it I’m pretty sure.

Frank Kolnick <fkolnick@home.com> wrote:

Thanks. That pretty much sums up what I’m doing. The error I get is “host
unreachable”, suggesting that there’s something wrong with my ‘host’
argument to sendto(). As I mentioned, the same code works fine with an
earlier version of Socket. Interestingly, if I replace INADDR_BROADCAST with
the explicit broadcast address (i.e., the combination of IP address and
netmask), then the broadcast works. This seems to imply that Socket isn’t
recognizing INADDR_BROADCAST and translating into the appropriate broadcast
address(?!)

This will soon be part of the knowledge base, but I’ll post it here since
might help. It’s a guide to how broadcasting works, and hopefully explains
the behaviour you’re seeing.


-Adam


TCPIP 5.0 or earlier unless noted. RTP 6.1.0 or earlier unless noted.

When attempting to broadcast a UDP packet using the limited broadcast
address, the following happens:

The TCP/IP stack will attempt to resolve the limited broadcast address to
the broadcast address of the primary interface. eg 255.255.255.255 → 10.255.255.255
if the primary interface is on network 10.0.0.0 netmask 255.0.0.0

The primary interface is the first interface configured. In TCPRT 4.25 and later
versions, the primary interface is localhost since it is configured
automatically.

The stack will then check to see if the primary interface supports
broadcasting (interface flags “ifconfig -a”). If it does not, it will attempt to
transmit the broadcast with the original limited broadcast address.

This will result in a “no route to host” error since there is no route to that
network unless the default route exists, which would not be the
correct ethernet broadcast result.

There are two ways to get around this and specifically determine what
interface the packet is transmitted on.

You can reconfigure localhost, which will move localhost to the end of
the interface list making the next interface the primary interface (assuming
the next interface supports broadcasting). This will result in the packet
being transmitted with the destination address of the subnet broadcast
address.

or define the route

route add -interface 255.255.255.0 x.x.x.x

where x.x.x.x is the ip address of the interface you wish to transmit out
of. (This assumes the primary interface does not support broadcasting). This
will result in the packet being transmitted with the limited broadcast address
as the destination address.

When RTP 6.1.1 is released, the broadcast behavior may change so that
the interface list is searched until the first interface which supports
broadcasting is found, and the subnet broadcast address will be used.

When TCPIP 5.0 (QNX4) and RTP 6 with the full BSD stack was introduced,
it included support for SO_BINDTODEVICE. Using this socket option combined
with bypassing the routing table with either the socket option SO_DONTROUTE, or
more preferably using the flags option in send() or sendto() (MSG_DONTROUTE)
you can direct the broadcast packets to a specific interface in your program.
This will result in the packets being transmitted with the destination
ipadress set to the limited broadcast address (255.255.255.255) which
the application specified as the destination address.