QUdpSocket

Inheritance diagram of QUdpSocket

Synopsis

Functions

Detailed Description

The PySide.QtNetwork.QUdpSocket class provides a UDP socket.

UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn’t important. PySide.QtNetwork.QUdpSocket is a subclass of PySide.QtNetwork.QAbstractSocket that allows you to send and receive UDP datagrams.

The most common way to use this class is to bind to an address and port using PySide.QtNetwork.QUdpSocket.bind() , then call PySide.QtNetwork.QUdpSocket.writeDatagram() and PySide.QtNetwork.QUdpSocket.readDatagram() to transfer data. If you want to use the standard PySide.QtCore.QIODevice functions PySide.QtCore.QIODevice.read() , PySide.QtCore.QIODevice.readLine() , PySide.QtCore.QIODevice.write() , etc., you must first connect the socket directly to a peer by calling PySide.QtNetwork.QAbstractSocket.connectToHost() .

The socket emits the PySide.QtCore.QIODevice.bytesWritten() signal every time a datagram is written to the network. If you just want to send datagrams, you don’t need to call PySide.QtNetwork.QUdpSocket.bind() .

The PySide.QtCore.QIODevice.readyRead() signal is emitted whenever datagrams arrive. In that case, PySide.QtNetwork.QUdpSocket.hasPendingDatagrams() returns true. Call PySide.QtNetwork.QUdpSocket.pendingDatagramSize() to obtain the size of the first pending datagram, and PySide.QtNetwork.QUdpSocket.readDatagram() to read it.

Note

An incoming datagram should be read when you receive the PySide.QtCore.QIODevice.readyRead() signal, otherwise this signal will not be emitted for the next datagram.

Example:

def initSocket(self):
    udpSocket = QUdpSocket(self)
    udpSocket.bind(QHostAddress.LocalHost, 7755)

    self.connect(udpSocket, SIGNAL('readyRead()'),
                 self, SLOT('readPendingDatagrams()'))

def readPendingDatagrams(self):
    while udpSocket.hasPendingDatagrams():
        datagram = QByteArray()
        datagram.resize(udpSocket.pendingDatagramSize())

        (sender, senderPort) = udpSocket.readDatagram(datagram.data(), datagram.size())

        processTheDatagram(datagram)

With PySide.QtNetwork.QUdpSocket , you can also establish a virtual connection to a UDP server using PySide.QtNetwork.QAbstractSocket.connectToHost() and then use PySide.QtCore.QIODevice.read() and PySide.QtCore.QIODevice.write() to exchange datagrams without specifying the receiver for each datagram.

The Broadcast Sender and Broadcast Receiver examples illustrate how to use PySide.QtNetwork.QUdpSocket in applications.

Symbian Platform Security Requirements

On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, operations will result in a panic.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.

class PySide.QtNetwork.QUdpSocket([parent=None])
Parameters:parentPySide.QtCore.QObject

Creates a PySide.QtNetwork.QUdpSocket object.

parent is passed to the PySide.QtCore.QObject constructor.

PySide.QtNetwork.QUdpSocket.BindFlag

This enum describes the different flags you can pass to modify the behavior of QUdpSocket.bind() .

Note

On Symbian OS bind flags behaviour depends on process capabilties. If process has NetworkControl capability, the bind attempt with ReuseAddressHint will always succeed even if the address and port is already bound by another socket with any flags. If process does not have NetworkControl capability, the bind attempt to address and port already bound by another socket will always fail.

Constant Description
QUdpSocket.ShareAddress Allow other services to bind to the same address and port. This is useful when multiple processes share the load of a single service by listening to the same address and port (e.g., a web server with several pre-forked listeners can greatly improve response time). However, because any service is allowed to rebind, this option is subject to certain security considerations. Note that by combining this option with ReuseAddressHint , you will also allow your service to rebind an existing shared address. On Unix, this is equivalent to the SO_REUSEADDR socket option. On Windows, this option is ignored.
QUdpSocket.DontShareAddress Bind the address and port exclusively, so that no other services are allowed to rebind. By passing this option to QUdpSocket.bind() , you are guaranteed that on successs, your service is the only one that listens to the address and port. No services are allowed to rebind, even if they pass ReuseAddressHint . This option provides more security than ShareAddress , but on certain operating systems, it requires you to run the server with administrator privileges. On Unix and Mac OS X, not sharing is the default behavior for binding an address and port, so this option is ignored. On Windows, this option uses the SO_EXCLUSIVEADDRUSE socket option.
QUdpSocket.ReuseAddressHint Provides a hint to PySide.QtNetwork.QUdpSocket that it should try to rebind the service even if the address and port are already bound by another socket. On Windows, this is equivalent to the SO_REUSEADDR socket option. On Unix, this option is ignored.
QUdpSocket.DefaultForPlatform The default option for the current platform. On Unix and Mac OS X, this is equivalent to ( DontShareAddress + ReuseAddressHint ), and on Windows, its equivalent to ShareAddress .
PySide.QtNetwork.QUdpSocket.bind(port, mode)
Parameters:
  • portPySide.QtCore.quint16
  • modePySide.QtNetwork.QUdpSocket.BindMode
Return type:

PySide.QtCore.bool

PySide.QtNetwork.QUdpSocket.bind([port=0])
Parameters:portPySide.QtCore.quint16
Return type:PySide.QtCore.bool

This is an overloaded function.

Binds to PySide.QtNetwork.QHostAddress :Any on port port .

PySide.QtNetwork.QUdpSocket.bind(address, port, mode)
Parameters:
Return type:

PySide.QtCore.bool

PySide.QtNetwork.QUdpSocket.bind(address, port)
Parameters:
Return type:

PySide.QtCore.bool

Binds this socket to the address address and the port port . When bound, the signal PySide.QtCore.QIODevice.readyRead() is emitted whenever a UDP datagram arrives on the specified address and port. This function is useful to write UDP servers.

On success, the functions returns true and the socket enters BoundState ; otherwise it returns false.

The socket is bound using the DefaultForPlatform BindMode .

PySide.QtNetwork.QUdpSocket.hasPendingDatagrams()
Return type:PySide.QtCore.bool

Returns true if at least one datagram is waiting to be read; otherwise returns false.

PySide.QtNetwork.QUdpSocket.pendingDatagramSize()
Return type:PySide.QtCore.qint64

Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.

PySide.QtNetwork.QUdpSocket.readDatagram(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:(data, address, port)

Receives a datagram no larger than maxSize bytes and stores it in data . The sender’s host address and port is stored in *``address`` and *``port`` (unless the pointers are 0).

Returns the size of the datagram on success; otherwise returns -1.

If maxSize is too small, the rest of the datagram will be lost. To avoid loss of data, call PySide.QtNetwork.QUdpSocket.pendingDatagramSize() to determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded.

PySide.QtNetwork.QUdpSocket.writeDatagram(datagram, host, port)
Parameters:
Return type:

PySide.QtCore.qint64

This is an overloaded function.

Sends the datagram datagram to the host address host and at port port .