HTTP Keep-Alive
HTTP Keep-Alive is an important feature of the HTTP protocol, enabling multiple HTTP requests/responses to be sent and received over the same TCP connection instead of establishing a new connection for each request. This approach enhances the efficiency and performance of network communication.
To implement HTTP Keep-Alive in C, socket programming is typically used, with the Connection: keep-alive header explicitly specified in the HTTP request. Below is a simple example demonstrating how to implement a Keep-Alive-enabled HTTP client using sockets in C.
c#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> int main() { int sock; struct sockaddr_in server; char message[1000], server_reply[2000]; // Create socket sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { printf("Could not create socket"); } puts("Socket created"); server.sin_addr.s_addr = inet_addr("192.168.0.1"); // Server IP server.sin_family = AF_INET; server.sin_port = htons(80); // HTTP port // Connect to remote server if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { perror("Connect failed. Error"); return 1; } puts("Connected\n"); // Send HTTP request strcpy(message, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: keep-alive\r\n\r\n"); if (send(sock, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } // Receive server reply if (recv(sock, server_reply, 2000, 0) < 0) { puts("recv failed"); return 1; } puts("Server reply :"); puts(server_reply); // Maintain connection for subsequent communication... close(sock); return 0; }
WebSockets
WebSockets provide full-duplex communication capabilities between the client and server. Implementing WebSockets in C involves performing the correct WebSocket handshake followed by sending and receiving data frames over the same connection.
A complete implementation is complex, but the basic steps include:
- Create a TCP Socket Connection.
- Send the WebSocket Upgrade Request (including the correct
UpgradeandConnectionrequest headers). - Parse the response to confirm the server accepts the WebSocket upgrade.
- Send and receive WebSocket data frames.
Here is a simplified code example focusing on sending the WebSocket handshake request:
c#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> int main() { int sock; struct sockaddr_in server; char message[1024], server_reply[2000]; // Create socket sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { printf("Could not create socket\n"); return 1; } printf("Socket created\n"); server.sin_addr.s_addr = inet_addr("192.168.0.1"); server.sin_family = AF_INET; server.sin_port = htons(80); // Connect to remote server if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { perror("Connect failed. Error"); return 1; } printf("Connected\n"); // Send WebSocket upgrade request strcpy(message, "GET /chat HTTP/1.1\r\n"); strcat(message, "Host: example.com\r\n"); strcat(message, "Upgrade: websocket\r\n"); strcat(message, "Connection: Upgrade\r\n"); strcat(message, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); strcat(message, "Sec-WebSocket-Version: 13\r\n\r\n"); if (send(sock, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } // Receive a reply from the server if (recv(sock, server_reply, 2000, 0) < 0) { puts("recv failed"); return 1; } puts("Server reply :"); puts(server_reply); // Close the socket close(sock); return 0; }
These examples provide a foundational framework for implementing HTTP Keep-Alive and WebSockets functionality in C. When developing a complete application, additional considerations such as error handling, more complex data exchange, and security issues must be addressed.