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).
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:
- Network programming for PyS60 (XII) Until now we have used only TCP in our examples...
- Network programming for PyS60 (IV) Network programming is innocuous if you don't have at least...
- Network programming for PyS60 (V) Before some more practicing, it is time to finish the...
- Network programming for PyS60 (XV) As discussed in post III, TCP sockets have flow...
- 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.
Hi!
First of all, this is a great series of posts!
I would like to create a subnet directed broadcast on an arbitrary LAN.
I noticed in the past that some local network devices block the generic broadcast address (255.255.255.255), but not the subnet broadcast.
Is there any way to get the subnet mask for an access point through pys60 in order to calculate the broadcast address for whatever network the phone is connected to?
I looked through the network programming posts and could not find it – only hard-coding the bcast address for the manually defined ad-hoc network.
Thanks!
Nadav
Hi Nadav,
You are right, subnet broadcast is better supported by network devices (it is allowed in general). However, it is not possible to retrieve the network mask using access point functions. I have heard about a network extension, I am looking for it in forum nokia. If I find it, I will post it here.
Hi Marcelo,
Thanks for the response. That would be great if you could find it. It would be very useful for me and I hope that for others as well.
The closest thing I was able to find are some comments in a native Symbian code: http://qt.gitorious.com/+qt-s60-developers/qt/qt-s60/commit/1401d0a8b232278f5f07c9c34ed537885e7f17e2
In there it is mentioned how netmask could somehow be retrieved from routing information, but I have no idea if there is any way to gain access to this data through PyS60.
I could not find. The module I was thinking about didn’t have masks … just WiFi parameters. We need to check if it is possible using symbian c++ and create a Pys60 module for it.
Very bad news:
Unable to retrieve netmask, broadcast and gateway address
http://wiki.forum.nokia.com/index.php/Unable_to_retrieve_netmask,_broadcast_and_gateway_address
I think that the code link from QT found a workaround for that by using the available routing information:
“For some reason interface info on Symbian will not give neither correct netmask nor broadcast address.
But, experiments show that route info can be used to obtain netmask. Then is easy to calculate broadcast address. dea is to match interface and destination address from routeinfo entry, and then to use given netmask.”
I think that the c version works (its part of the QT package), but I have no idea how to wrap it up and create a python interface to it..
http://qt.gitorious.com/+qt-s60-developers/qt/qt-s60/commit/1401d0a8b232278f5f07c9c34ed537885e7f17e2