初めて、HostileForkが私の問題を説明するのを手伝ってくれたことに感謝したいと思います。ありがとうございます !
バイナリプロトコルを介してデータを送信するクライアントとサーバーを構築しようとしています。
私の問題は、QTクライアントからBoostサーバーにクラスを送信したいということです。私のヘッダー(私のクラスのサイズである1つの整数)がソケットに書き込んでいます。サーバー側でヘッダーを読み取りたい場合、適切な整数を取得できません(代わりに、-13050660のような大きな数値があります)。サーバーでのデシリアライズに問題があると思いますが、よくわかりません。
これは、私のQtクライアントコードが番号10をソケットに書き込むために使用する手法です。
QByteArray paquet;
QDataStream out(&paquet, QIODevice::WriteOnly);
out << (quint32) 0;
out.device()->seek(0);
out << (quint32) (10);
cout << "Writing " << sizeof(quint32) << " bytes to socket." << endl;
次に、boostのasync_read()を使用するサーバープロセスでそれを読み取ろうとします。
this->Iheader.resize(size, '\0'); // Iheader is a vector of char
async_read(
this->socket,
buffer(this->Iheader),
bind(
&Client::endRead,
cli,
placeholders::error,
placeholders::bytes_transferred)
);
文字列の結果を操作する関数は次のとおりです。
#ifdef WIN32
#define MYINT INT32
#include <Windows.h>
#else
#define MYINT int
#endif
void Client::endRead(const error_code& error, size_t nbytes)
{
if (!error && nbytes == sizeof(MYINT)) {
cout << "Read " << sizeof(MYINT) << " bytes from a socket." << endl;
istringstream stream(this->connection->getIheader(nbytes));
stream >> this->Isize;
cout << "Integer value read was " << this->Isize << endl;
} else {
cout << "Could not read " << sizeof(MYINT) << " bytes." << endl;
}
}
32ビットの符号付き整数(4バイト)を取得しますが、10ではなく、-1163005939のようなものです。誰かが持っていて、なぜこれが機能しないのか考えていますか?
サーバーとクライアントはどちらも、64ビットのWindows7proで起動しています。