![]() |
Qt 6.x
The Qt SDK
|
\reentrant More...
#include <qudpsocket.h>
Public Member Functions | |
QUdpSocket (QObject *parent=nullptr) | |
Creates a QUdpSocket object. | |
virtual | ~QUdpSocket () |
Destroys the socket, closing the connection if necessary. | |
bool | joinMulticastGroup (const QHostAddress &groupAddress) |
bool | joinMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface) |
bool | leaveMulticastGroup (const QHostAddress &groupAddress) |
bool | leaveMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface) |
QNetworkInterface | multicastInterface () const |
void | setMulticastInterface (const QNetworkInterface &iface) |
bool | hasPendingDatagrams () const |
Returns true if at least one datagram is waiting to be read; otherwise returns false . | |
qint64 | pendingDatagramSize () const |
Returns the size of the first pending UDP datagram. | |
QNetworkDatagram | receiveDatagram (qint64 maxSize=-1) |
qint64 | readDatagram (char *data, qint64 maxlen, QHostAddress *host=nullptr, quint16 *port=nullptr) |
Receives a datagram no larger than maxSize bytes and stores it in data. | |
qint64 | writeDatagram (const QNetworkDatagram &datagram) |
qint64 | writeDatagram (const char *data, qint64 len, const QHostAddress &host, quint16 port) |
Sends the datagram at data of size size to the host address address at port port. | |
qint64 | writeDatagram (const QByteArray &datagram, const QHostAddress &host, quint16 port) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sends the datagram datagram to the host address host and at port port. | |
![]() | |
QAbstractSocket (SocketType socketType, QObject *parent) | |
Creates a new abstract socket of type socketType. | |
virtual | ~QAbstractSocket () |
Destroys the socket. | |
virtual void | resume () |
PauseModes | pauseMode () const |
void | setPauseMode (PauseModes pauseMode) |
virtual bool | bind (const QHostAddress &address, quint16 port=0, BindMode mode=DefaultForPlatform) |
bool | bind (QHostAddress::SpecialAddress addr, quint16 port=0, BindMode mode=DefaultForPlatform) |
bool | bind (quint16 port=0, BindMode mode=DefaultForPlatform) |
virtual void | connectToHost (const QString &hostName, quint16 port, OpenMode mode=ReadWrite, NetworkLayerProtocol protocol=AnyIPProtocol) |
Attempts to make a connection to hostName on the given port. | |
void | connectToHost (const QHostAddress &address, quint16 port, OpenMode mode=ReadWrite) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Attempts to make a connection to address on port port. | |
virtual void | disconnectFromHost () |
Attempts to close the socket. | |
bool | isValid () const |
Returns true if the socket is valid and ready for use; otherwise returns false . | |
qint64 | bytesAvailable () const override |
Returns the number of incoming bytes that are waiting to be read. | |
qint64 | bytesToWrite () const override |
Returns the number of bytes that are waiting to be written. | |
quint16 | localPort () const |
Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0. | |
QHostAddress | localAddress () const |
Returns the host address of the local socket if available; otherwise returns QHostAddress::Null. | |
quint16 | peerPort () const |
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0. | |
QHostAddress | peerAddress () const |
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHostAddress::Null. | |
QString | peerName () const |
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() has not been called. | |
qint64 | readBufferSize () const |
Returns the size of the internal read buffer. | |
virtual void | setReadBufferSize (qint64 size) |
Sets the size of QAbstractSocket's internal read buffer to be size bytes. | |
void | abort () |
Aborts the current connection and resets the socket. | |
virtual qintptr | socketDescriptor () const |
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise returns -1. | |
virtual bool | setSocketDescriptor (qintptr socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite) |
Initializes QAbstractSocket with the native socket descriptor socketDescriptor. | |
virtual void | setSocketOption (QAbstractSocket::SocketOption option, const QVariant &value) |
virtual QVariant | socketOption (QAbstractSocket::SocketOption option) |
SocketType | socketType () const |
Returns the socket type (TCP, UDP, or other). | |
SocketState | state () const |
Returns the state of the socket. | |
SocketError | error () const |
Returns the type of error that last occurred. | |
void | close () override |
Closes the I/O device for the socket and calls disconnectFromHost() to close the socket's connection. | |
bool | isSequential () const override |
\reimp | |
bool | flush () |
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. | |
virtual bool | waitForConnected (int msecs=30000) |
Waits until the socket is connected, up to msecs milliseconds. | |
bool | waitForReadyRead (int msecs=30000) override |
This function blocks until new data is available for reading and the \l{QIODevice::}{readyRead()} signal has been emitted. | |
bool | waitForBytesWritten (int msecs=30000) override |
\reimp | |
virtual bool | waitForDisconnected (int msecs=30000) |
Waits until the socket has disconnected, up to msecs milliseconds. | |
void | setProxy (const QNetworkProxy &networkProxy) |
QNetworkProxy | proxy () const |
QString | protocolTag () const |
void | setProtocolTag (const QString &tag) |
![]() | |
QIODevice () | |
Constructs a QIODevice object. | |
QIODevice (QObject *parent) | |
Constructs a QIODevice object with the given parent. | |
virtual | ~QIODevice () |
The destructor is virtual, and QIODevice is an abstract base class. | |
QIODeviceBase::OpenMode | openMode () const |
Returns the mode in which the device has been opened; i.e. | |
void | setTextModeEnabled (bool enabled) |
If enabled is true, this function sets the \l Text flag on the device; otherwise the \l Text flag is removed. | |
bool | isTextModeEnabled () const |
Returns true if the \l Text flag is enabled; otherwise returns false . | |
bool | isOpen () const |
Returns true if the device is open; otherwise returns false . | |
bool | isReadable () const |
Returns true if data can be read from the device; otherwise returns false. | |
bool | isWritable () const |
Returns true if data can be written to the device; otherwise returns false. | |
virtual bool | isSequential () const |
Returns true if this device is sequential; otherwise returns false. | |
int | readChannelCount () const |
int | writeChannelCount () const |
int | currentReadChannel () const |
void | setCurrentReadChannel (int channel) |
int | currentWriteChannel () const |
void | setCurrentWriteChannel (int channel) |
virtual bool | open (QIODeviceBase::OpenMode mode) |
Opens the device and sets its OpenMode to mode. | |
virtual void | close () |
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen. | |
virtual qint64 | pos () const |
For random-access devices, this function returns the position that data is written to or read from. | |
virtual qint64 | size () const |
For open random-access devices, this function returns the size of the device. | |
virtual bool | seek (qint64 pos) |
For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred. | |
virtual bool | atEnd () const |
Returns true if the current read and write position is at the end of the device (i.e. | |
virtual bool | reset () |
Seeks to the start of input for random-access devices. | |
virtual qint64 | bytesAvailable () const |
Returns the number of bytes that are available for reading. | |
virtual qint64 | bytesToWrite () const |
For buffered devices, this function returns the number of bytes waiting to be written. | |
qint64 | read (char *data, qint64 maxlen) |
Reads at most maxSize bytes from the device into data, and returns the number of bytes read. | |
QByteArray | read (qint64 maxlen) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reads at most maxSize bytes from the device, and returns the data read as a QByteArray. | |
QByteArray | readAll () |
Reads all remaining data from the device, and returns it as a byte array. | |
qint64 | readLine (char *data, qint64 maxlen) |
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. | |
QByteArray | readLine (qint64 maxlen=0) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reads a line from the device, but no more than maxSize characters, and returns the result as a byte array. | |
virtual bool | canReadLine () const |
Returns true if a complete line of data can be read from the device; otherwise returns false . | |
void | startTransaction () |
void | commitTransaction () |
void | rollbackTransaction () |
bool | isTransactionStarted () const |
qint64 | write (const char *data, qint64 len) |
Writes at most maxSize bytes of data from data to the device. | |
qint64 | write (const char *data) |
qint64 | write (const QByteArray &data) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes the content of data to the device. | |
qint64 | peek (char *data, qint64 maxlen) |
QByteArray | peek (qint64 maxlen) |
qint64 | skip (qint64 maxSize) |
virtual bool | waitForReadyRead (int msecs) |
Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. | |
virtual bool | waitForBytesWritten (int msecs) |
For buffered devices, this function waits until a payload of buffered written data has been written to the device and the bytesWritten() signal has been emitted, or until msecs milliseconds have passed. | |
void | ungetChar (char c) |
Puts the character c back into the device, and decrements the current position unless the position is 0. | |
bool | putChar (char c) |
Writes the character c to the device. | |
bool | getChar (char *c) |
Reads one character from the device and stores it in c. | |
QString | errorString () const |
Returns a human-readable description of the last device error that occurred. | |
![]() | |
Q_INVOKABLE | QObject (QObject *parent=nullptr) |
Constructs an object with parent object parent. | |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. | |
virtual bool | event (QEvent *event) |
This virtual function receives events to an object and should return true if the event e was recognized and processed. | |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
Filters events if this object has been installed as an event filter for the watched object. | |
QString | objectName () const |
Q_WEAK_OVERLOAD void | setObjectName (const QString &name) |
Sets the object's name to name. | |
void | setObjectName (QAnyStringView name) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QBindable< QString > | bindableObjectName () |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false . | |
bool | isWindowType () const |
Returns true if the object is a window; otherwise returns false . | |
bool | isQuickItemType () const |
Returns true if the object is a QQuickItem; otherwise returns false . | |
bool | signalsBlocked () const noexcept |
Returns true if signals are blocked; otherwise returns false . | |
bool | blockSignals (bool b) noexcept |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). | |
QThread * | thread () const |
Returns the thread in which the object lives. | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. | |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds. | |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType=Qt::CoarseTimer) |
void | killTimer (int id) |
Kills the timer with timer identifier, id. | |
template<typename T > | |
T | findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object. | |
template<typename T > | |
QList< T > | findChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. | |
template<typename T > | |
QList< T > | findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
const QObjectList & | children () const |
Returns a list of child objects. | |
void | setParent (QObject *parent) |
Makes the object a child of parent. | |
void | installEventFilter (QObject *filterObj) |
Installs an event filter filterObj on this object. | |
void | removeEventFilter (QObject *obj) |
Removes an event filter object obj from this object. | |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const |
bool | disconnect (const QObject *receiver, const char *member=nullptr) const |
void | dumpObjectTree () const |
Dumps a tree of children to the debug output. | |
void | dumpObjectInfo () const |
Dumps information about signal connections, etc. | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. | |
bool | setProperty (const char *name, QVariant &&value) |
QVariant | property (const char *name) const |
Returns the value of the object's name property. | |
QList< QByteArray > | dynamicPropertyNames () const |
QBindingStorage * | bindingStorage () |
const QBindingStorage * | bindingStorage () const |
QObject * | parent () const |
Returns a pointer to the parent object. | |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false . | |
Additional Inherited Members | |
![]() | |
enum | SocketType { TcpSocket , UdpSocket , SctpSocket , UnknownSocketType = -1 } |
This enum describes the transport layer protocol. More... | |
enum | SocketError { ConnectionRefusedError , RemoteHostClosedError , HostNotFoundError , SocketAccessError , SocketResourceError , SocketTimeoutError , DatagramTooLargeError , NetworkError , AddressInUseError , SocketAddressNotAvailableError , UnsupportedSocketOperationError , UnfinishedSocketOperationError , ProxyAuthenticationRequiredError , SslHandshakeFailedError , ProxyConnectionRefusedError , ProxyConnectionClosedError , ProxyConnectionTimeoutError , ProxyNotFoundError , ProxyProtocolError , OperationError , SslInternalError , SslInvalidUserDataError , TemporaryError , UnknownSocketError = -1 } |
This enum describes the socket errors that can occur. More... | |
enum | SocketState { UnconnectedState , HostLookupState , ConnectingState , ConnectedState , BoundState , ListeningState , ClosingState } |
This enum describes the different states in which a socket can be. More... | |
enum | SocketOption { LowDelayOption , KeepAliveOption , MulticastTtlOption , MulticastLoopbackOption , TypeOfServiceOption , SendBufferSizeSocketOption , ReceiveBufferSizeSocketOption , PathMtuSocketOption } |
enum | BindFlag { DefaultForPlatform = 0x0 , ShareAddress = 0x1 , DontShareAddress = 0x2 , ReuseAddressHint = 0x4 } |
enum | PauseMode { PauseNever = 0x0 , PauseOnSslErrors = 0x1 } |
using | NetworkLayerProtocol = QHostAddress::NetworkLayerProtocol |
![]() | |
enum | OpenModeFlag { NotOpen = 0x0000 , ReadOnly = 0x0001 , WriteOnly = 0x0002 , ReadWrite = ReadOnly | WriteOnly , Append = 0x0004 , Truncate = 0x0008 , Text = 0x0010 , Unbuffered = 0x0020 , NewOnly = 0x0040 , ExistingOnly = 0x0080 } |
This enum is used with QIODevice::open() to describe the mode in which a device is opened. More... | |
![]() | |
void | deleteLater () |
\threadsafe | |
![]() | |
void | hostFound () |
This signal is emitted after connectToHost() has been called and the host lookup has succeeded. | |
void | connected () |
This signal is emitted after connectToHost() has been called and a connection has been successfully established. | |
void | disconnected () |
This signal is emitted when the socket has been disconnected. | |
void | stateChanged (QAbstractSocket::SocketState) |
This signal is emitted whenever QAbstractSocket's state changes. | |
void | errorOccurred (QAbstractSocket::SocketError) |
void | proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *authenticator) |
![]() | |
void | readyRead () |
This signal is emitted once every time new data is available for reading from the device's current read channel. | |
void | channelReadyRead (int channel) |
void | bytesWritten (qint64 bytes) |
This signal is emitted every time a payload of data has been written to the device's current write channel. | |
void | channelBytesWritten (int channel, qint64 bytes) |
void | aboutToClose () |
This signal is emitted when the device is about to close. | |
void | readChannelFinished () |
![]() | |
void | destroyed (QObject *=nullptr) |
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked. | |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
This signal is emitted after the object's name has been changed. | |
![]() | |
static QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
\threadsafe | |
static QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
\threadsafe | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static bool | disconnect (const QMetaObject::Connection &) |
Disconnect a connection. | |
template<typename Func1 , typename Func2 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot) |
template<typename Func1 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero) |
![]() | |
static constexpr auto | IPv4Protocol = QHostAddress::IPv4Protocol |
static constexpr auto | IPv6Protocol = QHostAddress::IPv6Protocol |
static constexpr auto | AnyIPProtocol = QHostAddress::AnyIPProtocol |
static constexpr auto | UnknownNetworkLayerProtocol = QHostAddress::UnknownNetworkLayerProtocol |
![]() | |
qint64 | readData (char *data, qint64 maxlen) override |
\reimp | |
qint64 | readLineData (char *data, qint64 maxlen) override |
\reimp | |
qint64 | skipData (qint64 maxSize) override |
\reimp | |
qint64 | writeData (const char *data, qint64 len) override |
\reimp | |
void | setSocketState (SocketState state) |
Sets the state of the socket to state. | |
void | setSocketError (SocketError socketError) |
Sets the type of error that last occurred to socketError. | |
void | setLocalPort (quint16 port) |
void | setLocalAddress (const QHostAddress &address) |
void | setPeerPort (quint16 port) |
void | setPeerAddress (const QHostAddress &address) |
void | setPeerName (const QString &name) |
QAbstractSocket (SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent=nullptr) | |
![]() | |
QIODevice (QIODevicePrivate &dd, QObject *parent=nullptr) | |
virtual qint64 | readData (char *data, qint64 maxlen)=0 |
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an error occurred. | |
virtual qint64 | readLineData (char *data, qint64 maxlen) |
Reads up to maxSize characters into data and returns the number of characters read. | |
virtual qint64 | skipData (qint64 maxSize) |
virtual qint64 | writeData (const char *data, qint64 len)=0 |
Writes up to maxSize bytes from data to the device. | |
void | setOpenMode (QIODeviceBase::OpenMode openMode) |
Sets the OpenMode of the device to openMode. | |
void | setErrorString (const QString &errorString) |
Sets the human readable description of the last device error that occurred to str. | |
![]() | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr. | |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. | |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | timerEvent (QTimerEvent *event) |
This event handler can be reimplemented in a subclass to receive timer events for the object. | |
virtual void | childEvent (QChildEvent *event) |
This event handler can be reimplemented in a subclass to receive child events. | |
virtual void | customEvent (QEvent *event) |
This event handler can be reimplemented in a subclass to receive custom events. | |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
QObject (QObjectPrivate &dd, QObject *parent=nullptr) | |
![]() | |
~QIODeviceBase ()=default | |
![]() | |
QScopedPointer< QObjectData > | d_ptr |
![]() | |
QString | objectName |
the name of this object | |
![]() | |
template< class T > T | qobject_cast (const QObject *object) |
Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns \nullptr. | |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
\macro Q_CLASSINFO(Name, Value) | |
\reentrant
The QUdpSocket class provides a UDP socket.
\inmodule QtNetwork
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.
Example:
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 \l{broadcastsender}{Broadcast Sender}, \l{broadcastreceiver}{Broadcast Receiver}, \l{multicastsender}{Multicast Sender}, and \l{multicastreceiver}{Multicast Receiver} examples illustrate how to use QUdpSocket in applications.
Definition at line 20 of file qudpsocket.h.
Creates a QUdpSocket object.
parent is passed to the QObject constructor.
Definition at line 121 of file qudpsocket.cpp.
|
virtual |
Destroys the socket, closing the connection if necessary.
Definition at line 132 of file qudpsocket.cpp.
bool QUdpSocket::hasPendingDatagrams | ( | ) | const |
Returns true
if at least one datagram is waiting to be read; otherwise returns false
.
Definition at line 266 of file qudpsocket.cpp.
References QT_CHECK_BOUND.
Referenced by QSocks5SocketEnginePrivate::_q_udpSocketReadNotification().
bool QUdpSocket::joinMulticastGroup | ( | const QHostAddress & | groupAddress | ) |
Joins the multicast group specified by groupAddress on 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 QHostAddress::Any). You must use QHostAddress::AnyIPv4 instead.
This function returns true
if successful; otherwise it returns false
and sets the socket error accordingly.
Definition at line 158 of file qudpsocket.cpp.
References joinMulticastGroup().
Referenced by joinMulticastGroup().
bool QUdpSocket::joinMulticastGroup | ( | const QHostAddress & | groupAddress, |
const QNetworkInterface & | iface | ||
) |
Joins the multicast group address groupAddress on the interface iface.
Definition at line 172 of file qudpsocket.cpp.
References d, and QT_CHECK_BOUND.
bool QUdpSocket::leaveMulticastGroup | ( | const QHostAddress & | groupAddress | ) |
Leaves the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState, otherwise an error occurs.
This function returns true
if successful; otherwise it returns false
and sets the socket error accordingly.
Definition at line 195 of file qudpsocket.cpp.
References leaveMulticastGroup().
Referenced by leaveMulticastGroup().
bool QUdpSocket::leaveMulticastGroup | ( | const QHostAddress & | groupAddress, |
const QNetworkInterface & | iface | ||
) |
Leaves the multicast group specified by groupAddress on the interface iface.
Definition at line 212 of file qudpsocket.cpp.
References QT_CHECK_BOUND.
QNetworkInterface QUdpSocket::multicastInterface | ( | ) | const |
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 QNetworkInterface is returned.
Definition at line 231 of file qudpsocket.cpp.
References d, and QT_CHECK_BOUND.
qint64 QUdpSocket::pendingDatagramSize | ( | ) | const |
Returns the size of the first pending UDP datagram.
If there is no datagram available, this function returns -1.
Definition at line 278 of file qudpsocket.cpp.
References QT_CHECK_BOUND.
Referenced by QSocks5SocketEnginePrivate::_q_udpSocketReadNotification().
qint64 QUdpSocket::readDatagram | ( | char * | data, |
qint64 | maxSize, | ||
QHostAddress * | address = nullptr , |
||
quint16 * | port = nullptr |
||
) |
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 \nullptr).
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 pendingDatagramSize() to determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded.
Definition at line 458 of file qudpsocket.cpp.
References d, header(), qDebug, QT_CHECK_BOUND, QAbstractSocket::TemporaryError, tr, and QAbstractSocketEngine::WantDatagramSender.
Referenced by QSocks5SocketEnginePrivate::_q_udpSocketReadNotification().
QNetworkDatagram QUdpSocket::receiveDatagram | ( | qint64 | maxSize = -1 | ) |
Receives a datagram no larger than maxSize bytes and returns it in the QNetworkDatagram 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 reports \l {QNetworkDatagram::isValid()}{not valid}.
If maxSize is too small, the rest of the datagram will be lost. If maxSize is 0, the datagram will be discarded. If maxSize is -1 (the default), this function will attempt to read the entire datagram.
Definition at line 415 of file qudpsocket.cpp.
References d, qDebug, QT_CHECK_BOUND, Qt::Uninitialized, and QAbstractSocketEngine::WantAll.
void QUdpSocket::setMulticastInterface | ( | const QNetworkInterface & | iface | ) |
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.
Definition at line 248 of file qudpsocket.cpp.
References d, QAbstractSocket::isValid(), and qWarning.
qint64 QUdpSocket::writeDatagram | ( | const char * | data, |
qint64 | size, | ||
const QHostAddress & | address, | ||
quint16 | port | ||
) |
Sends the datagram at data of size size to the host address address at port port.
Returns the number of bytes sent on success; otherwise returns -1.
Datagrams are always written as one block. The maximum size of a datagram is highly platform-dependent, but can be as low as 8192 bytes. If the datagram is too large, this function will return -1 and error() will return DatagramTooLargeError.
Sending datagrams larger than 512 bytes is in general disadvised, as even if they are sent successfully, they are likely to be fragmented by the IP layer before arriving at their final destination.
Definition at line 305 of file qudpsocket.cpp.
References QHostAddress::Any, QAbstractSocket::bind(), QIODevice::bytesWritten(), d, emit, qDebug, QAbstractSocket::state(), QAbstractSocket::TemporaryError, tr, and QAbstractSocket::UnconnectedState.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sends the datagram datagram to the host address host and at port port.
The function returns the number of bytes sent if it succeeded or -1 if it encountered an error.
Definition at line 53 of file qudpsocket.h.
References QByteArray::constData(), QByteArray::size(), and writeDatagram().
qint64 QUdpSocket::writeDatagram | ( | const QNetworkDatagram & | datagram | ) |
Sends the datagram datagram to 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 \l{QHostAddress::scopeId()}{scope id} but 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.
Definition at line 370 of file qudpsocket.cpp.
References QHostAddress::Any, QAbstractSocket::bind(), QIODevice::bytesWritten(), QByteArray::constData(), d, QNetworkDatagramPrivate::data, QNetworkDatagram::destinationAddress(), QNetworkDatagram::destinationPort(), emit, QNetworkDatagramPrivate::header, qDebug, QByteArray::size(), QAbstractSocket::state(), QString::toLatin1(), QHostAddress::toString(), and QAbstractSocket::UnconnectedState.
Referenced by dtlsbio::q_dgram_write().