Archived:How to use udp broadcast in PySymbian
All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.
Article Metadata
Introduction
The User Datagram Protocol (UDP) is one of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without requiring prior communications to set up special transmission channels or data paths. UDP is sometimes called the Universal Datagram Protocol.
How to send UDP broadcast
It's possible to use UDP broadcast with PySymbian (e.g. with ad hoc Wi-Fi networks on the N95). However, the operation is slightly different from that of the standard Python libraries. There are two primary issues:
1) You do not need to (and indeed cannot) call socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) to enable broadcast transmit. Also, the address "<broadcast>" will not be recognized. Use an explicit 255.255.255.255 instead.
Example:
#broadcasts "hello" to all machines on the local subnet, on port 8100
outsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
msg = "hello"
port = 8100
outsock.sendto(msg, ("255.255.255.255", 8100))
2) You must bind to the address 0.0.0.0 to receive UDP broadcast packets. This will bind to all available interfaces. This corresponds to calling bind with an empty string in the standard Python socket implementation. Do not bind to the external IP address of the machine!
This example reads a packet from a UDP connection, and will respond to broadcast packets:
# receives a single packet, including broadcast packets
self.insock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
insock.bind(("0.0.0.0",8100))
(msg, addr) = recvfrom(65536)
As a further quick note, it seems that in the pys60 socket implementation, you can use nonblocking sockets by calling socket.setblocking(False). However, this only works for recv() and not for recvfrom().
If you need nonblocking behaviour with recvfrom(), use socket._recv_will_return_data_immediately() (which returns True if packets waiting) to test if there is a packet waiting, then call recvfrom() as required.


I have been looking for a concise procedure for individual networked devices to periodically broadcast its own identity information (IP address, Host Name & brief functionality description). So that other nodes can decide by just listening to this broadcast whether a link exists and if capabilities are compatible. This approach will enable a pure peer-to-peer network, without requiring a proxy type device that provides network status and information clearing service.
I have set my mind on UDP to do this job for quite some time. I was excited to come across this article. The pair of codes mentioned looks short enough to be the building block for the task that I want to accomplish. I am no programmer. But, the Python description sounds simple enough for a person who used to work with BASIC Interpreter. In fact, I was able to get the tutorial exercise examples going right after downloaded and installed a Python Ver. 2.7
However, trying to follow the few line example in this article, I got error message from the first line. Looking through the article, I noticed the terminology "PyS60" mentioned. Can someone explain what this is? I suspect that it is an advanced layer of Python? If so, how could I get such environment set up to experiment the pair of codes in the example?
Thanks a lot in advance.
Abe Chen (2010-11-01, 13:35) Avinta Communications, Inc. WebSite: www.Avinta.com Skype: Abraham.Y.Chen Tel: +1 (408) 942-1485