QThread 中的 QTcpSocket
我有简单的服务器连接线程。当您调用函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要么你的代码破坏了进程堆栈,要么你的超时值不够。
Either your code ruined the process stack, or your timeout value is not enough.
返回值或
QTcpSocket::errorString()
是什么?请注意,在运行中,即使
QTcpSocket::waitForReadyRead
返回 false,您始终会读取套接字上的数据。您确定在
run
中不会遇到与receiveString
中相同的错误,但读取成功是因为您忽略了此错误?如果您使用以下代码,
run
是否仍然成功:What is the return value or
QTcpSocket::errorString()
?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 inreceiveString
, but the read succeeds because you ignore this error?Is
run
still succeeding if you use this code: