首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QTcpSocket问题

QTcpSocket问题
EN

Stack Overflow用户
提问于 2011-05-27 02:42:15
回答 1查看 3.2K关注 0票数 2

使用Qt编写聊天。有个问题。我的客户机的QTcpSocket保持在连接状态,但是服务器发出newConnection()信号。不需要网络会话。为什么会这样呢?下面是一些代码:

代码语言:javascript
复制
ChatClient::ChatClient(QObject *parent)
    : QObject(parent) {
    tcpSocket = new QTcpSocket(this);
    QNetworkConfigurationManager manager;
    if (QNetworkConfigurationManager::NetworkSessionRequired
        & manager.capabilities()) {
        qDebug() << "Network session required";
    }
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(error(QAbstractSocket::SocketError)));
    connect(tcpSocket, SIGNAL(connected()),
            this, SLOT(requestForID()));
    connect(tcpSocket, SIGNAL(readyRead()),
            this, SLOT(receiveMessage()));
    tcpSocket->connectToHost("192.168.0.100", PORT);
}

void ChatClient::requestForID() {
    qDebug() << "Connected, requesting for ID";
    QByteArray segment;
    QDataStream out(&segment, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    out << (quint16)0 << ID;
    out.device()->seek(0);
    out << (quint16)(segment.size() - sizeof(quint16));
    tcpSocket->write(segment);
}

requestForID()从未被执行过

代码语言:javascript
复制
ChatServer::ChatServer(QObject *parent)
    : QObject(parent) {
    tcpServer = new QTcpServer(this);
    if (!tcpServer->listen(QHostAddress::Any, PORT)) {
        qDebug() << "Unable to start the server"
                 << tcpServer->errorString();
    }
    qDebug() << "Server port" << tcpServer->serverPort();
    connect(tcpServer, SIGNAL(newConnection()),
            this, SLOT(processConnection()));
}
void ChatServer::processConnection() {
    qDebug() << "Incoming connection";
    QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
    /*connect(clientSocket, SIGNAL(readyRead()),
            this, SLOT(readData()));
    readData(clientSocket);
    connect(clientSocket, SIGNAL(disconnected()),
            clientSocket, SLOT(deleteLater()));*/
    QByteArray segment;
    QDataStream out(&segment, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    out << (quint16)0 << (quint16)Message
        << "Successfully connected";
    out.device()->seek(0);
    out << (quint16)(segment.size() - sizeof(quint16));
    clientSocket->write(segment);
    clientSocket->disconnectFromHost();
}

服务器显示传入的连接,客户端不发出连接,保持在连接状态,也不接收服务器消息...有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-09 18:00:13

我也遇到了同样的问题,我找到了原因和解决方案。

不能在主窗口的contstructor中直接调用connectToHost。为什么?

原因是,主消息循环此时尚未运行。在内部,QAbstractSocketPrivate::fetchConnectionParameters()永远不会被调用,并且Qt套接字超时计时器认为永远不会建立连接。

解决方案是将其称为“延迟”,如下所示

代码语言:javascript
复制
QMetaObject::invokeMethod(this, "OnDelayedConnect", Qt::QueuedConnection);

或者在connectToHost之后调用waitForConnected()

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6143487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档