2

好吧,这似乎很棘手。我实现了一个 QLocalServer 来做一些 IPC。但它不会正常工作。客户端连接并告诉我他已连接。服务器......嗯......不是,这让我无法收到任何消息。

服务器实现如下所示:

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()...的连接,但这也返回错误...

[编辑]

我每 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
4

0 回答 0