Results 1 to 3 of 3

Thread: server-client problem

  1. #1
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default server-client problem

    i am making a program in which my compulsion is that the send & receive of data has to be completed at one go i.e unlike in normal server-client program, where server reads only when there is readyRead signal. my requirement is that when i press a button in UI, it will call a function in which first it will send data & then also immediately read data in same function. My code is

    Function in my client. It is called when user presses a button in GUI. It first sends data to server and then receive data from it
    void comn::sendMAC()
    {
    if(tcpSocket->isOpen())
    {
    //send MAC
    QByteArray arr;
    quint32 msg = calc_mac;
    qDebug() << "MAC: " << calc_mac;
    QString str = QVariant( calc_mac ).toString();
    str = QString::number( calc_mac, 16 );
    clienttextEdit->append( "MAC of the message: " + str );
    //clienttextEdit->append( "Sending MAC: " + calc_mac);
    QDataStream out(&arr, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_5);
    out << msg;
    qDebug() << "msg:" << msg;
    qDebug() << "created bytearray size:" << arr.size();
    qDebug() << "msg size:" << sizeof(quint32) << "(" << msg << ")";
    tcpSocket->write(arr);
    qDebug() << "bytes written";
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(clientstartRead()));

    //reading session key. Problem is here
    qDebug() << "bytes recieved";
    QByteArray buffer2;
    qDebug() << "avaliable on start" << tcpSocket->bytesAvailable();
    buffer2 = tcpSocket->readAll();
    qDebug() << "read to buffer" << buffer2.size();
    qDebug() << "availiable now" << tcpSocket->bytesAvailable();
    QDataStream in (buffer2);
    //in >> size; qDebug() << "size" << size;
    in >> bob_sessionkey; qDebug() << "msg" << bob_sessionkey;
    QString str2 = QVariant( bob_sessionkey ).toString();
    str2 = QString::number( bob_sessionkey, 16 );
    clienttextEdit->append( "Received session key: " + str2 );

    }
    }


    Function in server, which first receives data and then write back to client
    {
    //reading mac
    qDebug() << "bytes recieved";
    QByteArray buffer2;
    qDebug() << "avaliable on start" << client->bytesAvailable();
    buffer2 = client->readAll();
    qDebug() << "read to buffer" << buffer2.size();
    qDebug() << "availiable now" << client->bytesAvailable();
    QDataStream in (buffer2);
    //in >> size; qDebug() << "size" << size;
    in >> recd_mac; qDebug() << "msg" << recd_mac;
    QString str = QVariant( recd_mac ).toString();
    str = QString::number( recd_mac, 16 );
    servertextEdit->append( "Received MAC: " + str );

    //sending session key
    QByteArray arr;
    quint32 msg = sessionkey;
    qDebug() << "Sending session key: " << sessionkey;
    QString str2 = QVariant( sessionkey ).toString();
    str2 = QString::number( sessionkey, 16 );
    servertextEdit->append( "Sending session key: " + str2 );
    //clienttextEdit->append( "Sending MAC: " + calc_mac);
    QDataStream out(&arr, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_5);
    out << msg;
    qDebug() << "msg:" << msg;
    qDebug() << "created bytearray size:" << arr.size();
    qDebug() << "msg size:" << sizeof(quint32) << "(" << msg << ")";
    tcpSocket->write(arr);
    /*while(tcpSocket->bytesToWrite() > 0)
    {
    tcpSocket->waitForBytesWritten();
    }*/
    qDebug() << "bytes written";
    count++;
    break;
    }


    my problem is in client data. At server end its correctly receiving and sending data. But at client end, its able to send data but is not reading anything from server. The output of debug used in the function is below

    avaliable on start 0
    read to buffer 0
    availiable now 0
    msg 0


    Please help me. THANX

  2. #2
    Join Date
    May 2010
    Posts
    24
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: server-client problem

    To have blocking sockets, you should use the functions waitFor... instead of QObject::connect.
    For example, for the client code:
    Qt Code:
    1. tcpSocket->write(arr);
    2. bool ok = false;
    3. // Wait for data to be received and at least sizeof(sessionkey) bytes
    4. while((ok = tcpSocket->waitForReadyRead()) &&
    5. tcpSocket->bytesAvailable < sizeof(bob_sessionkey))
    6. ;
    7. if(ok)
    8. {
    9. QDataStream in(tcpSocket);
    10. in >> bob_sessionkey;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Since the server code is only sending 4 bytes, you should probably use QAbstractSocket::setSocketOption() with QAbstractSocket::LowDelayOption to be sure these bytes are sent right away and not waiting for enough data to fill a packet.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: server-client problem

    Please don't use blocking sockets, not without threading and especially not for 4 bytes.

Similar Threads

  1. tcp QT server - .Net c# client
    By soniaerm in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2010, 22:15
  2. TCP server-client app problem
    By pogostick in forum Newbie
    Replies: 6
    Last Post: 25th January 2010, 08:13
  3. Qt-- Client Server communication problem
    By thisismyuname in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 01:04
  4. password problem with client and server
    By mate in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2008, 18:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.