Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QUdpSocket Class Reference

Inheritance diagram for QUdpSocket:

QAbstractSocket QIODevice QObject

List of all members.


Detailed Description

The QUdpSocket class provides a UDP socket.

network

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 {network/broadcastsender}{Broadcast Sender} and {network/broadcastreceiver}{Broadcast Receiver} examples illustrate how to use QUdpSocket in applications.

See also:
QTcpSocket

Definition at line 58 of file qudpsocket.h.


Public Types

enum  BindFlag { DefaultForPlatform = 0x0, ShareAddress = 0x1, DontShareAddress = 0x2, ReuseAddressHint = 0x4 }
enum  NetworkLayerProtocol { IPv4Protocol, IPv6Protocol, UnknownNetworkLayerProtocol = -1 }
enum  OpenModeFlag {
  NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly,
  Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020
}
enum  SocketError {
  ConnectionRefusedError, RemoteHostClosedError, HostNotFoundError, SocketAccessError,
  SocketResourceError, SocketTimeoutError, DatagramTooLargeError, NetworkError,
  AddressInUseError, SocketAddressNotAvailableError, UnsupportedSocketOperationError, UnfinishedSocketOperationError,
  ProxyAuthenticationRequiredError, UnknownSocketError = -1
}
enum  SocketState {
  UnconnectedState, HostLookupState, ConnectingState, ConnectedState,
  BoundState, ListeningState, ClosingState
}
enum  SocketType { TcpSocket, UdpSocket, UnknownSocketType = -1 }

Public Slots

void deleteLater ()

Signals

void aboutToClose ()
void bytesWritten (qint64 bytes)
void connected ()
void destroyed (QObject *=0)
void disconnected ()
void error (QAbstractSocket::SocketError)
void hostFound ()
void proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *authenticator)
void readyRead ()
void stateChanged (QAbstractSocket::SocketState)

Public Member Functions

void abort ()
bool atEnd () const
bool bind (quint16 port, BindMode mode)
bool bind (const QHostAddress &address, quint16 port, BindMode mode)
bool bind (quint16 port=0)
bool bind (const QHostAddress &address, quint16 port)
bool blockSignals (bool b)
qint64 bytesAvailable () const
qint64 bytesToWrite () const
bool canReadLine () const
const QObjectListchildren () const
void close ()
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
void connectToHost (const QHostAddress &address, quint16 port, OpenMode mode=ReadWrite)
void connectToHost (const QString &hostName, quint16 port, OpenMode mode=ReadWrite)
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void disconnectFromHost ()
void dumpObjectInfo ()
void dumpObjectTree ()
QList< QByteArraydynamicPropertyNames () const
SocketError error () const
QString errorString () const
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
template<typename T>
findChild (const QString &name=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &name=QString()) const
bool flush ()
bool getChar (char *c)
bool hasPendingDatagrams () const
bool inherits (const char *classname) const
void installEventFilter (QObject *)
bool isOpen () const
bool isReadable () const
bool isSequential () const
bool isTextModeEnabled () const
bool isValid () const
bool isWidgetType () const
bool isWritable () const
void killTimer (int id)
QHostAddress localAddress () const
quint16 localPort () const
void moveToThread (QThread *thread)
QString objectName () const
virtual bool open (OpenMode mode)
OpenMode openMode () const
QObjectparent () const
QByteArray peek (qint64 maxlen)
qint64 peek (char *data, qint64 maxlen)
QHostAddress peerAddress () const
QString peerName () const
quint16 peerPort () const
qint64 pendingDatagramSize () const
virtual qint64 pos () const
QVariant property (const char *name) const
QNetworkProxy proxy () const
bool putChar (char c)
 QUdpSocket (QObject *parent=0)
QByteArray read (qint64 maxlen)
qint64 read (char *data, qint64 maxlen)
QByteArray readAll ()
qint64 readBufferSize () const
qint64 readDatagram (char *data, qint64 maxlen, QHostAddress *host=0, quint16 *port=0)
QByteArray readLine (qint64 maxlen=0)
qint64 readLine (char *data, qint64 maxlen)
void removeEventFilter (QObject *)
virtual bool reset ()
virtual bool seek (qint64 pos)
void setObjectName (const QString &name)
void setParent (QObject *)
bool setProperty (const char *name, const QVariant &value)
void setProxy (const QNetworkProxy &networkProxy)
void setReadBufferSize (qint64 size)
bool setSocketDescriptor (int socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
void setTextModeEnabled (bool enabled)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
virtual qint64 size () const
int socketDescriptor () const
SocketType socketType () const
int startTimer (int interval)
SocketState state () const
QThreadthread () const
void ungetChar (char c)
QObjectUserData * userData (uint id) const
bool waitForBytesWritten (int msecs=30000)
bool waitForConnected (int msecs=30000)
bool waitForDisconnected (int msecs=30000)
bool waitForReadyRead (int msecs=30000)
qint64 write (const QByteArray &data)
qint64 write (const char *data, qint64 len)
qint64 writeDatagram (const QByteArray &datagram, const QHostAddress &host, quint16 port)
qint64 writeDatagram (const char *data, qint64 len, const QHostAddress &host, quint16 port)
virtual ~QUdpSocket ()

Static Public Member Functions

static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static uint registerUserData ()

Protected Slots

void connectToHostImplementation (const QString &hostName, quint16 port, OpenMode mode=ReadWrite)
void disconnectFromHostImplementation ()

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
qint64 readData (char *data, qint64 maxlen)
qint64 readLineData (char *data, qint64 maxlen)
int receivers (const char *signal) const
QObjectsender () const
void setErrorString (const QString &errorString)
void setLocalAddress (const QHostAddress &address)
void setLocalPort (quint16 port)
void setOpenMode (OpenMode openMode)
void setPeerAddress (const QHostAddress &address)
void setPeerName (const QString &name)
void setPeerPort (quint16 port)
void setSocketError (SocketError socketError)
void setSocketState (SocketState state)
virtual void timerEvent (QTimerEvent *)
qint64 writeData (const char *data, qint64 len)

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject

Properties

QString objectName
 the name of this object

Friends

class QApplication
class QApplicationPrivate
class QCoreApplication
class QCoreApplicationPrivate
struct QMetaObject
class QThreadData
class QWidget

Related Functions

(Note that these are not member functions.)

qFindChild (const QObject *obj, const QString &name=QString(), T dummy=0)
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QString &name=QString(), T dummy=0)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
 QObjectList
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index