QTcpSocket没有读取所有内容

时间:2014-06-27 09:08:37

标签: qt sockets encryption

我正在尝试将cryptopp与QTcpSocket一起使用,我所做的是在客户端加密用户名,通过TCPSocket发送,并在服务器端解密。这是我的问题,QTcpSocket没有读取所有消息,因为你可以看到服务器端我希望收到一个密码的一个密码,读取方法说整个消息被读取但是在我的char *中,我不&# 39;真的看到了......这是我发送密码的代码:

char size[5];
sprintf(size,"%d{",crypted_mess.size());

int written = 0;
written = socket->write(size);
written = socket->write(crypted_mess.c_str(),crypted_mess.size()+ 1);
socket->waitForBytesWritten(crypted_mess.size());
string recovered = T.discryptor(crypted_mess) ;

和我的代码解密它(在没有发送消息的情况下工作正常的客户端):

    if(paquet_missing_size == 0) {
        current_message->clear();
        was_read = socket->read(start_message,1000);

        int message_start = 0;
        while(start_message[message_start]!= '{')
            message_start++;

        char *size_total_string;
        size_total_string = (char*)malloc (message_start * sizeof (char));

        for(int j = 0 ;j < message_start;j++  ){
            size_total_string[j] = start_message[j];
        }

        size_total_string[message_start] = '\0' ;
        paquet_missing_size = atoi(size_total_string);
        memcpy(read,start_message+message_start+1,was_read - message_start);
    }
    else
        was_read = socket->read(read,1000);

    // add the read thing to the current_message
    if(was_read <= paquet_missing_size) {
        current_message->append(read, was_read);
        paquet_missing_size -= was_read;
    } else {
        current_message->append(read, paquet_missing_size);
        paquet_missing_size = 0;
    }

1 个答案:

答案 0 :(得分:1)

为此目的,您应该始终使用带有QDataStream的QByteArray,以便先传递消息的大小,然后传递消息。

希望得到这个帮助。