我正在使用c++开发一个websocket服务器(用winsock2 )。现在,我正在响应客户端的初始握手(在我的例子中是Chrome),然后我从客户端发送数据:
socket.send("Hello!");我正在试图解码数据帧,但我有一些问题。让我们看一看代码:
int GetBit(const char * data, unsigned int idx)
{
unsigned int arIdx = idx / 4;
unsigned int biIdx = idx % 4;
return (data[arIdx] >> biIdx) & 1;
}
void ParseDataFrame(const char * packet)
{
int FIN = GetBit(packet, 0);
unsigned char OPC = 0;
{
int opc0 = GetBit(packet, 4);
int opc1 = GetBit(packet, 5);
int opc2 = GetBit(packet, 6);
int opc3 = GetBit(packet, 7);
OPC |= (opc0 << 0);
OPC |= (opc1 << 1);
OPC |= (opc2 << 2);
OPC |= (opc3 << 3);
}
int MASK = GetBit(packet, 5);
}我得到了:
FIN = 1
OPC = x6 (can´t be)
MAKS = 0 (can´t be)我一直在阅读WS协议,也许问题在我的代码中。提前感谢!
编辑
我想提到的是,连接是正确建立的,因为控制台(chrome)中没有错误,并且调用了socket.onopen事件。
发布于 2017-08-31 23:16:18
您的数据看起来不错,是第一个字节(12月为-127,或0x81或1000 0001)。
使用GetBit读取时,您使用的是每字节4位,而不是8位。
biIdx当前从最右边的位开始,到最左边的位。这应该是另一回事:
int GetBit(const char * data, unsigned int idx)
{
unsigned int arIdx = idx / 8;
unsigned int biIdx = idx % 8;
return (data[arIdx] >> (7 - biIdx)) & 1;
}这应该能让你得到正确的信息。
对于MASK,您应该读取第8位。
https://stackoverflow.com/questions/45991015
复制相似问题