Overview | Package | Class | Tree | Deprecated | Index | Help
Qt Jambi Home |
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.trolltech.qt.network
Class QUdpSocket

    java.lang.Object
      com.trolltech.qt.QSignalEmitter
          com.trolltech.qt.QtJambiObject
              com.trolltech.qt.core.QObject
                  com.trolltech.qt.core.QIODevice
                      com.trolltech.qt.network.QAbstractSocket
                          com.trolltech.qt.network.QUdpSocket

All Implemented Interfaces:
  QtJambiInterface

    public class QUdpSocket

extends QAbstractSocket

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. 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() 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;
                quint16 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 Broadcast Sender and Broadcast Receiver examples illustrate how to use `QUdpSocket` in applications.

See Also:
  `QTcpSocket`

Nested Class Summary
`static class` | `QUdpSocket.BindFlag` This enum describes the different flags you can pass to modify the behavior of QUdpSocket.:bind().
`static class` | `QUdpSocket.BindMode` This QFlag class provides flags for the int enum.
`static class` | `QUdpSocket.HostInfo`

Nested classes/interfaces inherited from class com.trolltech.qt.network.QAbstractSocket
`QAbstractSocket.NetworkLayerProtocol, QAbstractSocket.SocketError, QAbstractSocket.SocketState, QAbstractSocket.SocketType`

Nested classes/interfaces inherited from class com.trolltech.qt.core.QIODevice
`QIODevice.OpenMode, QIODevice.OpenModeFlag`

Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
`QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>`

Field Summary

Fields inherited from class com.trolltech.qt.network.QAbstractSocket
`connected, disconnected, error, hostFound, proxyAuthenticationRequired, stateChanged`

Fields inherited from class com.trolltech.qt.core.QIODevice
`aboutToClose, bytesWritten, readyRead`

Constructor Summary
`QUdpSocket()` Equivalent to `QUdpSocket`(0).
`QUdpSocket(QObject parent)` Creates a `QUdpSocket` object.

Method Summary
`boolean` | `bind(int port)` Binds to QHostAddress.Any on port `port`.
`boolean` | `bind(int port, QUdpSocket.BindMode mode)` Binds to QHostAddress.Any on port `port`, using the BindMode `mode`.
`boolean` | `bind(QHostAddress address, int port)` Binds this socket to the address `address` and the port `port`.
`boolean` | `bind(QHostAddress address, int port, QUdpSocket.BindMode mode)` Binds to `address` on port `port`, using the BindMode `mode`.
`static QUdpSocket` | `fromNativePointer(QNativePointer nativePointer)` This function returns the QUdpSocket instance pointed to by nativePointer
`boolean` | `hasPendingDatagrams()` Returns true if at least one datagram is waiting to be read; otherwise returns false.
`long` | `pendingDatagramSize()` Returns the size of the first pending UDP datagram.
`int` | `readDatagram(byte[] data, QUdpSocket.HostInfo info)` Receives a datagram from the host described by `info` and stores it in `data`.
`int` | `writeDatagram(byte[] data, QHostAddress address, int port)` Sends the datagram at `data` of size `size` to the host address `address` at port `port`.
`int` | `writeDatagram(QByteArray data, QHostAddress address, int port)` Sends the datagram `datagram` to the host address `host` and at port `port`.

Methods inherited from class com.trolltech.qt.network.QAbstractSocket
`abort, atEnd, bytesAvailable, bytesToWrite, canReadLine, close, connectToHost, connectToHost, connectToHostImplementation, disconnectFromHost, disconnectFromHostImplementation, error, flush, isSequential, isValid, localAddress, localPort, peerAddress, peerName, peerPort, proxy, readBufferSize, readData, readLineData, setLocalAddress, setLocalPort, setPeerAddress, setPeerName, setPeerPort, setProxy, setReadBufferSize, setSocketDescriptor, setSocketDescriptor, setSocketDescriptor, setSocketDescriptor, setSocketError, setSocketState, socketDescriptor, socketType, state, waitForBytesWritten, waitForConnected, waitForConnected, waitForDisconnected, waitForDisconnected, waitForReadyRead, writeData`

Methods inherited from class com.trolltech.qt.core.QIODevice
`errorString, getByte, isOpen, isReadable, isTextModeEnabled, isWritable, open, open, openMode, peek, peek, pos, putByte, read, read, readAll, readLine, readLine, readLine, reset, seek, setErrorString, setOpenMode, setOpenMode, setTextModeEnabled, size, ungetByte, write, write`

Methods inherited from class com.trolltech.qt.core.QObject
`blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent`

Methods inherited from class com.trolltech.qt.QtJambiObject
`dispose, disposed, finalize, reassignNativeResources, tr, tr, tr`

Methods inherited from class com.trolltech.qt.QSignalEmitter
`disconnect, disconnect, signalSender`

Methods inherited from class java.lang.Object
`clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Methods inherited from interface com.trolltech.qt.QtJambiInterface
`disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership`

Constructor Detail

### QUdpSocket

    public QUdpSocket()

  Equivalent to `QUdpSocket`(0).

### QUdpSocket

    public QUdpSocket(QObject parent)

  Creates a `QUdpSocket` object.

  `parent` is passed to the `QObject` constructor.

  See Also:
    `socketType`

Method Detail

### hasPendingDatagrams

    public final boolean hasPendingDatagrams()

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

  See Also:
    `pendingDatagramSize`, `readDatagram`

### pendingDatagramSize

    public final long pendingDatagramSize()

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

  See Also:
    `hasPendingDatagrams`, `readDatagram`

### fromNativePointer

    public static QUdpSocket fromNativePointer(QNativePointer nativePointer)

  This function returns the QUdpSocket instance pointed to by nativePointer

  Parameters:
    `nativePointer` - the QNativePointer of which object should be returned.

### bind

    public final boolean bind(QHostAddress address,
                              int 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 BoundState; otherwise it returns false.

  The socket is bound using the DefaultForPlatform BindMode.

### bind

    public final boolean bind(int port)

  Binds to QHostAddress.Any on port `port`.

### bind

    public final boolean bind(QHostAddress address,
                              int port,
                              QUdpSocket.BindMode mode)

  Binds to `address` on port `port`, using the BindMode `mode`.

### bind

    public final boolean bind(int port,
                              QUdpSocket.BindMode mode)

  Binds to QHostAddress.Any on port `port`, using the BindMode `mode`.

### readDatagram

    public final int readDatagram(byte[] data,
                                  QUdpSocket.HostInfo info)

  Receives a datagram from the host described by `info` and stores it in `data`. Returns the size of the datagram on success; otherwise returns -1.

### writeDatagram

    public int writeDatagram(byte[] data,
                             QHostAddress address,
                             int 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.

  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

    public int writeDatagram(QByteArray data,
                             QHostAddress address,
                             int port)

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

Overview | Package | Class | Tree | Deprecated | Index | Help
Qt Jambi Home |
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD