Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

TCP SOCKET

$
0
0
I am basically following the code in the example fortuneserver and fortuneclient that comes with Qt . On the server side i have the following : QByteArray block ; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_0);   out << (quint16)0; out << dataStr; out.device()->seek(0); int temp = (block.size() - sizeof(quint16)); out << (quint16)(block.size() - sizeof(quint16));   connection->write(block); connection->waitForBytesWritten(); Now , i have several client connections in a list . So I loop through the list and for each connection , i send the QString in dataStr . Now if I have 2 clients connected to the server , the code appeared to be working great . When I added a 3rd client , sometimes one of the clients doesn’t receive the correct block size . On my client I have : QDataStream in(this->sock); in.setVersion(QDataStream::Qt_5_0);   if (blockSize == 0) {   if (this->sock->bytesAvailable() < (int)sizeof(quint16))    return;     in >> blockSize; }   if (this->sock->bytesAvailable() < blockSize)   return;   QString data; in >> data; This code is identical to the sample fortuneclient . On my server , i check that the data going out looks good . But for some reason , sometimes one of the clients is receiving bad data . The blocksize received is incorrect . And even though bytesavailable looks correct , no data goes into the QString data . What is the possible cause of this and how would i go about debugging it ?

Viewing all articles
Browse latest Browse all 18427

Trending Articles