PySide6.QtNetwork.QUdpSocket¶
- class QUdpSocket¶
- The - QUdpSocketclass provides a UDP socket. More…- Synopsis¶- Methods¶- def - __init__()
- def - bind()
- def - readDatagram()
- def - writeDatagram()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn’t important. - QUdpSocketis a subclass of- QAbstractSocketthat 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 call- writeDatagram()and- readDatagram()/- receiveDatagram()to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling- connectToHost().- 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 call - bind().- The readyRead() signal is emitted whenever datagrams arrive. In that case, - hasPendingDatagrams()returns- true. Call- pendingDatagramSize()to obtain the size of the first pending datagram, and- readDatagram()or- receiveDatagram()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) udpSocket.readyRead.connect( self.readPendingDatagrams) def readPendingDatagrams(self): while udpSocket.hasPendingDatagrams(): datagram = udpSocket.receiveDatagram() processTheDatagram(datagram) - QUdpSocketalso supports UDP multicast. Use- joinMulticastGroup()and- leaveMulticastGroup()to control group membership, and- MulticastTtlOptionand- MulticastLoopbackOptionto set the TTL and loopback socket options. Use- setMulticastInterface()to control the outgoing interface for multicast datagrams, and- multicastInterface()to query it.- With - QUdpSocket, you can also establish a virtual connection to a UDP server using- connectToHost()and then use read() and write() 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 - QUdpSocketin applications.- See also - Creates a - QUdpSocketobject.- parentis passed to the QObject constructor.- See also - bind(addr[, port=0[, mode=QAbstractSocket.BindFlag.DefaultForPlatform]])¶
- Parameters:
- addr – - SpecialAddress
- port – int 
- mode – Combination of - BindFlag
 
- Return type:
- bool 
 
 - hasPendingDatagrams()¶
- Return type:
- bool 
 
 - Returns - trueif at least one datagram is waiting to be read; otherwise returns- false.- See also - joinMulticastGroup(groupAddress)¶
- Parameters:
- groupAddress – - QHostAddress
- Return type:
- bool 
 
 - Joins the multicast group specified by - groupAddresson the default interface chosen by the operating system. The socket must be in BoundState, 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 use- AnyIPv4instead.- This function returns - trueif successful; otherwise it returns- falseand 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 - joinMulticastGroup(groupAddress, iface)
- Parameters:
- groupAddress – - QHostAddress
- iface – - QNetworkInterface
 
- Return type:
- bool 
 
 - This is an overloaded function. - Joins the multicast group address - groupAddresson the interface- iface.- See also - leaveMulticastGroup(groupAddress)¶
- Parameters:
- groupAddress – - QHostAddress
- Return type:
- bool 
 
 - Leaves the multicast group specified by - groupAddresson the default interface chosen by the operating system. The socket must be in BoundState, otherwise an error occurs.- This function returns - trueif successful; otherwise it returns- falseand sets the socket error accordingly.- Note - This function should be called with the same arguments as were passed to - joinMulticastGroup().- See also - leaveMulticastGroup(groupAddress, iface)
- Parameters:
- groupAddress – - QHostAddress
- iface – - QNetworkInterface
 
- Return type:
- bool 
 
 - This is an overloaded function. - Leaves the multicast group specified by - groupAddresson the interface- iface.- Note - This function should be called with the same arguments as were passed to - joinMulticastGroup().- See also - 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 in BoundState, otherwise an invalid- QNetworkInterfaceis returned.- See also - 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 - readDatagram(maxlen)¶
- Parameters:
- maxlen – int 
- Return type:
- (data, address, port) 
 
 - Receives a datagram no larger than - maxSizebytes and stores it in- data. The sender’s host address and port is stored in *``address`` and *``port`` (unless the pointers are- None).- Returns the size of the datagram on success; otherwise returns -1. - If - maxSizeis too small, the rest of the datagram will be lost. To avoid loss of data, call- pendingDatagramSize()to determine the size of the pending datagram before attempting to read it. If- maxSizeis 0, the datagram will be discarded.- receiveDatagram([maxSize=-1])¶
- Parameters:
- maxSize – int 
- Return type:
 
 - Receives a datagram no larger than - maxSizebytes and returns it in the- QNetworkDatagramobject, 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 - QNetworkDatagramthat reports- not valid.- If - maxSizeis too small, the rest of the datagram will be lost. If- maxSizeis 0, the datagram will be discarded. If- maxSizeis -1 (the default), this function will attempt to read the entire datagram.- setMulticastInterface(iface)¶
- Parameters:
- iface – - QNetworkInterface
 
 - 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 in BoundState, otherwise this function does nothing.- writeDatagram(datagram)¶
- Parameters:
- datagram – - QNetworkDatagram
- Return type:
- int 
 
 - This is an overloaded function. - Sends the datagram - datagramto the host address and port numbers contained in- datagram, 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 to- connectToHost().- If the destination address is IPv6 with a non-empty - scope idbut differs from the interface index in- datagram, 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. - writeDatagram(datagram, host, port)
- Parameters:
- datagram – - QByteArray
- host – - QHostAddress
- port – int 
 
- Return type:
- int 
 
 - This is an overloaded function. - Sends the datagram - datagramto the host address- hostand at port- port.- The function returns the number of bytes sent if it succeeded or -1 if it encountered an error.