What does it have to do with anything?
This change the image of how hexadecimal represent it in 32-bit machine.
You are still using QDataStream, which is not what you really want.
Would you suggest a solution fit my need, I made this try. It looks send the formatted packet as I expect but I don't now how to parse it correctly in other side:
void GuiClient::sendMsg()
{
int n = ui->msgEdit->text().toAscii().trimmed().size();
msg.append(static_cast<unsigned char> (PROTO_VERSION));
for(int i2 = 0; i2 != sizeof(n); ++i2)
{
msg.append((char)(n&(0xFF << i2) >>i2));
}
msg.append(static_cast<unsigned char>(MSG_TYPE));
msg.append(ui->msgEdit->text().toAscii().trimmed());
qDebug("%d", msg.size());
qDebug("%s", msg.data());
client->write(msg);
f.write(msg);
f.close();
}
ui->msgEdit->clear();
ui->msgEdit->setFocus();
}
void GuiClient::sendMsg()
{
QByteArray msg;
int n = ui->msgEdit->text().toAscii().trimmed().size();
msg.append(static_cast<unsigned char> (PROTO_VERSION));
for(int i2 = 0; i2 != sizeof(n); ++i2)
{
msg.append((char)(n&(0xFF << i2) >>i2));
}
msg.append(static_cast<unsigned char>(MSG_TYPE));
msg.append(ui->msgEdit->text().toAscii().trimmed());
qDebug("%d", msg.size());
qDebug("%s", msg.data());
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
{
int blockSize = 0;
while (client->bytesAvailable() < (int)sizeof(int) + 2) {
if (!client->waitForReadyRead(2000)) {
//emit error(client->error(), client->errorString());
return;
}
}
array2[0] = array[1];
array2[1] = array[2];
array2[2] = array[3];
array2[3] = array[4];
memcpy(&blockSize, array2, sizeof(int));
int i = 0;
f.write(array);
f.close();
}
qDebug("msg %s", array.data());
qDebug("protocol %s", array.data()[0]);
qDebug("size %d", blockSize);
qDebug("message type %s", array.data()[5]);
while (client->bytesAvailable() < blockSize) {
if (!client->waitForReadyRead(2000)) {
//emit error(socket.error(), socket.errorString());
return;
}
}
ui->historyEdit->append(msg);
}
}
void GuiServer::readMsg()
{
if(client) //user stored member variable, should be 0 if no client is connected
{
int blockSize = 0;
while (client->bytesAvailable() < (int)sizeof(int) + 2) {
if (!client->waitForReadyRead(2000)) {
//emit error(client->error(), client->errorString());
return;
}
}
QByteArray array(client->read(6));
QByteArray array2;
array2[0] = array[1];
array2[1] = array[2];
array2[2] = array[3];
array2[3] = array[4];
memcpy(&blockSize, array2, sizeof(int));
int i = 0;
QFile f("test0" + QString::number(++i) + ".bin");
if (f.open(QIODevice::WriteOnly)) {
f.write(array);
f.close();
}
qDebug("msg %s", array.data());
qDebug("protocol %s", array.data()[0]);
qDebug("size %d", blockSize);
qDebug("message type %s", array.data()[5]);
while (client->bytesAvailable() < blockSize) {
if (!client->waitForReadyRead(2000)) {
//emit error(socket.error(), socket.errorString());
return;
}
}
QString msg;
ui->historyEdit->append(msg);
}
}
To copy to clipboard, switch view to plain text mode
Hexdump shows:
Client side:
$ hexdump -Cb test01.bin
00000000 a7 05 05 05 05 a8 65 72 74 |......ert|
0000000 247 005 005 005 005 250 145 162 164
0000009
00000000 a7 05 05 05 05 a8 65 72 74 |......ert|
0000000 247 005 005 005 005 250 145 162 164
0000009
To copy to clipboard, switch view to plain text mode
Server side:
00000000 a7 05 05 05 05 a8 |......|
0000000 247 005 005 005 005 250
0000006
00000000 a7 05 05 05 05 a8 |......|
0000000 247 005 005 005 005 250
0000006
To copy to clipboard, switch view to plain text mode
It seems like the server can read the size of packet but it crashes when it tries to read data.
I am in this project since week(trying to implement my protocol), google tired from me, and I am not sleep well because this(its like nightmare).
Bookmarks