Network programming for PyS60 (XIII)

by Marcelo Barros

In our last post we talked about multicast, a special range of IPs used to send messages for a group of machines. In this case, machines that want to receive message in such group must subscribe to it using socket options. However, it is possible to send messages for all machine in the subnet without any previous subscription using UDP packets and a broadcast address. All machines in the subnet must receive these broadcast messages even when they are not addressed to them. Such messages only will be discarded  in the network or transport layer, after checking the package contents.It is not difficult to understand why this kind of addressing was removed in IPv6, doesn’t it ?

There are two four types of broadcast (first two are the most common):

  • Limited broadcast: A special broadcast where the destination address is 255.255.255.255. It is called limited because it is never forwarded for other networks by routers, just machines in the local subnet will receive this message.
  • Subnet directed broadcast: In this case, the subnet broadcast IP address is used (see post IV and V if you want to learn how to calculate broadcast addresses) to specify the target subnet. Routers may forward this message but they need to know the subnet mask. For instance, the broadcast address for network 172.16.10.0 with mask 255.255.255.0 is 172.16.10.255.
  • Net directed broadcast: The broadcast address is calculated using the default mask for classes A, B or C . So, class A will have broadcast address like <netid>.255.255.255, for instance. Routers will forward these messages but it is possible to disable them.
  • All subnets directed broadcast: Quite similar to net directed broadcast but  grouping all subnets in this approach. For instance, 172.16.255.255 is the all subnets directed brodcast address for network 172.16.10.0.

In the data link layer broadcast messages use the special MAC address FF:FF:FF:FF:FF:FF, regardless the type of broadcast IP address used, as we can see in the next picture where it was used the subnet broadcast address (10.0.0.255 for network 10.0.0.0/24).

broadcast_pc

When programming, you do not need any special care when creating broadcast receivers. However, for transmitting broadcast messages, it is necessary to set the socket option SO_BROADCAST to true, in SOL_SOCKET group.

Using Python 1.9.5 (socket module) it was only possible to send broadcast messages. For receiving, it is necessary to select the access point first but access point functions are only in btsocket and they do not affect socket module. Moreover, btsocket does not have the necessary socket options.

I will present here the PyS60 version for sending broadcast messages and the PC versions for sending and receiving broadcast messages.

# Marcelo Barros de Almeida
# marcelobarrosalmeida@gmail.com
 
import time
import sys
import socket
from appuifw import note, popup_menu
 
BCIP = "255.255.255.255"
PORT = 54321
 
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST, True)
while True:
    n = sock.sendto('hello world',(BCIP,PORT))
    print "Message sent (%d bytes)" % n
    time.sleep(2)
# Marcelo Barros de Almeida
# marcelobarrosalmeida@gmail.com
 
import time
import socket
 
BCIP = "255.255.255.255"
PORT = 54321
 
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST, True)
 
while True:
    n = sock.sendto('hello world',(BCIP,PORT))
    print "Message sent (%d bytes)" % n
    time.sleep(2)
# Marcelo Barros de Almeida
# marcelobarrosalmeida@gmail.com
 
import time
import socket
 
PORT = 54321
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(('0.0.0.0',PORT))
 
while True:
    (data,addr) = sock.recvfrom(1500)
    print "Received ",data,"from",addr

Related posts:

  1. Network programming for PyS60 (XII) Until now we have used only TCP in our examples...
  2. Network programming for PyS60 (IV) Network programming is innocuous if you don’t have at least...
  3. Network programming for PyS60 (V) Before some more practicing, it is time to finish the...
  4. Network programming for PyS60 (XV) As discussed in post III, TCP sockets have flow...
  5. Network programming for PyS60 (XI) We have already talked about IP addressing in the post...

Related posts brought to you by Yet Another Related Posts Plugin.