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  ·  Tous les espaces de nom  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QTcpServer Class Reference
[QtNetwork module]

The QTcpServer class provides a TCP-based server. More...

 #include <QTcpServer>

Inherits QObject.

Note: All the functions in this class are reentrant.

Public Functions

  • 29 public functions inherited from QObject

Signals

Protected Functions

  • 7 protected functions inherited from QObject

Additional Inherited Members

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

Detailed Description

The QTcpServer class provides a TCP-based server.

This class makes it possible to accept incoming TCP connections. You can specify the port or have QTcpServer pick one automatically. You can listen on a specific address or on all the machine's addresses.

Call listen() to have the server listen for incoming connections. The newConnection() signal is then emitted each time a client connects to the server.

Call nextPendingConnection() to accept the pending connection as a connected QTcpSocket. The function returns a pointer to a QTcpSocket in QAbstractSocket::ConnectedState that you can use for communicating with the client.

If an error occurs, serverError() returns the type of error, and errorString() can be called to get a human readable description of what happened.

When listening for connections, the address and port on which the server is listening are available as serverAddress() and serverPort().

Calling close() makes QTcpServer stop listening for incoming connections.

Although QTcpServer is mostly designed for use with an event loop, it's possible to use it without one. In that case, you must use waitForNewConnection(), which blocks until either a connection is available or a timeout expires.

See also QTcpSocket, Fortune Server Example, Threaded Fortune Server Example, Loopback Example, and Torrent Example.


Member Function Documentation

QTcpServer::QTcpServer ( QObject * parent = 0 )

Constructs a QTcpServer object.

parent is passed to the QObject constructor.

See also listen() and setSocketDescriptor().

QTcpServer::~QTcpServer ()   [virtual]

Destroys the QTcpServer object. If the server is listening for connections, the socket is automatically closed.

Any client QTcpSockets that are still connected must either disconnect or be reparented before the server is deleted.

See also close().

void QTcpServer::close ()

Closes the server. The server will no longer listen for incoming connections.

See also listen().

QString QTcpServer::errorString () const

Returns a human readable description of the last error that occurred.

See also serverError().

bool QTcpServer::hasPendingConnections () const   [virtual]

Returns true if the server has a pending connection; otherwise returns false.

See also nextPendingConnection() and setMaxPendingConnections().

void QTcpServer::incomingConnection ( int socketDescriptor )   [virtual protected]

This virtual function is called by QTcpServer when a new connection is available. The socketDescriptor argument is the native socket descriptor for the accepted connection.

The base implementation creates a QTcpSocket, sets the socket descriptor and then stores the QTcpSocket in an internal list of pending connections. Finally newConnection() is emitted.

Reimplement this function to alter the server's behavior when a connection is available.

If this server is using QNetworkProxy then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket::setSocketDescriptor().

See also newConnection() and nextPendingConnection().

bool QTcpServer::isListening () const

Returns true if the server is currently listening for incoming connections; otherwise returns false.

See also listen().

bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )

Tells the server to listen for incoming connections on address address and port port. If port is 0, a port is chosen automatically. If address is QHostAddress::Any, the server will listen on all network interfaces.

Returns true on success; otherwise returns false.

See also isListening().

int QTcpServer::maxPendingConnections () const

Returns the maximum number of pending accepted connections. The default is 30.

See also setMaxPendingConnections() and hasPendingConnections().

void QTcpServer::newConnection ()   [signal]

This signal is emitted every time a new connection is available.

See also hasPendingConnections() and nextPendingConnection().

QTcpSocket * QTcpServer::nextPendingConnection ()   [virtual]

Returns the next pending connection as a connected QTcpSocket object.

The socket is created as a child of the server, which means that it is automatically deleted when the QTcpServer object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.

0 is returned if this function is called when there are no pending connections.

See also hasPendingConnections().

QNetworkProxy QTcpServer::proxy () const

Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used.

This function was introduced in Qt 4.1.

See also setProxy() and QNetworkProxy.

QHostAddress QTcpServer::serverAddress () const

Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null.

See also serverPort() and listen().

QAbstractSocket::SocketError QTcpServer::serverError () const

Returns an error code for the last error that occurred.

See also errorString().

quint16 QTcpServer::serverPort () const

Returns the server's port if the server is listening for connections; otherwise returns 0.

See also serverAddress() and listen().

void QTcpServer::setMaxPendingConnections ( int numConnections )

Sets the maximum number of pending accepted connections to numConnections. QTcpServer will accept no more than numConnections incoming connections before nextPendingConnection() is called. By default, the limit is 30 pending connections.

Clients may still able to connect after the server has reached its maximum number of pending connections (i.e., QTcpSocket can still emit the connected() signal). QTcpServer will stop accepting the new connections, but the operating system may still keep them in queue.

See also maxPendingConnections() and hasPendingConnections().

void QTcpServer::setProxy ( const QNetworkProxy & networkProxy )

Sets the explicit network proxy for this socket to networkProxy.

To disable the use of a proxy for this socket, use the QNetworkProxy::NoProxy proxy type:

 server->setProxy(QNetworkProxy::NoProxy);

This function was introduced in Qt 4.1.

See also proxy() and QNetworkProxy.

bool QTcpServer::setSocketDescriptor ( int socketDescriptor )

Sets the socket descriptor this server should use when listening for incoming connections to socketDescriptor. Returns true if the socket is set successfully; otherwise returns false.

The socket is assumed to be in listening state.

See also socketDescriptor() and isListening().

int QTcpServer::socketDescriptor () const

Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.

If the server is using QNetworkProxy, the returned descriptor may not be usable with native socket functions.

See also setSocketDescriptor() and isListening().

bool QTcpServer::waitForNewConnection ( int msec = 0, bool * timedOut = 0 )

Waits for at most msec milliseconds or until an incoming connection is available. Returns true if a connection is available; otherwise returns false. If the operation timed out and timedOut is not 0, *timedOut will be set to true.

This is a blocking function call. Its use is disadvised in a single-threaded GUI application, since the whole application will stop responding until the function returns. waitForNewConnection() is mostly useful when there is no event loop available.

The non-blocking alternative is to connect to the newConnection() signal.

If msec is -1, this function will not time out.

See also hasPendingConnections() and nextPendingConnection().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 53
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  4. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  5. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  6. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard

Logo

Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.4
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