No. 0xA7 is really (usually) 0x000000A7 on a 32 bit machine and it occupies four bytes.
I thought hexadecimal size is 4bits.
I Understand I have to cast PROTO_VERSION to unsigned char so I can make 1 byte, I make this changes:
void GuiClient::sendMsg()
{
out <<static_cast<unsigned char> (PROTO_VERSION)
<< (quint32)0 << static_cast<unsigned char>(MSG_TYPE) << ui->msgEdit->text().toAscii().trimmed();
out.device()->seek(1);
out << quint32(msg.size() - sizeof(quint32));
qDebug("%d", msg.size());
client->write(msg);
f.write(msg);
f.close();
}
ui->msgEdit->clear();
ui->msgEdit->setFocus();
}
void GuiClient::sendMsg()
{
QByteArray msg;
QDataStream out(&msg, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out <<static_cast<unsigned char> (PROTO_VERSION)
<< (quint32)0 << static_cast<unsigned char>(MSG_TYPE) << ui->msgEdit->text().toAscii().trimmed();
out.device()->seek(1);
out << quint32(msg.size() - sizeof(quint32));
qDebug("%d", msg.size());
client->write(msg);
QFile f("test0" + QString::number(++i) + ".bin");
if (f.open(QIODevice::WriteOnly)) {
f.write(msg);
f.close();
}
ui->msgEdit->clear();
ui->msgEdit->setFocus();
}
To copy to clipboard, switch view to plain text mode
void GuiServer::readMsg()
{
if(client) //user stored member variable, should be 0 if no client is connected
{
quint32 blockSize = 0;
while (client->bytesAvailable() < (int)sizeof(quint32) + 2) {
if (!client->waitForReadyRead(2000)) {
//emit error(client->error(), client->errorString());
return;
}
}
unsigned char proto, msg_type;
in >> proto;
in.device()->seek(1);
in >> blockSize;
in.device()->seek(1 + (int)sizeof(quint32));
in >> msg_type;
qDebug("protocol %X", proto);
qDebug("size %d", blockSize);
qDebug("message type %X", msg_type);
while (client->bytesAvailable() < blockSize) {
if (!client->waitForReadyRead(2000)) {
//emit error(socket.error(), socket.errorString());
return;
}
}
in.device()->seek(2 + (int)sizeof(quint32));
in >> msg;
ui->historyEdit->append(msg);
}
}
void GuiServer::readMsg()
{
if(client) //user stored member variable, should be 0 if no client is connected
{
QDataStream in(client);
quint32 blockSize = 0;
while (client->bytesAvailable() < (int)sizeof(quint32) + 2) {
if (!client->waitForReadyRead(2000)) {
//emit error(client->error(), client->errorString());
return;
}
}
unsigned char proto, msg_type;
in.setVersion(QDataStream::Qt_4_7);
in >> proto;
in.device()->seek(1);
in >> blockSize;
in.device()->seek(1 + (int)sizeof(quint32));
in >> msg_type;
qDebug("protocol %X", proto);
qDebug("size %d", blockSize);
qDebug("message type %X", msg_type);
while (client->bytesAvailable() < blockSize) {
if (!client->waitForReadyRead(2000)) {
//emit error(socket.error(), socket.errorString());
return;
}
}
QString msg;
in.device()->seek(2 + (int)sizeof(quint32));
in >> msg;
ui->historyEdit->append(msg);
}
}
To copy to clipboard, switch view to plain text mode
The dump show this:
$ hexdump -Cd test09.bin
00000000 a7 00 00 00 08 a8 00 00 00 02 2a 38 |..........*8|
0000000 00167 00000 43016 00000 00512 14378
000000c
$ hexdump -Cd test09.bin
00000000 a7 00 00 00 08 a8 00 00 00 02 2a 38 |..........*8|
0000000 00167 00000 43016 00000 00512 14378
000000c
To copy to clipboard, switch view to plain text mode
What I miss here?
Bookmarks