服务器实现如下所示:
PipeServer::PipeServer(QString servername, QObject *parent) : QObject(parent) {
m_server = new QLocalServer(this);
m_server->setSocketOptions(QLocalServer::WorldAccessOption);
if (!m_server->listen(servername)) {
logToFile("Unable to start the Server");
} else {
logToFile("Server up and running");
}
connect(m_server, SIGNAL(newConnection()), this, SLOT(socket_new_connection()));
}
void PipeServer::socket_new_connection() {
logToFile("incoming connection");
/* handling of connection ... */
}
输出服务器:
[Debug]: Server up and running
输出客户端:
[Debug]: ConnectingState
[Debug]: ConnectedState
[Debug]: socket_connected
没有正在运行的服务器的输出客户端:
[Debug]: ConnectingState
[Debug]: UnconnectedState
[Debug]: socket_error
[Debug]: ServerNotFoundError
如果在连接客户端时关闭服务器,则客户端输出:
[Debug]: ClosingState
[Debug]: UnconnectedState
[Debug]: socket_disconnected
所以客户端肯定是连接到服务器,但从不调用服务器的newConnection()
信号。我甚至尝试用m_server->hasPendingConnections()
来检查自己的连接......但是这也会返回false ...
[编辑]
我每隔30秒检查服务器状态:
void PipeServer::checkListening(){
if(m_server->isListening()){
logToFile("server is listening");
} else {
logToFile("server is NOT listening");
}
if(m_server->hasPendingConnections()){
logToFile("server has pending connections");
} else {
logToFile("server has no pending connections");
}
}
输出:
[Debug]: server is listening
[Debug]: server has no pending connections