Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:How to use udp broadcast in PySymbian

From Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

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
Created: johnhw (18 Jul 2007)
Last edited: hamishwillee (31 May 2013)


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 instead.


#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, ("", 8100))

2) You must bind to the address 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)
(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.

Reference link

This page was last modified on 31 May 2013, at 01:09.
35 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.