|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.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
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:
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.
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 BindModemode. |
boolean |
bind(int port,
QUdpSocket.BindMode mode)
Binds to address on port port, using the BindModemode. |
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 BindModemode. |
boolean |
bind(QHostAddress address,
int port,
QUdpSocket.BindMode mode)
Binds to address on port port, using the BindModemode. |
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.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 |
---|
public QUdpSocket()
parent is passed to the QObject
constructor.
socketType()
.
public QUdpSocket(QObject parent)
parent is passed to the QObject
constructor.
socketType()
.
Method Detail |
---|
public final boolean hasPendingDatagrams()
pendingDatagramSize()
, and readDatagram()
.
public final long pendingDatagramSize()
hasPendingDatagrams()
, and readDatagram()
.
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()
public final boolean bind(int port)
public final boolean bind(QHostAddress address, int port, QUdpSocket.BindFlag[] mode)
public final boolean bind(QHostAddress address, int port, QUdpSocket.BindMode mode)
public final boolean bind(int port, QUdpSocket.BindFlag[] mode)
public final boolean bind(int port, QUdpSocket.BindMode mode)
public final int readDatagram(byte[] data, QUdpSocket.HostInfo info)
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.
writeDatagram()
, hasPendingDatagrams()
, and pendingDatagramSize()
.
public final int readDatagram(byte[] data)
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.
writeDatagram()
, hasPendingDatagrams()
, and pendingDatagramSize()
.
public final 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.
readDatagram()
, and write()
.
public final int writeDatagram(QByteArray data, QHostAddress address, int port)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |