Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QUdpSocket Class Reference

The QUdpSocket class provides a UDP socket. More...

    #include <QUdpSocket>

Inherits QAbstractSocket.

Note: All the functions in this class are reentrant.

- List of all members, including inherited members

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QUdpSocket ( QObject * parent = 0 )
- virtual ~QUdpSocket ()
- bool bind ( const QHostAddress & address, Q_UINT16 port )
- bool bind ( Q_UINT16 port = 0 )
- bool hasPendingDatagrams () const
- Q_LONGLONG pendingDatagramSize () const
- Q_LONGLONG readDatagram ( char * data, Q_LONGLONG maxSize, QHostAddress * address = 0, Q_UINT16 * port = 0 )
- Q_LONGLONG writeDatagram ( const char * data, Q_LONGLONG size, const QHostAddress & address, Q_UINT16 port )
- Q_LONGLONG writeDatagram ( const QByteArray & datagram, const QHostAddress & host, Q_UINT16 port )

- 30 public functions inherited from QAbstractSocket
- 24 public functions inherited from QObject
- 36 public functions inherited from QIODevice

### Public Slots

- 1 public slot inherited from QObject

### Signals

- 8 signals inherited from QAbstractSocket
- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- 9 protected functions inherited from QObject
- 2 protected functions inherited from QIODevice

## Detailed Description

The 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. 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() to transfer data.

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() to read it.

Example:

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

            connect(udpSocket, SIGNAL(readyRead()),
                    this, SLOT(readPendingDatagrams()));
        }

        void Server::readPendingDatagrams()
        {
            while (udpSocket->hasPendingDatagrams()) {
                QByteArray datagram;
                datagram.resize(udpSocket->pendingDatagramSize());
                QHostAddress sender;
                Q_UINT16 senderPort;

                udpSocket->readDatagram(datagram.data(), datagram.size(),
                                        &sender, &senderPort);

                processTheDatagram(datagram);
            }
        }

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 network/broadcastclient and network/broadcastserver examples illustrate how to use QUdpSocket in applications.

See also QTcpSocket.

## Member Function Documentation

### QUdpSocket::QUdpSocket ( QObject * parent = 0 )

Creates a QUdpSocket object.

parent is passed to the QObject constructor.

See also socketType().

### QUdpSocket::~QUdpSocket () `[virtual]`

Destroys the socket, closing the connection if necessary.

See also close().

### bool QUdpSocket::bind ( const QHostAddress & address, Q_UINT16 port )

Binds this socket to the address address and the port port. When bound, the signal 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 Qt::BoundState; otherwise it returns false.

See also readDatagram().

### bool QUdpSocket::bind ( Q_UINT16 port = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Binds to QHostAddress:Any on port port.

### bool QUdpSocket::hasPendingDatagrams () const

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

See also pendingDatagramSize() and readDatagram().

### Q_LONGLONG QUdpSocket::pendingDatagramSize () const

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

See also hasPendingDatagrams() and readDatagram().

### Q_LONGLONG QUdpSocket::readDatagram ( char * data, Q_LONGLONG maxSize, QHostAddress * address = 0, Q_UINT16 * port = 0 )

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 pendingDatagramSize() to determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded.

See also writeDatagram(), hasPendingDatagrams(), and pendingDatagramSize().

### Q_LONGLONG QUdpSocket::writeDatagram ( const char * data, Q_LONGLONG size, const QHostAddress & address, Q_UINT16 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 socketError() will return Qt::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.

See also readDatagram().

### Q_LONGLONG QUdpSocket::writeDatagram ( const QByteArray & datagram, const QHostAddress & host, Q_UINT16 port )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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