C++ Qt客户端向boost asio服务器发送结构数据
当我的客户机向我的服务器发送struct数据时,我遇到了一个问题。 我的客户端使用Qt tcp,服务器使用boost.asio。在我的服务器端,我可以接收客户端发送的缓冲区数据,但当我将数据强制转换为struct data时,会得到一个无法读取的struct data 这是有问题的结构数据:C++ Qt客户端向boost asio服务器发送结构数据,c++,qt,boost,boost-asio,qtnetwork,C++,Qt,Boost,Boost Asio,Qtnetwork,当我的客户机向我的服务器发送struct数据时,我遇到了一个问题。 我的客户端使用Qt tcp,服务器使用boost.asio。在我的服务器端,我可以接收客户端发送的缓冲区数据,但当我将数据强制转换为struct data时,会得到一个无法读取的struct data 这是有问题的结构数据: struct Protocole { int type; char infos[1024]; } 这是我的服务器中读取客户端套接字上数据的代码: this->_socket.asyn
struct Protocole
{
int type;
char infos[1024];
}
这是我的服务器中读取客户端套接字上数据的代码:
this->_socket.async_read_some(boost::asio::buffer(_buffer), // _buffer is type of char[1024];
_strand.wrap(boost::bind(&ClientManager::HandleRead,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred))
);
在ClientManager::HandlerRead中:
ProtocoleCS *_proto; // this is the struct data i have to cast
_proto = static_cast<ProtocoleCS*>(static_cast<void*>(&_buffer));
// I can read _proto
protocoles*_proto;//这是我必须转换的结构数据
_proto=静态_cast(静态_cast(&_buffer));
//我会读原版
这是我的客户端中用于发送结构数据的代码:
void Network::SendMsgToServer()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
Protocole proto;
proto.type = 1;
std::cout << " i am sending a message" << std::endl;
proto._infos[0] = 'H';
proto._infos[1] = 'E';
proto._infos[2] = 'L';
proto._infos[3] = 'L';
proto._infos[4] = 'O';
proto._id[5] = '\0';
out << static_cast<char*>(static_cast<void*>(&proto));
this->socket->write(block);
}
void网络::SendMsgToServer()
{
QByteArray区块;
QDataStream out(块和QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
原生质体;
原型=1;
std::coutQDataStream运算符
// If you want to avoid endianness swapping on boost asio side
// and if both the server and the client use the same endianness
out.setByteOrder(QDataStream::ByteOrder(QSysInfo::ByteOrder));
out << proto.type;
out.writeRawData(proto.infos, sizeof(proto.infos));