|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.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
public class QUdpSocket
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.
Nested Class Summary | |
---|---|
static class |
QUdpSocket.BindFlag
This enum describes the different flags you can pass to modify the behavior of QUdpSocket. |
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.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 |
---|
public QUdpSocket()
Equivalent to QUdpSocket(0).
public QUdpSocket(QObject parent)
Creates a QUdpSocket object.
parent is passed to the QObject constructor.
Method Detail |
---|
public final boolean hasPendingDatagrams()
Returns true if at least one datagram is waiting to be read; otherwise returns false.
public final long pendingDatagramSize()
Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.
public static QUdpSocket fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.public final boolean bind(QHostAddress address, int port)
On success, the functions returns true and the socket enters BoundState; otherwise it returns false.
The socket is bound using the DefaultForPlatform BindMode.
public final boolean bind(int port)
public final boolean bind(QHostAddress address, int port, QUdpSocket.BindMode mode)
public final boolean bind(int port, QUdpSocket.BindMode mode)
public final int readDatagram(byte[] data, QUdpSocket.HostInfo info)
public int writeDatagram(byte[] data, QHostAddress address, int port)
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.
public int writeDatagram(QByteArray data, QHostAddress address, int port)
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |