Skip to content

PythonQt.QtNetwork.QUdpSocket

QUdpSocket

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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 of 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 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:

void Server::initSocket()
{
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::LocalHost, 7755);

    connect(udpSocket, &QUdpSocket::readyRead,
            this, &Server::readPendingDatagrams);
}

void Server::readPendingDatagrams()
{
    while (udpSocket->hasPendingDatagrams()) {
        QNetworkDatagram datagram = udpSocket->receiveDatagram();
        processTheDatagram(datagram);
    }
}

QUdpSocket also supports UDP multicast. Use joinMulticastGroup() and leaveMulticastGroup() to control group membership, and QAbstractSocket::MulticastTtlOption and QAbstractSocket::MulticastLoopbackOption to 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 QUdpSocket in applications.

Source: https://doc.qt.io/archives/qt-5.15/qudpsocket.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
AddressInUseError attribute Any (shared captured text)
AnyIPProtocol attribute Any (shared captured text)
Append attribute Any (shared captured text)
BindFlag attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
BindMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
BoundState attribute Any (shared captured text)
ClosingState attribute Any (shared captured text)
ConnectedState attribute Any (shared captured text)
ConnectingState attribute Any (shared captured text)
ConnectionRefusedError attribute Any (shared captured text)
DatagramTooLargeError attribute Any (shared captured text)
DefaultForPlatform attribute Any (shared captured text)
DontShareAddress attribute Any (shared captured text)
HostLookupState attribute Any (shared captured text)
HostNotFoundError attribute Any (shared captured text)
IPv4Protocol attribute Any (shared captured text)
IPv6Protocol attribute Any (shared captured text)
KeepAliveOption attribute Any (shared captured text)
ListeningState attribute Any (shared captured text)
LowDelayOption attribute Any (shared captured text)
MulticastLoopbackOption attribute Any (shared captured text)
MulticastTtlOption attribute Any (shared captured text)
NetworkError attribute Any (shared captured text)
NetworkLayerProtocol attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
NotOpen attribute Any (shared captured text)
OpenMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
OpenModeFlag attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
OperationError attribute Any (shared captured text)
PathMtuSocketOption attribute Any (shared captured text)
PauseMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
PauseModes attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
PauseNever attribute Any (shared captured text)
PauseOnSslErrors attribute Any (shared captured text)
ProxyAuthenticationRequiredError attribute Any (shared captured text)
ProxyConnectionClosedError attribute Any (shared captured text)
ProxyConnectionRefusedError attribute Any (shared captured text)
ProxyConnectionTimeoutError attribute Any (shared captured text)
ProxyNotFoundError attribute Any (shared captured text)
ProxyProtocolError attribute Any (shared captured text)
ReadOnly attribute Any (shared captured text)
ReadWrite attribute Any (shared captured text)
ReceiveBufferSizeSocketOption attribute Any (shared captured text)
RemoteHostClosedError attribute Any (shared captured text)
ReuseAddressHint attribute Any (shared captured text)
SctpSocket attribute Any (shared captured text)
SendBufferSizeSocketOption attribute Any (shared captured text)
ShareAddress attribute Any (shared captured text)
SocketAccessError attribute Any (shared captured text)
SocketAddressNotAvailableError attribute Any (shared captured text)
SocketError attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
SocketOption attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
SocketResourceError attribute Any (shared captured text)
SocketState attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
SocketTimeoutError attribute Any (shared captured text)
SocketType attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
SslHandshakeFailedError attribute Any (shared captured text)
SslInternalError attribute Any (shared captured text)
SslInvalidUserDataError attribute Any (shared captured text)
TcpSocket attribute Any (shared captured text)
TemporaryError attribute Any (shared captured text)
Text attribute Any (shared captured text)
Truncate attribute Any (shared captured text)
TypeOfServiceOption attribute Any (shared captured text)
UdpSocket attribute Any (shared captured text)
Unbuffered attribute Any (shared captured text)
UnconnectedState attribute Any (shared captured text)
UnfinishedSocketOperationError attribute Any (shared captured text)
UnknownNetworkLayerProtocol attribute Any (shared captured text)
UnknownSocketError attribute Any (shared captured text)
UnknownSocketType attribute Any (shared captured text)
UnsupportedSocketOperationError attribute Any (shared captured text)
WriteOnly attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
abort function (self) -> Any (shared captured text)
aboutToClose function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.aboutToClose; argument details are not documented.
atEnd function (self) -> bool X.atEnd() -> bool (shared captured text)
bind function (self, address: Any, port: Any, mode: Any) -> bool X.bind(address, port, mode) -> bool (shared captured text)
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
bytesAvailable function (self) -> int X.bytesAvailable() -> int (shared captured text)
bytesToWrite function (self) -> int X.bytesToWrite() -> int (shared captured text)
bytesWritten function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.bytesWritten; argument details are not documented.
canReadLine function (self) -> bool X.canReadLine() -> bool (shared captured text)
channelBytesWritten function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.channelBytesWritten; argument details are not documented.
channelReadyRead function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.channelReadyRead; argument details are not documented.
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
close function (self) -> Any (shared captured text)
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
connectToHost function (self, hostName: Any, port: Any, mode: Any, protocol: Any) -> Any X.connectToHost(hostName, port, mode, protocol) (shared captured text)
connected function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.connected; argument details are not documented.
customEvent function (self, event: Any) -> Any (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.destroyed; argument details are not documented.
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
disconnectFromHost function (self) -> Any (shared captured text)
disconnected function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.disconnected; argument details are not documented.
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
error function (self) -> Any (shared captured text)
errorOccurred function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.errorOccurred; argument details are not documented.
errorString function (self) -> str X.errorString() -> str (shared captured text)
event function (self, event: Any) -> bool X.event(event) -> bool (shared captured text)
eventFilter function (self, watched: Any, event: Any) -> bool X.eventFilter(watched, event) -> bool (shared captured text)
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
flush function (self) -> bool X.flush() -> bool (shared captured text)
getChar function (self, c: Any) -> bool X.getChar(c) -> bool (shared captured text)
hasPendingDatagrams function (self) -> bool X.hasPendingDatagrams() -> bool
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
hostFound function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.hostFound; argument details are not documented.
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isOpen function (self) -> bool X.isOpen() -> bool (shared captured text)
isReadable function (self) -> bool X.isReadable() -> bool (shared captured text)
isSequential function (self) -> bool X.isSequential() -> bool (shared captured text)
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isTextModeEnabled function (self) -> bool X.isTextModeEnabled() -> bool (shared captured text)
isValid function (self) -> bool X.isValid() -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
isWritable function (self) -> bool X.isWritable() -> bool (shared captured text)
joinMulticastGroup function (self, groupAddress: Any, iface: Any) -> bool X.joinMulticastGroup(groupAddress, iface) -> bool
killTimer function (self, id: Any) -> Any (shared captured text)
leaveMulticastGroup function (self, groupAddress: Any, iface: Any) -> bool X.leaveMulticastGroup(groupAddress, iface) -> bool
localAddress function (self) -> Any X.localAddress() -> PythonQt.QtNetwork.QHostAddress (shared captured text)
localPort function (self) -> int X.localPort() -> int (shared captured text)
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
moveToThread function (self, thread: Any) -> Any (shared captured text)
multicastInterface function (self) -> Any X.multicastInterface() -> PythonQt.QtNetwork.QNetworkInterface
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.objectNameChanged; argument details are not documented.
open function (self, mode: Any) -> bool X.open(mode) -> bool (shared captured text)
openMode function (self) -> Any (shared captured text)
parent function (self) -> Any X.parent() -> PythonQt.private.QObject (shared captured text)
pauseMode function (self) -> Any (shared captured text)
peek function (self, maxlen: Any) -> Any X.peek(maxlen) -> PythonQt.QtCore.QByteArray (shared captured text)
peerAddress function (self) -> Any X.peerAddress() -> PythonQt.QtNetwork.QHostAddress (shared captured text)
peerName function (self) -> str X.peerName() -> str (shared captured text)
peerPort function (self) -> int X.peerPort() -> int (shared captured text)
pendingDatagramSize function (self) -> int X.pendingDatagramSize() -> int
pos function (self) -> int X.pos() -> int (shared captured text)
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
proxy function (self) -> Any X.proxy() -> PythonQt.QtNetwork.QNetworkProxy (shared captured text)
proxyAuthenticationRequired function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.proxyAuthenticationRequired; argument details are not documented.
putChar function (self, c: Any) -> bool X.putChar(c) -> bool (shared captured text)
read function (self, maxlen: Any) -> Any X.read(maxlen) -> PythonQt.QtCore.QByteArray (shared captured text)
readAll function (self) -> Any X.readAll() -> PythonQt.QtCore.QByteArray (shared captured text)
readBufferSize function (self) -> int X.readBufferSize() -> int (shared captured text)
readChannelFinished function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.readChannelFinished; argument details are not documented.
readData function (self, data: Any, maxlen: Any) -> int X.readData(data, maxlen) -> int (shared captured text)
readDatagram function (self, data: str, maxlen: int, host: Any, port: Any) -> int X.readDatagram(data, maxlen, host, port) -> int
readLine function (self, maxlen: Any) -> Any X.readLine(maxlen) -> PythonQt.QtCore.QByteArray (shared captured text)
readLineData function (self, data: Any, maxlen: Any) -> int X.readLineData(data, maxlen) -> int (shared captured text)
readyRead function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.readyRead; argument details are not documented.
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
reset function (self) -> bool X.reset() -> bool (shared captured text)
resume function (self) -> Any (shared captured text)
seek function (self, pos: Any) -> bool X.seek(pos) -> bool (shared captured text)
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setErrorString function (self, errorString: Any) -> Any (shared captured text)
setLocalAddress function (self, address: Any) -> Any (shared captured text)
setLocalPort function (self, port: Any) -> Any (shared captured text)
setMulticastInterface function (self, iface: Any) -> None Sets the outgoing interface for multicast datagrams to the interface iface.
setObjectName function (self, name: Any) -> Any (shared captured text)
setOpenMode function (self, openMode: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setPauseMode function (self, pauseMode: Any) -> Any (shared captured text)
setPeerAddress function (self, address: Any) -> Any (shared captured text)
setPeerName function (self, name: Any) -> Any (shared captured text)
setPeerPort function (self, port: Any) -> Any (shared captured text)
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
setProxy function (self, networkProxy: Any) -> Any (shared captured text)
setReadBufferSize function (self, size: Any) -> Any (shared captured text)
setSocketDescriptor function (self, socketDescriptor: Any, state: Any, openMode: Any) -> bool X.setSocketDescriptor(socketDescriptor, state, openMode) -> bool (shared captured text)
setSocketError function (self, socketError: Any) -> Any (shared captured text)
setSocketOption function (self, option: Any, value: Any) -> Any X.setSocketOption(option, value) (shared captured text)
setSocketState function (self, state: Any) -> Any (shared captured text)
setTextModeEnabled function (self, enabled: Any) -> Any (shared captured text)
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
size function (self) -> int X.size() -> int (shared captured text)
socketDescriptor function (self) -> int X.socketDescriptor() -> int (shared captured text)
socketOption function (self, option: Any) -> Any X.socketOption(option) -> object (shared captured text)
socketType function (self) -> Any (shared captured text)
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
state function (self) -> Any (shared captured text)
stateChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QUdpSocket.stateChanged; argument details are not documented.
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)
ungetChar function (self, c: Any) -> Any (shared captured text)
waitForBytesWritten function (self, msecs: Any) -> bool X.waitForBytesWritten(msecs) -> bool (shared captured text)
waitForConnected function (self, msecs: Any) -> bool X.waitForConnected(msecs) -> bool (shared captured text)
waitForDisconnected function (self, msecs: Any) -> bool X.waitForDisconnected(msecs) -> bool (shared captured text)
waitForReadyRead function (self, msecs: Any) -> bool X.waitForReadyRead(msecs) -> bool (shared captured text)
write function (self, data: Any) -> int X.write(data) -> int (shared captured text)
writeData function (self, data: Any, len: Any) -> int X.writeData(data, len) -> int (shared captured text)
writeDatagram function (self, datagram: Any, host: Any, port: Any) -> int X.writeDatagram(datagram, host, port) -> int