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
Bookmarks