com.trolltech.qt.network
Class QUdpSocket

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.core.QIODevice
                  extended by com.trolltech.qt.network.QAbstractSocket
                      extended by 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.

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.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)
          This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind.
 boolean bind(int port, QUdpSocket.BindMode mode)
          This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind.
 boolean bind(QHostAddress address, int port)
          This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind.
 boolean bind(QHostAddress address, int port, QUdpSocket.BindMode mode)
          This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind.
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)
          This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.writeDatagram.
 int writeDatagram(QByteArray data, QHostAddress address, int port)
          This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.writeDatagram.
 
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)
This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind. address port


bind

public final boolean bind(int port)
This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind. port


bind

public final boolean bind(QHostAddress address,
                          int port,
                          QUdpSocket.BindMode mode)
This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind. address port mode


bind

public final boolean bind(int port,
                          QUdpSocket.BindMode mode)
This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.bind. port 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)
This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.writeDatagram. data address port


writeDatagram

public int writeDatagram(QByteArray data,
                         QHostAddress address,
                         int port)
This is a JambiDoclet generated javadoc comment for com.trolltech.qt.network.QUdpSocket.writeDatagram. data address port