以下代码实现websocket的数据收发功能websocket_client.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // close
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <openssl/sha.h> // For SHA1
#include <openssl/evp.h> // For Base64 encoding
#include <time.h>
#include <stdint.h>
#include <arpa/inet.h>
/**
* 生成一个随机的 Sec-WebSocket-Key
* WebSocket 握手中的 Sec-WebSocket-Key 应该是一个 16 字节的随机值,然后进行 Base64 编码,生成一个 24 字符的字符串。
*/
void generate_sec_websocket_key(char *key, size_t length) {
const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char random_by