基于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\r\n") ;
	sprintf( response , "%sServer: Lqingyi Server\r\n\r\n" , 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\r\n") ;
	sprintf( response , "%sServer: Lqingyi Server\r\n\r\n" , 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 连接成功\n",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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值