阅读QTcpSocket

时间:2012-05-04 06:56:46

标签: c++ qt sockets qtcpsocket

我有一个与UNIX C ++套接字的套接字连接,在连接之后,我有一个循环来逐字节读取,直到我有完整的消息。我知道我要接收的消息的前两个字节,以及它的长度(15个字节)。所以函数看起来像:

bool mastControl::findPacket(int sockfd, st_messageMastToPc * messageReceived, bool * connected) {

    int n = 0;
    bool messageFound = false;
    char * buffer = (char *) messageReceived;

    unsigned int pos = 0;

    while ( ((n = read(sockfd, &(buffer[pos]), 1)) > 0) and not messageFound) {

         if (n == 1) {
            pos++;

            if ( (pos == 1) && (buffer[0] == 0x02)) { // First byte to receive
                std::cout << "INFO - Rcv1" << std::endl;
            } else if ( (pos == 2) && (buffer[1] == 0x33) ) { // Second byte
                std::cout << "INFO - Rcv2" << std::endl;
            } else if (pos >= uiMessageMastToPcSize) { // Full msg received
                messageFound = true;
                std::cout << "INFO - Complete message received" << std::endl;
            } else if (pos <= 2) { // Wrong values for the first 2 bytes
                std::cout << "WARN - Reseting (byte " << pos << " -> " << int(pos) << ")" << std::endl;
                pos = 0;
            }
        }
    }

    if (n < 0){
        //EROR
        *connected = false;
    }

    return messageFound;
}

现在我正在使用QTcpSockets实现相同的功能。建立连接,然后我打电话:

if(socket->waitForReadyRead(Global::tiempoMaximoDeEsperaParaRecibirDatosMastil)){
                /* Read socket to find a valid packet */
                if (findPacket(socket, &messageReceived)) {
                    qDebug()<<"New packet found!";
//...
}
}

所以我等到有一些信息可以读取,然后调用findPacket,现在几乎一样,逐字节读取:

bool mastControl::findPacket(QTcpSocket *socket, st_messageMastToPc * messageReceived) {
    int n = 0;
    bool messageFound = false;
    char * buffer = (char *) messageReceived;
    unsigned int pos = 0;

    while ( ((n = socket->read(&(buffer[pos]), 1)) >= 0) and not messageFound) {
        if (n == 1) {
            qDebug()<<"Recibido: "<<buffer[pos]<<", en pos: "<<pos;
            pos++;
            if ( (pos == 1) && (buffer[0] == 0x022)) {
                qDebug()<<"0x02 in first position";
//                std::cout << "INFO - Rcv1" << std::endl;
            } else if ( (pos == 2) && (buffer[1] == 0x33) ) {
                qDebug()<<"0x33 in second";
                std::cout << "INFO - Rcv2" << std::endl;
            } else if (pos >= uiMessageMastToPcSize) {
                messageFound = true;
                std::cout << "INFO - Complete message received" << std::endl;
            } else if (pos <= 2) {
                std::cout << "WARN - Reseting (byte " << pos << " -> " << int(pos) << ")" << std::endl;
                pos = 0;
            }
        }
    }

    if (n < 0){
      qDebug()<< "Disconnected. Reason: " << socket->errorString();
    }

    return messageFound;
}

看起来很不一样,但它不起作用。一旦我等待waitForReadyRead,我进入findPacket的循环,我能够读取收到的前4个字节。之后,不再收到数据。它仍然在findPacket的循环检查中再次出现,并且再一次,但是read函数总是返回0字节读取。没有收到新的信息。这是不可能的,因为服务器每隔几毫秒发送一次相同的数据包,所以即使我丢失了一些数据,最终我应该读一些东西。

那么,我做错了什么?我应该以不同的方式等待吗?我应该再次等待第一次读取函数返回0字节读取?这个读取函数和C ++库之间的区别是什么?

1 个答案:

答案 0 :(得分:0)

最后,问题是尝试一直读取而不是在读取函数返回的第一个零之后等待新数据。这样做就像一个魅力!