websocket是什么,如何用C语言写一个简单的websocket通信
时间: 2023-11-08 21:04:44 浏览: 310
RS485,websocket编程c语言源码,c语言项目
WebSocket是一种在单个TCP连接上进行全双工通信的协议,它可以使浏览器和服务器之间实现实时通信。WebSocket 在 Web 应用程序中使用越来越广泛,它被广泛应用于在线游戏、视频会议、网络直播等场景。
下面是一个使用C语言编写的简单的WebSocket通信示例:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_size;
char buf[BUF_SIZE];
bool is_handshake = false;
// 创建服务器socket
server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_socket < 0) {
perror("socket");
exit(1);
}
// 设置服务器socket地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(8080);
// 绑定服务器socket和地址
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_socket, 5) < 0) {
perror("listen");
exit(1);
}
printf("Server is running on port 8080...\n");
while (1) {
// 接受客户端连接
client_addr_size = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_size);
if (client_socket < 0) {
perror("accept");
continue;
}
printf("Client %s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
while (1) {
// 接收客户端发送的数据
int len = recv(client_socket, buf, BUF_SIZE, 0);
if (len < 0) {
perror("recv");
break;
} else if (len == 0) { // 客户端关闭连接
printf("Client %s:%d closed\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
}
if (!is_handshake) { // 如果还没有进行WebSocket握手
// 进行WebSocket握手
char *key = strstr(buf, "Sec-WebSocket-Key: ");
if (key) {
char *end = strstr(key, "\r\n");
if (end) {
char accept_key[BUF_SIZE];
memset(accept_key, 0, sizeof(accept_key));
strncpy(accept_key, key + strlen("Sec-WebSocket-Key: "), end - key - strlen("Sec-WebSocket-Key: "));
strcat(accept_key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
unsigned char sha1_buf[BUF_SIZE];
memset(sha1_buf, 0, sizeof(sha1_buf));
sha1((unsigned char *)accept_key, strlen(accept_key), sha1_buf);
char accept_key_base64[BUF_SIZE];
memset(accept_key_base64, 0, sizeof(accept_key_base64));
base64_encode(sha1_buf, 20, accept_key_base64);
char response[BUF_SIZE];
memset(response, 0, sizeof(response));
sprintf(response, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n", accept_key_base64);
// 发送握手响应
send(client_socket, response, strlen(response), 0);
is_handshake = true;
printf("WebSocket handshake completed\n");
}
}
} else { // 已经完成WebSocket握手
// 解析WebSocket帧数据
int payload_len = buf[1] & 0x7f;
int masking_key_offset = 2;
if (payload_len == 126) {
payload_len = (buf[2] << 8) | buf[3];
masking_key_offset = 4;
} else if (payload_len == 127) {
printf("Payload length > 65535 is not supported\n");
break;
}
unsigned char masking_key[4];
memcpy(masking_key, buf + masking_key_offset, sizeof(masking_key));
unsigned char *payload = (unsigned char *)malloc(payload_len);
memcpy(payload, buf + masking_key_offset + sizeof(masking_key), payload_len);
for (int i = 0; i < payload_len; i++) {
payload[i] ^= masking_key[i % sizeof(masking_key)];
}
// 打印接收到的消息
printf("Received message: %s\n", payload);
// 发送消息给客户端
char message[BUF_SIZE] = "Hello, WebSocket!";
send_websocket_frame(client_socket, message, strlen(message));
free(payload);
}
}
close(client_socket);
}
close(server_socket);
return 0;
}
```
这个示例程序实现了一个简单的WebSocket服务器,它可以接收客户端发送的WebSocket帧数据,并且向客户端发送一条固定的消息。在实际使用中,你需要根据具体的需求进行修改和扩展。
阅读全文