Overview | Package | Class | Tree | Deprecated | Index | Help
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.internal.QSignalEmitterInternal
          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:

        public class Server extends QObject {
        private QUdpSocket udpSocket;

            public void initSocket()
            {
                udpSocket = new QUdpSocket(this);
                udpSocket.bind(new QHostAddress(QHostAddress.SpecialAddress.LocalHost), 7755);

                udpSocket.readyRead.connect(this, "readPendingDatagrams()");
            }

            public void readPendingDatagrams()
            {
                while (udpSocket.hasPendingDatagrams()) {
                    byte[] datagram = new byte[(int)udpSocket.pendingDatagramSize()];
            QUdpSocket.HostInfo senderInfo = new QUdpSocket.HostInfo();

                    udpSocket.readDatagram(datagram, senderInfo);

                    // process the 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`
`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, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9`

Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal`

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, readChannelFinished, readyRead`

Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`currentSender`

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

Method Summary
`boolean` | `bind()`
`boolean` | `bind(int port)` Binds to QHostAddress.Any on port `port`.
`boolean` | `bind(int port, QUdpSocket.BindFlag[] mode)` Binds to `address` on port `port`, using the BindMode`mode`.
`boolean` | `bind(int port, QUdpSocket.BindMode mode)` Binds to `address` 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.BindFlag[] mode)` Binds to `address` on port `port`, using the BindMode`mode`.
`boolean` | `bind(QHostAddress address, int port, QUdpSocket.BindMode mode)` Binds to `address` on port `port`, using the BindMode`mode`.
`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)` Receives a datagram no larger than `maxSize` bytes and stores it in `data`.
`int` | `readDatagram(byte[] data, QUdpSocket.HostInfo info)` Receives a datagram no larger than `maxSize` bytes 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, connectToHost, connectToHost, connectToHost, connectToHost, connectToHost, connectToHost, disconnectFromHost, disconnectFromHostImplementation, error, flush, isValid, localAddress, localPort, peerAddress, peerName, peerPort, proxy, readBufferSize, setLocalAddress, setLocalPort, setPeerAddress, setPeerName, setPeerPort, setProxy, setReadBufferSize, setSocketDescriptor, setSocketDescriptor, setSocketDescriptor, setSocketDescriptor, setSocketError, setSocketState, socketDescriptor, socketType, state, waitForConnected, waitForConnected, waitForDisconnected, waitForDisconnected`

Methods inherited from class com.trolltech.qt.core.QIODevice
`atEnd, bytesAvailable, bytesToWrite, canReadLine, close, errorString, getByte, isOpen, isReadable, isSequential, isTextModeEnabled, isWritable, open, open, openMode, peek, peek, pos, putByte, read, read, readAll, readData, readLine, readLine, readLine, readLineData, reset, seek, setErrorString, setOpenMode, setOpenMode, setTextModeEnabled, size, ungetByte, waitForBytesWritten, waitForReadyRead, write, write, writeData`

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

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

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

Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`__qt_signalInitialization`

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

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

Constructor Detail

### QUdpSocket

    public QUdpSocket()

  Creates a QUdpSocket object.

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

See also:
  `socketType()`.

### 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()`, and `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()`, and `readDatagram()`.

### 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()

### bind

    public final boolean bind(int port)

  Binds to QHostAddress.Any on port `port`.

### bind

    public final boolean bind(QHostAddress address,
                              int port,
                              QUdpSocket.BindFlag[] mode)

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

### 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.BindFlag[] mode)

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

### bind

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

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

### readDatagram

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

  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()`.

### readDatagram

    public final int readDatagram(byte[] data)

  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()`.

### writeDatagram

    public final 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.

See also:
  `readDatagram()`, and `write()`.

### writeDatagram

    public final 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
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD