Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QUdpSocket Class

The QUdpSocket class provides a UDP socket. More...

 #include <QUdpSocket>

Inherits: QAbstractSocket.

Note: All functions in this class are reentrant.

Public Types

flags OpenMode
enum OpenModeFlag { NotOpen, ReadOnly, WriteOnly, ReadWrite, ..., Unbuffered }

Public Functions

QIODevice()
QIODevice(QObject * parent)
QUdpSocket(QObject * parent = 0)
virtual ~QIODevice()
virtual ~QUdpSocket()
virtual bool atEnd() const
virtual qint64 bytesAvailable() const
virtual qint64 bytesToWrite() const
virtual bool canReadLine() const
virtual void close()
QString errorString() const
bool getChar(char * c)
bool hasPendingDatagrams() const
bool isOpen() const
bool isReadable() const
virtual bool isSequential() const
bool isTextModeEnabled() const
bool isWritable() const
bool joinMulticastGroup(const QHostAddress & groupAddress)
bool joinMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & iface)
bool leaveMulticastGroup(const QHostAddress & groupAddress)
bool leaveMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & iface)
QNetworkInterface multicastInterface() const
virtual bool open(OpenMode mode)
OpenMode openMode() const
qint64 peek(char * data, qint64 maxSize)
QByteArray peek(qint64 maxSize)
qint64 pendingDatagramSize() const
virtual qint64 pos() const
bool putChar(char c)
qint64 read(char * data, qint64 maxSize)
QByteArray read(qint64 maxSize)
QByteArray readAll()
qint64 readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)
qint64 readLine(char * data, qint64 maxSize)
QByteArray readLine(qint64 maxSize = 0)
virtual bool reset()
virtual bool seek(qint64 pos)
void setMulticastInterface(const QNetworkInterface & iface)
void setTextModeEnabled(bool enabled)
virtual qint64 size() const
void ungetChar(char c)
virtual bool waitForBytesWritten(int msecs)
virtual bool waitForReadyRead(int msecs)
qint64 write(const char * data, qint64 maxSize)
qint64 write(const char * data)
qint64 write(const QByteArray & byteArray)
qint64 writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)
qint64 writeDatagram(const QByteArray & datagram, const QHostAddress & host, quint16 port)

Signals

void aboutToClose()
void bytesWritten(qint64 bytes)
void readChannelFinished()
void readyRead()

Protected Functions

virtual qint64 readData(char * data, qint64 maxSize) = 0
virtual qint64 readLineData(char * data, qint64 maxSize)
void setErrorString(const QString & str)
void setOpenMode(OpenMode openMode)
virtual qint64 writeData(const char * data, qint64 maxSize) = 0

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 11 static public members inherited from QObject

Detailed Description

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.

Note: An incoming datagram should be read when you receive the readyRead() signal, otherwise this signal will not be emitted for the next datagram.

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);
     }
 }

QUdpSocket also supports UDP multicast. Use joinMulticastGroup() and leaveMulticastGroup() to control group membership, and QAbstractSocket::MulticastTtlOption and QAbstractSocket::MulticastLoopbackOption to set the TTL and loopback socket options. Use setMulticastInterface() to control the outgoing interface for multicast datagrams, and multicastInterface() to query it.

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, Broadcast Receiver, Multicast Sender, and Multicast Receiver examples illustrate how to use QUdpSocket in applications.

See also QTcpSocket.

Member Function Documentation

QUdpSocket::QUdpSocket(QObject * parent = 0)

Creates a QUdpSocket object.

parent is passed to the QObject constructor.

See also socketType().

QUdpSocket::~QUdpSocket() [virtual]

Destroys the socket, closing the connection if necessary.

See also close().

bool QUdpSocket::hasPendingDatagrams() const

Returns true if at least one datagram is waiting to be read; otherwise returns false.

See also pendingDatagramSize() and readDatagram().

bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress)

Joins the the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState, otherwise an error occurs.

Note that if you are attempting to join an IPv4 group, your socket must not be bound using IPv6 (or in dual mode, using QHostAddress::Any). You must use QHostAddress::AnyIPv4 instead.

This function returns true if successful; otherwise it returns false and sets the socket error accordingly.

This function was introduced in Qt 4.8.

See also leaveMulticastGroup().

bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & iface)

This is an overloaded function.

Joins the multicast group address groupAddress on the interface iface.

This function was introduced in Qt 4.8.

See also leaveMulticastGroup().

bool QUdpSocket::leaveMulticastGroup(const QHostAddress & groupAddress)

Leaves the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState, otherwise an error occurs.

This function returns true if successful; otherwise it returns false and sets the socket error accordingly.

This function was introduced in Qt 4.8.

See also joinMulticastGroup().

bool QUdpSocket::leaveMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & iface)

This is an overloaded function.

Leaves the multicast group specified by groupAddress on the interface iface.

This function was introduced in Qt 4.8.

See also joinMulticastGroup().

QNetworkInterface QUdpSocket::multicastInterface() const

Returns the interface for the outgoing interface for multicast datagrams. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. If no interface has been previously set, this function returns an invalid QNetworkInterface. The socket must be in BoundState, otherwise an invalid QNetworkInterface is returned.

This function was introduced in Qt 4.8.

See also setMulticastInterface().

qint64 QUdpSocket::pendingDatagramSize() const

Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.

See also hasPendingDatagrams() and readDatagram().

qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)

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

void QUdpSocket::setMulticastInterface(const QNetworkInterface & iface)

Sets the outgoing interface for multicast datagrams to the interface iface. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. The socket must be in BoundState, otherwise this function does nothing.

This function was introduced in Qt 4.8.

See also multicastInterface(), joinMulticastGroup(), and leaveMulticastGroup().

qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 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().

qint64 QUdpSocket::writeDatagram(const QByteArray & datagram, const QHostAddress & host, quint16 port)

This is an overloaded function.

Sends the datagram datagram to the host address host and at port port.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web