The QLocalServer class provides a local socket based server. 更多...
| 头: | #include <QLocalServer> |
| Since: | Qt 4.4 |
| 继承: | QObject |
| 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) |
| void | newConnection () |
| bool | removeServer (const QString & name ) |
| virtual void | incomingConnection (quintptr socketDescriptor ) |
The QLocalServer class provides a local socket based server.
此类使接受传入本地套接字连接成为可能。
调用 listen () 让服务器按指定键开始监听传入连接。 newConnection () 信号然后被发射,每次客户端连接到服务器时。
调用 nextPendingConnection () 以接受待决连接作为已连接 QLocalSocket 。函数返回的指针指向 QLocalSocket 可以用于与客户端通信。
若出现错误, serverError () 返回错误的类型,且 errorString () 可以被调用,以获得发生什么的人类可读描述。
当监听连接时,可获得服务器正监听的名称透过 serverName ().
调用 close () 使 QLocalServer 停止监听传入连接。
尽管 QLocalServer 是为用于事件循环而设计的,没有事件循环使用它是可能的。在此情况下,必须使用 waitForNewConnection () 阻塞直到连接可用或超时到期。
另请参阅 QLocalSocket and QTcpServer .
创建新的本地套接字服务器,采用给定 parent .
另请参阅 listen ().
销毁 QLocalServer 对象。若服务器正在监听连接,它将自动关闭。
任何仍连接的客户端 QLocalSockets 必须断开连接,或重设父级在删除服务器之前。
另请参阅 close ().
Stop listening for incoming connections. Existing connections are not effected, but any new connections will be refused.
另请参阅 isListening () 和 listen ().
返回人类可读消息适合当前报告错误通过 serverError ()。若没有合适字符串可用,返回空字符串。
另请参阅 serverError ().
返回服务器正监听的完整路径。
注意:这特定于平台
另请参阅 listen () 和 serverName ().
[虚拟]
bool
QLocalServer::
hasPendingConnections
() const
Returns true if the server has a pending connection; otherwise returns false.
另请参阅 nextPendingConnection () 和 setMaxPendingConnections ().
[virtual protected]
void
QLocalServer::
incomingConnection
(
quintptr
socketDescriptor
)
此虚函数被调用由 QLocalServer 当新连接可用时。 socketDescriptor is the native socket descriptor for the accepted connection.
基实现创建 QLocalSocket ,设置套接字描述符,然后存储 QLocalSocket 在待决连接的内部列表中。最后 newConnection () 被发射。
重实现此函数以更改服务器行为,当连接可用时。
另请参阅 newConnection (), nextPendingConnection (),和 QLocalSocket::setSocketDescriptor ().
Returns true if the server is listening for incoming connections otherwise false.
告诉服务器去监听传入连接在 name 。若服务器目前正在监听,那么它会返回 false。在成功时返回 true,否则返回 false。
name 可以是单个名称且 QLocalServer 会确定正确的特定平台路径。 serverName () 将返回被传入 listen 的名称。
通常,只需传递像 foo 的名称,但在 Unix 也可以是如 /tmp/foo 的路径,而在 Windows 这可以是如 \\.\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.
另请参阅 serverName (), isListening (),和 close ().
返回最大待决已接受连接数。默认为 30。
另请参阅 setMaxPendingConnections () 和 hasPendingConnections ().
[signal]
void
QLocalServer::
newConnection
()
此信号被发射,每次有新连接可用时。
另请参阅 hasPendingConnections () 和 nextPendingConnection ().
[虚拟]
QLocalSocket
* QLocalServer::
nextPendingConnection
()
返回下一待决连接作为连接的 QLocalSocket 对象。
套接字是作为服务器子级创建的,意味着会自动删除它当 QLocalServer 对象被销毁。明确删除对象仍是好主意当这样处理时,以避免浪费内存。
返回 0 若在没有待决连接时调用此函数。
另请参阅 hasPendingConnections (), newConnection (),和 incomingConnection ().
[static]
bool
QLocalServer::
removeServer
(const
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.
在 Windows,此函数什么都不做;在 Unix,它移除套接字文件给定通过 name .
警告: 小心避免移除正运行实例的套接字。
该函数在 Qt 4.5 引入。
Returns the type of error that occurred last or NoError.
另请参阅 errorString ().
返回服务器名称若服务器正监听连接;否则返回 QString()
另请参阅 listen () 和 fullServerName ().
把最大待决接受连接数设为 numConnections . QLocalServer 将接受不超过 numConnections 传入连接先于 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 () 和 hasPendingConnections ().
等待最多 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.
这是阻塞函数调用。在单线程 GUI 应用程序中不建议使用它,由于整个应用程序将停止响应直到函数返回。waitForNewConnection() 最有用,当没有事件循环可用时。
非阻塞替代是连接到 newConnection () 信号。
若 msec 为 -1,此函数不会超时。
另请参阅 hasPendingConnections () 和 nextPendingConnection ().