基于HTTP的服务器代码

#include<bits/stdc++.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>

using namespace std;



void headlestaticfunc(int fd,char *filename,char *args){
          
   

}
void headlestatic(int fd,char *filename){
          
   

}

void headledynamicfunc(char filename[],char args[],int &result){
          
   
    char *a = strtok(args,"&");
    char *b = strtok(NULL,"&");
    int x = atoi(a);
    int y = atoi(b);
    if(strstr(filename,"Add")){
          
   
        result = x + y;
    }else if(strstr(filename,"Sub")){
          
   
        result = x - y;
    }
}

void headledynamic(int fd,char filename[],char args[]){
          
   
    int result = 0;
    char response[8000];
    headledynamicfunc(filename,args,result);

    // 发送响应头到客户端
	sprintf( response, "HTTP/1.1 200 OK
") ;
	sprintf( response , "%sServer: Lqingyi Server

" , response ) ;
	send( fd, response , strlen( response ) ,0 ) ; 
 
	 //发送响应Body到客户端
	sprintf(response , "<head><head><title>Welcome to Lqingyi Server!</title></head>" ) ;
	sprintf( response , "%s<body>The result is %d</body>" , response , result ) ;
	send(fd, response , strlen( response ) ,0) ; 
}

bool ParseURI(int fd,char uri[],char filename[],char args[]){
          
   
    sprintf(filename,".%s",uri);
    if(!strstr(uri,"dynamic")){
          
   
        strcpy(args,"");
        strcpy(filename,".");
        strcat(filename,uri);
        if(uri[strlen(uri) - 1] == /){
          
   
            strcat(filename,"index.html");
        }
        return true;
    }else{
          
   
        char *ptr = strstr(uri,"?");
        if(ptr){
          
   
            strcpy(args,ptr + 1);
        }else{
          
   
            strcpy(args,"");
        }
        strcpy(filename,uri);
        strcat(uri,"?");
        strcat(uri,args);
        return false;
    }
}

void dealget(int fd,char buff[]){
          
   
    bool is_static;
    char *uri = strtok(NULL," ");
    char args[20] = {
          
   0},filename[100] = {
          
   0};
    is_static = ParseURI(fd,uri,filename,args);
    if(is_static){
          
   //请求为静态
        headlestatic(fd,filename);
    } else{
          
             
        headledynamic(fd,filename,args);
    }
}

void postsend(int result,int fd){
          
   
    char response[8000];
    sprintf( response, "HTTP/1.1 200 OK
") ;
	sprintf( response , "%sServer: Lqingyi Server

" , response ) ;
	send( fd, response , strlen( response ) ,0 ) ; 
 
	 //发送响应Body到客户端
	sprintf(response , "<head><head><title>Welcome to Lqingyi Server!</title></head>" ) ;
	sprintf( response , "%s<body>The result is %d</body>" , response , result ) ;
	send(fd, response , strlen( response ) ,0) ; 
}

void dealpost(int fd,char buff[]){
          
   
    int result = 0;
    char *s = strtok(NULL,"(");
    result = 0;
    char *num = strtok(NULL,")");
    if(strstr(s,"Add") != NULL){
          
   
        result += atoi(num);
    }else{
          
   
        result -= atoi(num);
    }

    s = strtok(NULL,"(");
    num = strtok(NULL,")");
    if(strstr(s,"Add")){
          
   
        result += atoi(num);
    }else{
          
   
        result -= atoi(num);
    }
    postsend(result,fd);
}


int main(int argc,char *argv[]){
          
   
    if(argc < 3){
          
   
        cout << "输入 IP PORT" << endl;
        return 0;
    }
    int fd = socket(AF_INET,SOCK_STREAM,0);
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);

    socklen_t len = sizeof addr;
    if(-1 == bind(fd,(struct sockaddr*)&addr,len)){
          
   
        perror("ERROR: ");
        return 0;
    }

    if(-1 == listen(fd,100)){
          
   
        perror("ERROR: ");
        return 0;
    }

    int fds;
    char buff[1024];
    while(1){
          
   
        sockaddr_in from;
        len = sizeof from;
        if( (fds = accept(fd,(struct sockaddr*)&from,&len)) == -1){
          
   
            cout << "连接失败" << endl;
            return 0;
        }
        cout << "IP : " << inet_ntoa(from.sin_addr) << "连接成功" << endl;
        //printf("IP: %s 连接成功
",inet_ntoa(from.sin_addr.s_addr));
        bzero(&buff,sizeof buff);
        recv(fds,&buff,sizeof buff,0);

        if(strstr(strtok(buff," "),"GET")){
          
   
            dealget(fds,buff);
        }else{
          
   
            dealpost(fds,buff);
        }
        
        close(fds);
    }
    close(fd);








    return 0;
}
经验分享 程序员 微信小程序 职场和发展