i've got the same problem.
receiver side code :
void Client::onSocketReadyRead()
{
bufferSocket += pSocket->readAll();
while (bufferSocket.size() >= 2)
{
quint16 size = (bufferSocket.at(0) << 8) + bufferSocket.at(1); // 16bit unsigned value
qDebug()<<"size="<<size;
if (size != 19998)
{
QMessageBox::critical(NULL, tr
("error"), tr
("This is it"));
pSocket->disconnectFromHost();
return;
}
if(bufferSocket.size() <= 2+size) return;
qDebug()<<"data read";
bufferSocket.remove(0,2+size);
}
void Client::onSocketReadyRead()
{
bufferSocket += pSocket->readAll();
while (bufferSocket.size() >= 2)
{
quint16 size = (bufferSocket.at(0) << 8) + bufferSocket.at(1); // 16bit unsigned value
qDebug()<<"size="<<size;
if (size != 19998)
{
QMessageBox::critical(NULL, tr("error"), tr("This is it"));
pSocket->disconnectFromHost();
return;
}
if(bufferSocket.size() <= 2+size) return;
qDebug()<<"data read";
QByteArray data = bufferSocket.mid(2, size);
bufferSocket.remove(0,2+size);
}
To copy to clipboard, switch view to plain text mode
sender side code:
void Server::onTimeout()
{
int i;
data.fill(1, 19998);
quint16 s = data.size();
ba[0] = (s >> 8) & 0xFF;
ba[1] = s & 0xFF;
ba.append(data);
for (i=0;i<vecClient.size();++i)
{
vecClient[i]->write(ba);
}
}
void Server::onTimeout()
{
QByteArray data;
int i;
data.fill(1, 19998);
quint16 s = data.size();
QByteArray ba(2, 0);
ba[0] = (s >> 8) & 0xFF;
ba[1] = s & 0xFF;
ba.append(data);
for (i=0;i<vecClient.size();++i)
{
vecClient[i]->write(ba);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks