QThread 中的 QTcpSocket

发布于 2024-08-07 10:45:35 字数 1448 浏览 7 评论 0原文

我有简单的服务器连接线程。当您调用函数 receiveString 时,它会失败。但是,当您在 run() 中执行相同的代码时,它会成功。函数 receiveString 需要什么才能工作?

我已经尝试过

bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
bool TestServerThread::receiveString(QTcpSocket* sock, QString& str)

实际代码:

TestServerThread::TestServerThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
{
}


bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
{
    if(sock.isValid())
    {
        if(!sock.waitForReadyRead(30))
        {
            qWarning() << "fail"; // fails here
            return false;
        }
        QByteArray buf = sock.readAll();
        str = buf;
    }
}

void TestServerThread::run()
{
    QTcpSocket sock;
    if (!sock.setSocketDescriptor(socketDescriptor)) {
        emit error(sock.error());
        return;
    }

    bool ok = true;
    while(ok)
    {
        QString str;
        //if(ok) ok = receiveString(sock, str);

        if(!sock.waitForReadyRead(30))
        {
            qWarning() << "false";
        }
        QByteArray buf = sock.readAll(); // same routine succeeds
        str = buf;
        qWarning() << str;

        qWarning() << "Received: " << str;
        if(ok) ok = sendString(sock, "kaka");
    }
    sock.disconnectFromHost();
    sock.waitForDisconnected();
}

I have simple server connection thread. When you call function receiveString, it fails. However when you execute same code in run(), it succeeds. What is needed for function receiveString to work?

I've tried both

bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
bool TestServerThread::receiveString(QTcpSocket* sock, QString& str)

Actual code:

TestServerThread::TestServerThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
{
}


bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
{
    if(sock.isValid())
    {
        if(!sock.waitForReadyRead(30))
        {
            qWarning() << "fail"; // fails here
            return false;
        }
        QByteArray buf = sock.readAll();
        str = buf;
    }
}

void TestServerThread::run()
{
    QTcpSocket sock;
    if (!sock.setSocketDescriptor(socketDescriptor)) {
        emit error(sock.error());
        return;
    }

    bool ok = true;
    while(ok)
    {
        QString str;
        //if(ok) ok = receiveString(sock, str);

        if(!sock.waitForReadyRead(30))
        {
            qWarning() << "false";
        }
        QByteArray buf = sock.readAll(); // same routine succeeds
        str = buf;
        qWarning() << str;

        qWarning() << "Received: " << str;
        if(ok) ok = sendString(sock, "kaka");
    }
    sock.disconnectFromHost();
    sock.waitForDisconnected();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-08-14 10:45:35

要么你的代码破坏了进程堆栈,要么你的超时值不够。

Either your code ruined the process stack, or your timeout value is not enough.

返回值或QTcpSocket::errorString()是什么?

//...
if(!sock.waitForReadyRead(30))
{
    qWarning() << "fails " << sock.errorString(); // fails here
    emit error(sock.error());
    return false;
}
//...

请注意,在运行中,即使 QTcpSocket::waitForReadyRead 返回 false,您始终会读取套接字上的数据。
您确定在 run 中不会遇到与 receiveString 中相同的错误,但读取成功是因为您忽略了此错误?

如果您使用以下代码,run 是否仍然成功:

bool ok = true;
while(ok)
{
    QString str;
    //if(ok) ok = receiveString(sock, str);

    if(sock.waitForReadyRead(30))
    {
        QByteArray buf = sock.readAll(); // same routine succeeds
        str = buf;
        qWarning() << str;
        qWarning() << "Received: " << str;
        if(ok) 
        {
            ok = sendString(sock, "kaka");
        }
    }
    else
    {
         qWarning() << "fails " << sock.errorString();
         ok = false;
    }
}

What is the return value or QTcpSocket::errorString()?

//...
if(!sock.waitForReadyRead(30))
{
    qWarning() << "fails " << sock.errorString(); // fails here
    emit error(sock.error());
    return false;
}
//...

Note that in the run, you are always reading the data on the socket, even if QTcpSocket::waitForReadyRead returned false.
Are you sure you don't get the same error in the run as in receiveString, but the read succeeds because you ignore this error?

Is run still succeeding if you use this code:

bool ok = true;
while(ok)
{
    QString str;
    //if(ok) ok = receiveString(sock, str);

    if(sock.waitForReadyRead(30))
    {
        QByteArray buf = sock.readAll(); // same routine succeeds
        str = buf;
        qWarning() << str;
        qWarning() << "Received: " << str;
        if(ok) 
        {
            ok = sendString(sock, "kaka");
        }
    }
    else
    {
         qWarning() << "fails " << sock.errorString();
         ok = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文