QTcpSocket does not read all the packages



  • I am trying to use cryptopp with QTcpSocket, what i do is encrypt a username on my client side, send it through a TCPSocket, and decrypt it server side. Here is my problem, QTcpSocket doesn't read all the message, as you can see server side i am expecting to receive several packets for one cipher, read method says the whole message was read but in my char*, i don't see it all really.... Here is my code sending the cipher :
    @
    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) ;
    @
    and my code to decrypt it (works fine client side without sending the message) :
    @
    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;
    }
    

    @



  • Not sure where the error is. Must be some problem with reading/writing number of bytes. Otherwise network layer will not eat up anything for sure.

    I have tried with some simple one again. It works like a charm. So try with simple example like the following and see it works.

    @Server -
    void MyServer::newservrequest(){
    qDebug() << "Connection request has come" <<endl;
    client = mServer->nextPendingConnection();

     QString crypted_mess = "Hello-pthinks.com";
     char size[5];
     sprintf(size,"%d{",crypted_mess.size());
     int written = 0;
     written = client->write(size);
     written = client->write(crypted_mess.toLocal8Bit(),crypted_mess.size()+ 1);
     client->waitForBytesWritten(crypted_mess.size());
     //string recovered = T.discryptor(crypted_mess) ;
    

    }

    Client -

    void MyClient::readmydata(){
    qDebug() << "Data has come " << endl;
    QTextStream stream(mClient);
    QString data;
    //stream >> data;
    char data1 = (char)malloc(100);
    mClient->read(data1,100);
    qDebug() << "Data =" << data1;
    }@



  • Thanks for answering my question, the thing is i already tried to push it as a QString but the transformation from string to QString makes in undecryptable, (i tried to declare a string, put it in a QString, decrypt it and Cryptopp will then throw an exeption)

    That's why i tried to avoid QString as much as possible.



  • Any chance you can use Qt on the server. You would just have to build against QCore and QNetwork which makes the whole executable pretty small. The advantage of this is you let Qt pass data over the wire and make the translation(int, double, etc)

    You can look at the Qt Example for QtcpServer but here is some code that more-or-less says the same
    @Server::write()
    {
    QByteArray block;
    QDataStream qout(&block, QIODevice::WriteOnly);
    qout << (quint32) 0;
    qout << (quint32) AppMessage::MessageType; // send enun
    qout << myMessageClass;
    qout.device()->seek(0);
    qout << (quint32) (block.size() - sizeof(quint32)); // send size
    qsocket->write(block);
    qsocket->flush();
    }
    @
    Then on the client side you have something like:

    @MyClient::readyReadSlot()
    {
    QDataStream in(socket);
    if (blockSize == 0) {
    quint32 bytesAvailable = socket->bytesAvailable();
    if (bytesAvailable < sizeof(quint32)) {
    qWarning() << "\tNot enough bytes to read quint32";
    return;
    }
    in >> blockSize;
    if(blockSize < 1) {
    qWarning() << "Invalid block size, as it is less then 1" << ;
    return;
    }

    }
    if (socket->bytesAvailable() < blockSize) {
        qApp->processEvents(QEventLoop::ExcludeSocketNotifiers,20);
        percentDownloaded = (int)     
                (((float)socket->bytesAvailable()/(float) blockSize) *100);
        emit retrievingData(percentDownloaded);
        return;
    }
    emit downloadCompleted();
    blockSize = 0;
     processData(in);
    

    }
    int MyClient:processData(QDataStream &in)
    {
    AppMessage::MessageType mt;
    in >> qui;
    mt = (AppMessage::MessageType) qui;
    switch (mt) {
    case AppMessage::ReplyInit:
    in >> myClass;
    }
    @

    Advantages to using Qt on the server side:

    Signals when socket disconnected

    Code reuse - Data sent between server & clients can be put in a share library, and by using the operators << & >> you minimize problem keeping code in sync

    Use Qt to make translation for sending things over the wire

    Just a suggestion, don't want to come off sounding like a know it all. Hope you don't take it that way.



  • I just used QString as on example. I have used char* as well. It works perfectly fine. My point is that somewhere data reading is not correct. I request you try with simple data passing with 1000 bytes or 2000 bytes and see how it goes. You should not face any problem. Based on this you check out the logic from your side.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.