×
Namespaces

Variants
Actions
(Redirected from How to use udp broadcast)

Archived:How to use udp broadcast in PySymbian

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

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.

Reference link

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