QUdpSocket¶
The
QUdpSocket
class provides a UDP socket. More…
Synopsis¶
Functions¶
def
hasPendingDatagrams
()def
joinMulticastGroup
(groupAddress)def
joinMulticastGroup
(groupAddress, iface)def
leaveMulticastGroup
(groupAddress)def
leaveMulticastGroup
(groupAddress, iface)def
multicastInterface
()def
pendingDatagramSize
()def
readDatagram
(, maxlen)def
receiveDatagram
([maxSize=-1])def
setMulticastInterface
(iface)def
writeDatagram
(datagram)def
writeDatagram
(datagram, host, port)
Detailed Description¶
UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn’t important.
QUdpSocket
is a subclass ofQAbstractSocket
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
bind()
, then callwriteDatagram()
andreadDatagram()
/receiveDatagram()
to transfer data. If you want to use the standardQIODevice
functionsread()
,readLine()
,write()
, etc., you must first connect the socket directly to a peer by callingconnectToHost()
.The socket emits the
bytesWritten()
signal every time a datagram is written to the network. If you just want to send datagrams, you don’t need to callbind()
.The
readyRead()
signal is emitted whenever datagrams arrive. In that case,hasPendingDatagrams()
returnstrue
. CallpendingDatagramSize()
to obtain the size of the first pending datagram, andreadDatagram()
orreceiveDatagram()
to read it.Note
An incoming datagram should be read when you receive the
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)
QUdpSocket
also supports UDP multicast. UsejoinMulticastGroup()
andleaveMulticastGroup()
to control group membership, andMulticastTtlOption
andMulticastLoopbackOption
to set the TTL and loopback socket options. UsesetMulticastInterface()
to control the outgoing interface for multicast datagrams, andmulticastInterface()
to query it.With
QUdpSocket
, you can also establish a virtual connection to a UDP server usingconnectToHost()
and then useread()
andwrite()
to exchange datagrams without specifying the receiver for each datagram.The Broadcast Sender , Broadcast Receiver , Multicast Sender , and Multicast Receiver examples illustrate how to use
QUdpSocket
in applications.See also
- class PySide2.QtNetwork.QUdpSocket([parent=None])¶
- param parent:
Creates a
QUdpSocket
object.parent
is passed to theQObject
constructor.See also
- PySide2.QtNetwork.QUdpSocket.hasPendingDatagrams()¶
- Return type:
bool
Returns
true
if at least one datagram is waiting to be read; otherwise returnsfalse
.See also
- PySide2.QtNetwork.QUdpSocket.joinMulticastGroup(groupAddress)¶
- Parameters:
groupAddress –
PySide2.QtNetwork.QHostAddress
- Return type:
bool
Joins the multicast group specified by
groupAddress
on the default interface chosen by the operating system. The socket must be inBoundState
, otherwise an error occurs.Note that if you are attempting to join an IPv4 group, your socket must not be bound using IPv6 (or in dual mode, using
Any
). You must useAnyIPv4
instead.This function returns
true
if successful; otherwise it returnsfalse
and sets the socket error accordingly.Note
Joining IPv6 multicast groups without an interface selection is not supported in all operating systems. Consider using the overload where the interface is specified.
See also
- PySide2.QtNetwork.QUdpSocket.joinMulticastGroup(groupAddress, iface)
- Parameters:
groupAddress –
PySide2.QtNetwork.QHostAddress
- Return type:
bool
This is an overloaded function.
Joins the multicast group address
groupAddress
on the interfaceiface
.See also
- PySide2.QtNetwork.QUdpSocket.leaveMulticastGroup(groupAddress)¶
- Parameters:
groupAddress –
PySide2.QtNetwork.QHostAddress
- Return type:
bool
Leaves the multicast group specified by
groupAddress
on the default interface chosen by the operating system. The socket must be inBoundState
, otherwise an error occurs.This function returns
true
if successful; otherwise it returnsfalse
and sets the socket error accordingly.Note
This function should be called with the same arguments as were passed to
joinMulticastGroup()
.See also
- PySide2.QtNetwork.QUdpSocket.leaveMulticastGroup(groupAddress, iface)
- Parameters:
groupAddress –
PySide2.QtNetwork.QHostAddress
- Return type:
bool
This is an overloaded function.
Leaves the multicast group specified by
groupAddress
on the interfaceiface
.Note
This function should be called with the same arguments as were passed to
joinMulticastGroup()
.See also
- PySide2.QtNetwork.QUdpSocket.multicastInterface()¶
- Return type:
Returns the interface for the outgoing interface for multicast datagrams. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. If no interface has been previously set, this function returns an invalid
QNetworkInterface
. The socket must be inBoundState
, otherwise an invalidQNetworkInterface
is returned.See also
- PySide2.QtNetwork.QUdpSocket.pendingDatagramSize()¶
- Return type:
int
Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.
See also
- PySide2.QtNetwork.QUdpSocket.readDatagram(maxlen)¶
- Parameters:
maxlen – int
- Return type:
(data, address, port)
Receives a datagram no larger than
maxSize
bytes and stores it indata
. The sender’s host address and port is stored in *``address`` and *``port`` (unless the pointers areNone
).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, callpendingDatagramSize()
to determine the size of the pending datagram before attempting to read it. IfmaxSize
is 0, the datagram will be discarded.
- PySide2.QtNetwork.QUdpSocket.receiveDatagram([maxSize=-1])¶
- Parameters:
maxSize – int
- Return type:
Receives a datagram no larger than
maxSize
bytes and returns it in theQNetworkDatagram
object, along with the sender’s host address and port. If possible, this function will also try to determine the datagram’s destination address, port, and the number of hop counts at reception time.On failure, returns a
QNetworkDatagram
that reportsnot valid
.If
maxSize
is too small, the rest of the datagram will be lost. IfmaxSize
is 0, the datagram will be discarded. IfmaxSize
is -1 (the default), this function will attempt to read the entire datagram.
- PySide2.QtNetwork.QUdpSocket.setMulticastInterface(iface)¶
- Parameters:
Sets the outgoing interface for multicast datagrams to the interface
iface
. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. The socket must be inBoundState
, otherwise this function does nothing.
- PySide2.QtNetwork.QUdpSocket.writeDatagram(datagram, host, port)¶
- Parameters:
datagram –
PySide2.QtCore.QByteArray
port –
quint16
- Return type:
int
This is an overloaded function.
Sends the datagram
datagram
to the host addresshost
and at portport
.The function returns the number of bytes sent if it succeeded or -1 if it encountered an error.
- PySide2.QtNetwork.QUdpSocket.writeDatagram(datagram)
- Parameters:
datagram –
PySide2.QtNetwork.QNetworkDatagram
- Return type:
int
This is an overloaded function.
Sends the datagram
datagram
to the host address and port numbers contained indatagram
, using the network interface and hop count limits also set there. If the destination address and port numbers are unset, this function will send to the address that was passed toconnectToHost()
.If the destination address is IPv6 with a non-empty
scope id
but differs from the interface index indatagram
, it is undefined which interface the operating system will choose to send on.The function returns the number of bytes sent if it succeeded or -1 if it encountered an error.
Warning
Calling this function on a connected UDP socket may result in an error and no packet being sent. If you are using a connected socket, use
write()
to send datagrams.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.