C++ Qt套接字未等待写入字节

C++ Qt套接字未等待写入字节,c++,qt,networking,tcp,C++,Qt,Networking,Tcp,我编写了一个chess客户端/服务器移动应用程序,并拥有一台远程服务器,该服务器已在西海岸、东海岸、中间等地进行了测试。该程序简而言之: 1. Log in to remote server with correct username/password via iOS/Android or Windows desktop. 2. Enter queue for a 1-minute, 5-minute or 30-minute game of chess. 3. Wait for anot

我编写了一个chess客户端/服务器移动应用程序,并拥有一台远程服务器,该服务器已在西海岸、东海岸、中间等地进行了测试。该程序简而言之:

1.  Log in to remote server with correct username/password via iOS/Android or Windows desktop.
2.  Enter queue for a 1-minute, 5-minute or 30-minute game of chess.
3.  Wait for another opponent to join queue.
4.  Get matched and play game of chess.
5.  When game is over, log out or play more chess.
然而,当我通过学校的互联网登录到服务器时,我遇到了最奇怪的错误。这太奇怪了,因为在我连接的众多ISP中,它是唯一有问题的ISP

当我通过学校的互联网登录到服务器时,我将从我的套接字中获得以下错误和错误字符串

QAbstractSocket::UnknownSocketError "Unknown error"
在我的应用程序中生成此项的步骤包括:

1. Enter username and password, log into server. (Successfully completes this).

2. Click to join a queue for a game of chess.  (Successfully writes to socket, but fails to wait for bytes written then emits the above error and error string.
我检查了服务器,甚至没有调用readyRead,因此我知道客户端没有向服务器发送任何内容

有趣的是,我找到了克服这个错误的方法

1.  Click on Settings page.
2.  Click Save. (Does the exact same thing as I try to do above.  Write to socket, flush and wait for bytes written).
3.  Join queue (Successfully joins queue).
这种解决方法没有任何意义,因为它与我在上面尝试的写入套接字、刷新和等待写入字节没有任何不同

有人对可能发生的事情有线索吗

为什么此错误特定于一个internet位置?我的学校网络速度非常慢,但无法解释为什么在下面的函数中插座会立即断开。 为什么我的变通方法有效? 我可以做些什么来了解更多关于我的问题,即愚蠢的错误消息…未知错误。 不仅如此,当我右键单击下面的函数,然后单击“查找用法”时,生成文件夹出现。这是程序中唯一执行此操作的函数。世界跆拳道联盟??? 此功能中的插座断开


出于某种原因,如果我在函数顶部调用它,一切都会正常工作

毫无意义。如果有人能解释为什么这样做,或者你有另一个解决方案,请发布它

//Temporary bug fix
this->sendUpdatedUserInfo();
我称之为使一切都能工作的函数


套接字是否确实成功连接?注意,connectToHost只是触发异步连接,一旦connectToHost返回,套接字就不会立即连接。还要检查write的返回值。是的,它确实连接了。write的返回值是68。我不知道那是什么意思。等等,我知道什么了吗?我猜68是它试图写入的字节数。m_socket->write方法返回一个qint64。我是否导致68字节而不是最大64字节的溢出?或者我在这里偏离了目标?如果你从写入中得到68,这意味着写入了68个字节。在这种情况下,flush返回false,因为没有要刷新的内容。字节已经写入。当flush返回false时,qDebug errorString;返回“未知错误”,因为在完全没有错误时也会返回此错误。qint64是64位整数。它可以容纳68个刚刚好;你的问题太让人困惑了,因为所有与你的国际象棋游戏等无关的参考资料。追踪Wireshark中的连接,看看到底发生了什么。我会做的。感谢您提供有关使用Wireshark的提示。
    Button
    {
        id: btn_oneMinuteGame
        text: "One Minute"
        style: cgButtonStyle

        Layout.alignment: Qt.AlignCenter
        Layout.preferredWidth: Lobby.getControlWidth()
        Layout.preferredHeight: Lobby.getControlHeight()

        onClicked:
        {
            ServerConnection.sendQueueType(1) // TODO : Magic numbers
            root.startOneMinuteGame()
        }
    }
//Temporary bug fix
this->sendUpdatedUserInfo();
void CG_serverConnection::sendUpdatedUserInfo()
{
    QJsonObject request;

    request["PacketHeader"] = UPDATE_INFO;
    request["loggedIn"]     = m_player.loggedIn;
    request["banned"]       = m_player.banned;
    request["username"]     = m_player.username;
    request["elo"]          = m_player.elo;
    request["countryFlag"]  = m_player.countryFlag;
    request["pieceSet"]     = m_player.pieceSet;
    request["language"]     = m_player.language;
    request["sound"]        = m_player.sound;
    request["coordinates"]  = m_player.coordinates;
    request["arrows"]       = m_player.arrows;
    request["autoPromote"]  = m_player.autoPromote;
    request["boardTheme"]   = m_player.boardTheme;

    QJsonDocument doc;
    doc.setObject(request);

    qDebug() << "Updated userInfo being sent: ";
    qDebug() << doc.toJson();

    m_socket->write(doc.toBinaryData());
    m_socket->flush();
    m_socket->waitForBytesWritten();
}