Sending UDP broadcasts

Top  Previous  Next

How to send UDP broadcasts

UDP datagrams can be sent as broadcasts. Broadcast, instead of specifying a particular network host as a destination, targets a group of hosts on the network.

 

note_tip-wt

The sock object supports link-level broadcasts. Such broadcast packets have their destination MAC address set to 255.255.255.255.255.255. Link-level broadcasts are received by all network hosts connected to the current network segment. Link-level broadcasts cannot penetrate routers, bridges, etc.

 

To make the socket send its outgoing UDP datagrams as broadcasts, set the sock.targetbcast property to 1- YES:

 

 

...

sock.targetbcast= YES

sock.setdata("ABC") 'this is explained in 'Working With Buffers' section

sock.send 'this is explained in 'Working With Buffers' section

sock.connect 'broadcast UDP datagram with string 'ABC' will be sent out at this point

...

 

 

There is one difference to grasp regarding the socket that is sending its outgoing packets as broadcasts: no incoming UDP packet that would have normally be interpreted as a re-connect will cause the socket to "switchover" to the source IP-address of the packet.

Let's evaluate two examples.

Example 1: regular UDP communications

Here is a sample setup for the UDP socket:

 

 

sock.protocol= PL_SOCK_PROTOCOL_UDP

sock.inconmode= PL_SOCK_INCONMODE_ANY_IP_ANY_PORT

sock.reconmode= PL_SOCK_RECONMODE_3

sock.targetip= "192.168.100.40"

sock.targetport= 1000

sock.localportlist= "2000"

sock.setdata("ABC")

sock.send

sock.connect

 

 

And here is a hypothetical sequence of events:

 

Incoming/outgoing datagram

Comment

Datagram with contents "ABC" sent to 192.168.100.40:1000 as soon as sock.connect method is invoked

We now have the UDP "connection" with 192.168.100.40:1000

Incoming UDP datagram from 192.168.100.41:20.

This will be taken as a reconnect- the socket is now engaged in a connection with 192.168.100.41:20.

Socket sends out another datagram- this time to 192.168.100.41:20!

Complete switchover happened- the socket is now transmitting data to the IP and port of the sender of the previous incoming datagram

Example 2: regular UDP communications

Here is another setup for the UDP socket:

 

 

sock.protocol= PL_SOCK_PROTOCOL_UDP

sock.inconmode= PL_SOCK_INCONMODE_ANY_IP_ANY_PORT

sock.reconmode= PL_SOCK_RECONMODE_3

sock.targetbcast= YES

sock.targetport=1000

sock.localportlist= "2000"

sock.setdata("ABC")

sock.send

sock.connect

 

 

Sequence of events:

 

Incoming/outgoing datagram

Comment

Datagram with contents "ABC" sent as broadcast to all stations on the network segment

We now have the UDP "connection" with... err... everybody on the segment

Incoming UDP datagram from 192.168.100.41:20.

This is still a reconnect, but the socket won't switch over to the IP-address of the sender. Only port switchover will take place!

Socket sends out another datagram- still as a broadcast but this time to port 20.

Port switchover happened because it is allowed by the sock.reconmode.