c语言编写一个简单的服务器<初级版>

2023-05-16

编写服务器源代码winhttp.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>


#define SERVER_PORT 80
void do_http_request(int client_sock);
int get_line(int sock,char *buf,int size);
int main(){

    int sock;//
    struct sockaddr_in server_addr;

    sock=socket(AF_INET,SOCK_STREAM,0);

    bzero(&server_addr,sizeof(server_addr));

    server_addr.sin_family=AF_INET;//选择协议族
    server_addr.sin_addr.s_addr=htonl(INADDR_ANY);//监听本地所有IP地址

    server_addr.sin_port=htons(SERVER_PORT);//绑定端口号

    bind(sock,(struct sockaddr *)&server_addr,sizeof(server_addr));


    listen(sock,120);

    printf("等待客户端的连接\n");
    

    int done=1;

    while(done){
        struct sockaddr_in client;
        int client_sock,len,i;
        char client_ip[64];
        char buf[256];

        socklen_t client_addr_len;
        client_addr_len=sizeof(client);
        client_sock=accept(sock,(struct sockaddr *)&client,&client_addr_len);

        printf("Client ip:%s\t port:%d\n",inet_ntop(AF_INET,&client.sin_addr.s_addr,client_ip,sizeof(client_ip)),ntohs(client.sin_port));

        do_http_request(client_sock);

        close(client_sock);
    }
    close(sock);
    return 0;
}
void do_http_request(int client_sock){

    int len=0;
    char buf[256];
    do{
        len=get_line(client_sock,buf,sizeof(buf));
        printf("read line :%s\n",buf);
    }while(len>0);
}

int get_line(int sock,char *buf,int size){

    int count=0;
    char ch='\0';
    int len=0;
    while((count<size-1) && ch!='\n'){

        len=read(sock,&ch,1);
        if(len==1){
            if(ch=='\r'){
                continue;
            }else if(ch == '\n'){
                buf[count]='\0';
                break;
            }//读取到一般字符
            buf[count]=ch;
            count++;
        }else if(len==-1){
            //读取出错
            perror("read faild");
            count=-1;
            break;
        }else{
            //客户端关闭sock连接
            fprintf(stderr,"client close.\n");
            count=-1;
            break;
        }
    }
    if(count>=0) buf[count]='\0';
    return count;
}

打包生成可执行文件

[root@localhost c++]# gcc winhttp.c -o winhttp.exe
[root@localhost c++]# ls
winhttp.c  winhttp.exe

启动服务器

[root@localhost c++]# ./winhttp.exe 
等待客户端的连接

浏览器访问
在这里插入图片描述

查看服务器终端输出

[root@localhost c++]# ./winhttp.exe 
等待客户端的连接
Client ip:192.168.137.1	 port:62653
read line :GET /index.html HTTP/1.1
read line :Host: 192.168.137.151
read line :Connection: keep-alive
read line :Cache-Control: max-age=0
read line :Upgrade-Insecure-Requests: 1
read line :User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
read line :Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
read line :Accept-Encoding: gzip, deflate
read line :Accept-Language: zh-CN,zh;q=0.9
read line :Cookie: PHPSESSID=495343c481b1e5f7c0c03dad27e0817d; thinkphp_show_page_trace=0|0
read line :

至此一个简单的服务器代码编写完成。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c语言编写一个简单的服务器<初级版> 的相关文章

随机推荐