当通过网络发送数据时,为什么在两台机器上接收到不同的值? 我有一个客户机-服务器体系结构,客户端在C上的Windows上,服务器上的C++在Linux上。我目前正在通过网络发送一个整数(字节),服务器接收该整数,然后将其作为字节回显到客户端
我正在使用当通过网络发送数据时,为什么在两台机器上接收到不同的值? 我有一个客户机-服务器体系结构,客户端在C上的Windows上,服务器上的C++在Linux上。我目前正在通过网络发送一个整数(字节),服务器接收该整数,然后将其作为字节回显到客户端,c++,networking,C++,Networking,我正在使用 byte[] rotationBytes = new byte[4]; rotationBytes[0] = (byte) (rotation >> 24); rotationBytes[1] = (byte)(rotation >> 16); rotationBytes[2] = (byte)(rotation >> 8); rotationBytes[3] = (byte)(rotation); 在服务
byte[] rotationBytes = new byte[4];
rotationBytes[0] = (byte) (rotation >> 24);
rotationBytes[1] = (byte)(rotation >> 16);
rotationBytes[2] = (byte)(rotation >> 8);
rotationBytes[3] = (byte)(rotation);
在服务器上,它是使用
char data[4];
udp::endpoint senderEndpoint;
size_t length = udpSocket.receive_from(boost::asio::buffer(data, 4), senderEndpoint);
int rotation = (int)(data[0] << 24 |
data[1] << 16 |
data[2] << 8 |
data[3]);
有些数据是正确的,有些是不正确的。我做错什么了吗?似乎您的
字符已签名,因此在转换为int
时符号会扩展
使用无符号字符可能会更好
Send from C# front end: 45
C++ server receives: 45
Send from C# front end: 90
C++ server receives: 90
Send from C# front end: 135
C++ server receives: -121
Send from C# front end: 180
C++ server receives: -76
Send from C# front end: 225
C++ server receives: -31
Send from C# front end: 270
C++ server receives: 270