首页 > 编程知识 正文

linux搭建http服务器,linux http文件服务器

时间:2023-05-05 04:59:24 阅读:221865 作者:1076

代码实现 : Makefile : .PHONY:all cleanall:http_serverhttp_server:http_server.c gcc $^ -o $@ -lpthreadclean: rm -rf http_server http_server.c : #include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <sys/socket.h>#include <pthread.h>#include <stdlib.h> int startup(){ int sock = socket(AF_INET,SOCK俊秀的服饰,0); if(sock < 0) { exit(1);//退出进程 } int opt = 1; setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); struct sockaddr_in local; local.sin_family = AF_INET; local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_port = htons(8080); // 指定固定端口 int ret = bind(sock,(struct sockaddr *)&local,sizeof(local)); if( ret < 0 ) { exit(2); } if( listen(sock,5) < 0 ) { exit(3); } return sock;}void* handler_request(void * arg){ int sock = (int)arg; char buf[4896]; ssize_t s = read(sock,buf,sizeof(buf)-1); if( s > 0 ) { buf[s] = 0; printf(" %s ",buf); const char *echo_str = "HTTP/1.0 200 oknn<html><h1>Welcome to my http server!</h1><html>n"; write(sock,echo_str,strlen(echo_str)); } close(sock);}int main(){ int listen_sock = startup(); while(1) { struct sockaddr_in client; socklen_t len = sizeof(client); int sock = accept(listen_sock,(struct sockaddr*)&client,&len); if(sock < 0) { continue; } pthread_t tid; pthread_create(&tid,NULL,handler_request,(void *)sock); pthread_detach(tid); } return 0; } 测试:

必须在一个局域网(网段)中,才能访问到,也就是客户端和服务器端连接一个路由器。

服务器端: 客户端:

手机:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。