chris15001900
11th October 2011, 19:38
Hi,
I'm developing a tcp-based server/client. I found a code to post a struct through tcp, but the data I get is looking weird. Here is what I have right now:
struct abc {
char a;
char b;
};
abc *a = (abc*)malloc(sizeof(struct abc));
a->a='1';
a->b = '2';
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (void*)a;
socket->write(block.data());
And here is the code to receive it:
char *data = (char*)this->socket->readAll().data();
QDataStream in(data);
in.setVersion(QDataStream::Qt_4_0);
QByteArray block;
in >> block;
abc *received;
received = (abc*)data;
And when I debug this data, there are many strange characters instead of chars... What can be wrong?
I'm developing a tcp-based server/client. I found a code to post a struct through tcp, but the data I get is looking weird. Here is what I have right now:
struct abc {
char a;
char b;
};
abc *a = (abc*)malloc(sizeof(struct abc));
a->a='1';
a->b = '2';
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (void*)a;
socket->write(block.data());
And here is the code to receive it:
char *data = (char*)this->socket->readAll().data();
QDataStream in(data);
in.setVersion(QDataStream::Qt_4_0);
QByteArray block;
in >> block;
abc *received;
received = (abc*)data;
And when I debug this data, there are many strange characters instead of chars... What can be wrong?