如何用c语言实现http服务器

2025-12-17 06:56:10
推荐回答(3个)
回答1:

去看一下《Advanced Linux Programming》这本书吧,第11章讲的就是怎么用C语言实现一Http服务器。 这里有下载地址(英文的): http://www.advancedlinuxprogramming.com/alp-folder 英文看起来不顺的话可以上网找找有没有中文版的这本书,应该叫Linux高级编程吧~~~参考资料: http://www.advancedlinuxprogramming.com/alp-folder

回答2:

apache就是用C写的,而且是开源,你下载它的源代码,把它看懂就行。

回答3:

//服务端简易代码如下:
#include   
#include   
  
#include   
#include   
#include   
  
void http_handle(struct evhttp_request *req, void *arg); /*  HTTP Request Handle  */  
  
int main(){  
    struct evhttp *httpd;  
    event_init();  
    httpd = evhttp_start("0.0.0.0", 2345);  
    if (httpd == NULL) {  
        fprintf(stderr, "Error: Unable to listen on %s:%d\n\n");  
        exit(1);      
    }     
    evhttp_set_timeout(httpd, 2000);  
    evhttp_set_gencb(httpd, http_handle, NULL);  
    event_dispatch();  
    evhttp_free(httpd);  
  
    return 0;  
}  
  
void http_handle(struct evhttp_request *req, void *arg){  
    struct evbuffer *buf;  
    buf = evbuffer_new();  
  
    /*  Response the client  */  
    evhttp_send_reply(req, HTTP_OK, "OK", buf);  
  
    //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED");  
  
    /*  Release the memory  */  
    evbuffer_free(buf);  
    fprintf(stderr,"Send \n");  
}

编译:编译时把libevent的类库中的.so文件和.h文件连接 进来。