简体   繁体   English

Qt:使用 QTcpSocket -> 我可以在套接字上写入,但我无法读取...

[英]Qt: Using QTcpSocket -> I can write on socket, but I can't read...

I am using Qt 4.8 GCC 32bit on xUbuntu 14.04.我在 xUbuntu 14.04 上使用 Qt 4.8 GCC 32 位。
I have the following piece of code, a TCP server that I use in order to get some remote commands and send back some answers - via TCP socket:我有以下一段代码,一个 TCP 服务器,我使用它来获取一些远程命令并通过 TCP 套接字发回一些答案:

struct _MyRequest
{
    unsigned long   Request;
    unsigned long   Data;
} __attribute__((packed));

struct _MyAnswer
{
    unsigned long   Error;
    unsigned long   Filler;
} __attribute__((packed));

_MyRequest request;
_MyAnswer  answer;

RemoteCmdServer::RemoteCmdServer(QObject * parent)
    : QTcpServer(parent)
{
    qDebug() << "Server started";

    listen(QHostAddress("172.31.250.110"), 5004);

    connect(this, SIGNAL(newConnection()), this, SLOT(processPendingRequest()));
}

void RemoteCmdServer::processPendingRequest()
{
    qDebug() << "Process request";

    QTcpSocket * clientConnection = nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));

    // get the request
    int ret = clientConnection->read((char*)&request, sizeof(request));
    qDebug() << "READ: " << ret;
    if(ret == sizeof(request))
    {
        // send answer
        clientConnection->write((char*)&answer, sizeof(answer));
    }

    qDebug() << "Disconnecting...";
    clientConnection->disconnectFromHost();
}   

I am able to write correctly if I comment the if(ret == sizeof(request)) line.如果我评论if(ret == sizeof(request))行,我就能正确书写。
Yet, I can't read from the socket (I always get 0 bytes).但是,我无法从套接字读取(我总是得到 0 字节)。
I am 100% sure that the TCP-tool I use to send packets to my app works ok.我 100% 确定我用来向我的应用程序发送数据包的 TCP 工具工作正常。
Here is the debug output from my app:这是我的应用程序的调试输出:

Server started     
Process request    
READ: 0     
Disconnecting...     

What am I doing wrong?我究竟做错了什么? Please advise!请指教!

You're trying to read data from the new connection without returning to the Qt event loop -- I don't think that's going to work.您正在尝试从新连接读取数据而不返回 Qt 事件循环——我认为这行不通。

After you've accepted the connect with...在您接受与...的连接后

QTcpSocket * clientConnection = nextPendingConnection();

You need to connect to its readyRead signal with something like...你需要用类似的东西连接到它的readyRead信号......

connect(clientConnection, SIGNAL(readyRead()), this, SLOT(my_read_slot()));

Where my_read_slot is the member function that will actually perform the read operation.其中my_read_slot是将实际执行读取操作的成员函数。

You should wait for the data either in a non-blocking or blocking way.您应该以非阻塞或阻塞的方式等待数据。 You can use waitForReadyRead to do it in a blocking way.您可以使用waitForReadyRead以阻塞方式执行此操作。

void RemoteCmdServer::processPendingRequest()
{
    qDebug() << "Process request";

    QTcpSocket * clientConnection = nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));

    if (clientConnection->waitForReadyRead())
    {
        // get the request
        QByteArray message = clientConnection->readAll(); // Read message
        qDebug() << "Message:" << QString(message);
    }
    else
    {
        qDebug().nospace() << "ERROR: could not receive message (" << qPrintable(clientConnection->errorString()) << ")";
    }

    qDebug() << "Disconnecting...";
    clientConnection->disconnectFromHost();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM