me
me copied to clipboard
学习 C/C++ (Part 22: minimal web server)
创建 tcp server 框架
不到50行的净代码,
#include "flvhttpd.h"
#include <arpa/inet.h>
#include <string.h>
#include <ctype.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
void error_die(const char *);
void accept_request(void *arg);
int main(void) {
uint16_t port = 3000;
struct sockaddr_in name = {.sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = htonl(INADDR_ANY)};
int on = 1;
int httpd = socket(PF_INET, SOCK_STREAM, 0);
if (httpd == -1) error_die("socket");
if (setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
error_die("setsocket failed");
}
if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0) error_die("bind");
if (listen(httpd, 5) < 0) error_die("listen");
printf("server start at %d\n", port);
int client = -1;
struct sockaddr_in client_name;
socklen_t client_name_len = sizeof(client_name);
pthread_t newthread;
while (true) {
int client = accept(httpd, (struct sockaddr *)&client_name, &client_name_len);
if (client == -1) error_die("accept");
if (pthread_create(&newthread, NULL, (void *)accept_request, (void *)(intptr_t)client) != 0)
perror("pthread_create");
}
close(httpd);
return 0;
}
void error_die(const char *sc) {
perror("error: ");
exit(1);
}
void accept_request(void *arg) {
printf("%s\n", __FUNCTION__);
int client = (intptr_t)arg;
char buffer[] = "HTTP/1.1 200 OK\nContent-type: text/html\nContent-length: 20\n\n<h1>hello world</h1>";
write(client, buffer, strlen(buffer));
close(client);
}
考虑Content-length:
char head[1024];
memset(head, 0x00, sizeof(head));
char head_format[] = "HTTP/1.1 200 OK\nContent-type: text/html\nContent-length: %d\n\n";
char body[] = "<h1>hello world, from httpd</h1>";
sprintf(head, head_format, strlen(body));
write(client, head, strlen(head));
write(client, body, strlen(body));
close(client);
参考文档
- Tiny HTTPd download | SourceForge.net
- EZLippi/Tinyhttpd: Tinyhttpd 是J. David Blackstone在1999年写的一个不到 500 行的超轻量型 Http Server,用来学习非常不错,可以帮助我们真正理解服务器程序的本质。官网 //tinyhttpd.sourceforge.net
- cesanta/mongoose: Embedded Web Server 8.4 stars
- liigo/tinyweb: Tinyweb, a tiny web server based on libuv, by liigo, 2013/06. libuv实现
- HTTP Server in C | Dev Notes
- A Very Simple HTTP Server writen in C | Abhijeet's Blog
- A very simple HTTP server in C, for Unix, using fork()