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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QLocalServer Class Reference
[QtNetwork module]

The QLocalServer class provides a local socket based server. More...

 #include <QLocalServer>

This class is not part of the Qt GUI Framework Edition.

Inherits QObject.

This class was introduced in Qt 4.4.


Public Functions

QLocalServer ( QObject * parent = 0 )
~QLocalServer ()
void close ()
QString errorString () const
QString fullServerName () const
virtual bool hasPendingConnections () const
bool isListening () const
bool listen ( const QString & name )
int maxPendingConnections () const
virtual QLocalSocket * nextPendingConnection ()
QAbstractSocket::SocketError serverError () const
QString serverName () const
void setMaxPendingConnections ( int numConnections )
bool waitForNewConnection ( int msec = 0, bool * timedOut = 0 )
  • 29 public functions inherited from QObject

Signals

void newConnection ()

Static Public Members

bool removeServer ( const QString & name )
  • 5 static public members inherited from QObject

Protected Functions

virtual void incomingConnection ( quintptr socketDescriptor )
  • 7 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject

Detailed Description

The QLocalServer class provides a local socket based server.

This class makes it possible to accept incoming local socket connections.

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

Call nextPendingConnection() to accept the pending connection as a connected QLocalSocket. The function returns a pointer to a QLocalSocket that can be used 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 name which the server is listening on is available through serverName().

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

Although QLocalServer is 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 QLocalSocket and QTcpServer.


Member Function Documentation

QLocalServer::QLocalServer ( QObject * parent = 0 )

Create a new local socket server with the given parent.

See also listen().

QLocalServer::~QLocalServer ()

Destroys the QLocalServer object. If the server is listening for connections, it is automatically closed.

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

See also close().

void QLocalServer::close ()

Stop listening for incoming connections. Existing connections are not effected, but any new connections will be refused.

See also isListening() and listen().

QString QLocalServer::errorString () const

Returns the human-readable message appropriate to the current error reported by serverError(). If no suitable string is available, an empty string is returned.

See also serverError().

QString QLocalServer::fullServerName () const

Returns the full path that the server is listening on.

Note: This is platform specific

See also listen() and serverName().

bool QLocalServer::hasPendingConnections () const   [virtual]

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

See also nextPendingConnection() and setMaxPendingConnections().

void QLocalServer::incomingConnection ( quintptr socketDescriptor )   [virtual protected]

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

The base implementation creates a QLocalSocket, sets the socket descriptor and then stores the QLocalSocket 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.

See also newConnection(), nextPendingConnection(), and QLocalSocket::setSocketDescriptor().

bool QLocalServer::isListening () const

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

See also listen() and close().

bool QLocalServer::listen ( const QString & name )

Tells the server to listen for incoming connections on name. If the server is currently listening then it will return false. Return true on success otherwise false.

name can be a single name and QLocalServer will determine the correct platform specific path. serverName() will return the name that is passed into listen.

Usually you would just pass in a name like "foo", but on Unix this could also be a path such as "/tmp/foo" and on Windows this could be a pipe path such as "\\.\pipe\foo"

Note: On Unix if the server crashes without closing listen will fail with AddressInUseError. To create a new server the file should be removed. On Windows two local servers can listen to the same pipe at the same time, but any connections will go to one of the server.

See also serverName(), isListening(), and close().

int QLocalServer::maxPendingConnections () const

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

See also setMaxPendingConnections() and hasPendingConnections().

void QLocalServer::newConnection ()   [signal]

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

See also hasPendingConnections() and nextPendingConnection().

QLocalSocket * QLocalServer::nextPendingConnection ()   [virtual]

Returns the next pending connection as a connected QLocalSocket object.

The socket is created as a child of the server, which means that it is automatically deleted when the QLocalServer 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(), newConnection(), and incomingConnection().

bool QLocalServer::removeServer ( const QString & name )   [static]

Removes any server instance that might cause a call to listen() to fail and returns true if successful; otherwise returns false. This function is meant to recover from a crash, when the previous server instance has not been cleaned up.

On Windows, this function does nothing; on Unix, it removes the socket file given by name.

Warning: Be careful to avoid removing sockets of running instances.

This function was introduced in Qt 4.5.

QAbstractSocket::SocketError QLocalServer::serverError () const

Returns the type of error that occurred last or NoError.

See also errorString().

QString QLocalServer::serverName () const

Returns the server name if the server is listening for connections; otherwise returns QString()

See also listen() and fullServerName().

void QLocalServer::setMaxPendingConnections ( int numConnections )

Sets the maximum number of pending accepted connections to numConnections. QLocalServer will accept no more than numConnections incoming connections before nextPendingConnection() is called.

Note: Even though QLocalServer will stop accepting new connections after it has reached its maximum number of pending connections, the operating system may still keep them in queue which will result in clients signaling that it is connected.

See also maxPendingConnections() and hasPendingConnections().

bool QLocalServer::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 ill-advised 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 79
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. 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
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Developer Network au hasard

Logo

Les bases

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. 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.6
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