websocket傳送接收協議

2021-08-16 06:13:41 字數 1568 閱讀 4268

一.websocket

接收資料

1)固定位元組

(1000 0001

或1000 0010);   ---區分是否是資料報的乙個固定

位元組(佔1個位元組) 2)

包長度位元組,第

1位是1, 剩下

7為得到乙個整數

(0,127);125

以內的長度直接表示就可以了;

126表示後面兩個位元組表示大小

,127

表示後面的

8個位元組是資料的長度;

3)mark

掩碼為包長之後的

4 個位元組 4)

兄弟資料:

得到真實資料的方法:將兄弟資料的每一

位元組x,

和掩碼的第

i%4

位元組做

xor運算,其中

i是x

在兄弟資料中的索引

測試**:

void on_ws_recv_data(struct session* s,unsigned char* pkg_data,int pkg_len) 

else if (len == 126)

else if(len == 127)

mask = pkg_data + 2 + 8;

} //資料部分開始的指標

raw_data = mask + 4;

static unsigned char raw_recvbuf[4096] = ;

//還原發過來的資料

//資料從第9個位元組開始與對應的掩碼異或 /迴圈4個位元組掩碼異或

for (int i = 0; i < len; i++)

raw_recvbuf[len] = 0;

printf("********************=\n");

printf("recv data:\n%s\n", raw_recvbuf);

//char* test = "server: hello!";

//ws_send_data(s, test, strlen(test));

}

二.

websocket

傳送資料

測試**:

void ws_send_data(struct session* s, unsigned char* pkg_data, int pkg_len) else if( pkg_len <= 0xffff ) else 

memcpy(send_buffer + send_len, pkg_data, pkg_len);

send_len += pkg_len;

//傳送資料

send(s->c_sock, send_buffer, send_len, 0);

}

TCP協議接收與傳送資料

tcp 建立連線通道 資料無限制 速度慢可靠 tcp協議傳送資料 1 建立傳送端的socket物件 這一步如果成功,就說明連線已經建立成功了。2 獲取輸出流,寫資料 3 釋放資源 public static void main string args throws ioexception tcp協議...

C 用HTTP協議傳送 接收資料

用http協議傳送 接收資料 傳送 post給定的url並獲取返回的資料 byte data encoding.unicode.getbytes strxml mywebrequest.contentlength data.length 新增request.contenttype,否則對方可能無法接...

UDP協議接收資料與傳送資料

udp 把資料打包 資料有限制 不建立連線 速度快不可靠一些需要用到的方法 inetaddress 類 tring gethostaddress 返回 ip 位址字串 以文字表現形式 string gethostname 獲取此 ip 位址的主機名。static inetaddress getbyn...