QLocalServer Class Reference

[ QtNetwork module]

QLocalServer 类提供基于本地套接字的服务器。 更多...

继承 QObject .

方法

Static Methods

Qt Signals


详细描述

QLocalServer 类提供基于本地套接字的服务器。

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

调用 listen () to have the server start listening for incoming connections on a specified key. newConnection () signal is then emitted each time a client connects to the 服务器。

调用 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.

若发生错误, 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 ().

调用 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.


方法文档编制

QLocalServer.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

创建新的本地套接字服务器,采用给定 parent .

另请参阅 listen ().

QLocalServer.close ( self )

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

另请参阅 isListening () 和 listen ().

QString QLocalServer.errorString ( self )

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

另请参阅 serverError ().

QString QLocalServer.fullServerName ( self )

Returns the full path that the server is listening on.

Note: This is platform specific

另请参阅 listen () and serverName ().

bool QLocalServer.hasPendingConnections ( self )

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

另请参阅 nextPendingConnection () and setMaxPendingConnections ().

QLocalServer.incomingConnection ( self , sip.voidptr  socketDescriptor )

此虚拟函数被调用由 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 () 是 emitted.

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

另请参阅 newConnection (), nextPendingConnection (), and QLocalSocket.setSocketDescriptor ().

bool QLocalServer.isListening ( self )

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

另请参阅 listen () and close ().

bool QLocalServer.listen ( self , QString  name )

告诉服务器去监听传入连接在 name . If the server is currently listening then it will return false. Return true on success otherwise false.

name 可以是单个名称且 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". For VxWorks following path must be always use "/comp/socket/0xNumber", where "0xNumber" is a string representation of a 16-bit number in hexadecimal format. Example "/comp/socket/0x00AA".

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 服务器。

另请参阅 serverName (), isListening (),和 close ().

int QLocalServer.maxPendingConnections ( self )

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

另请参阅 setMaxPendingConnections () and hasPendingConnections ().

QLocalSocket QLocalServer.nextPendingConnection ( self )

返回下一待决连接作为已连接 QLocalSocket 对象。

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.

另请参阅 hasPendingConnections (), newConnection (),和 incomingConnection ().

bool QLocalServer.removeServer (QString  name )

移除任何服务器实例可能导致调用 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 .

警告: Be careful to avoid removing sockets of running 实例。

该函数在 Qt 4.5 引入。

QAbstractSocket.SocketError QLocalServer.serverError ( self )

Returns the type of error that occurred last or NoError.

另请参阅 errorString ().

QString QLocalServer.serverName ( self )

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

另请参阅 listen () and fullServerName ().

QLocalServer.setMaxPendingConnections ( self , int  numConnections )

把最大待决接受连接数设为 numConnections . QLocalServer 将接受不超过 numConnections incoming connections before nextPendingConnection () 被调用。

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.

另请参阅 maxPendingConnections () and hasPendingConnections ().

(bool, bool  timedOut ) QLocalServer.waitForNewConnection ( self , int  msecs  = 0)

等待最多 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 () 信号。

若 msec 为 -1,此函数不会超时。

另请参阅 hasPendingConnections () and nextPendingConnection ().


Qt Signal Documentation

void newConnection ()

This is the default overload of this signal.

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

另请参阅 hasPendingConnections () and nextPendingConnection ().