实验四 Web服务器2

实验四 Web服务器2

## 信息安全系统设计与实现

20191318王泽文


Web服务器的客户端服务器
代码:

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
 
int port;
#define BACKLOG 5
 
extern int close(int fd);
 
//http协议text/html类型头
char *resp_header = "HTTP/1.1 200 OK\r\n"
                    "Content-Type: text/html;charset=UTF-8\r\n"
                    "Server: Wavin Web Server/1.1\r\n";
 
//头长度
int resp_header_len;
 
//模拟html文件
char *html_body = "<!DOCTYPE html>"
                  "<html><head>"
                  "<title>Welcome to 20191318wzw Web Server (WWS)</title>"
                  "</head><body>Hello by 20191318wzw</body></html>";
 
int get_socketfd()
{
    int server_fd;             
    struct sockaddr_in my_addr; 
    resp_header_len = strlen(resp_header);
    server_fd=socket(AF_INET,SOCK_STREAM,0);
    if(server_fd==-1){
        printf("socket failed:%d\n",errno);
        return -1;
    }
    my_addr.sin_family=AF_INET;
    my_addr.sin_port=htons(port);
    my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    bzero(&(my_addr.sin_zero),8);             
    if(bind(server_fd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))<0){
        printf("bind error\n");
        return -1;
    }
    listen(server_fd,BACKLOG);
    return server_fd;
}
 
void parse_request(int server_fd)
{
    int client;
    int len;
    char url[256];
    char buffer[1024];
    struct sockaddr_in client_addr;
    socklen_t length = sizeof(client_addr);
    printf("Start Socket Listening at port:%d...\n",port);
    while(1){
        client=accept(server_fd,(struct sockaddr*)&client_addr,&length);
        if(client < 0){
            printf("open client socket error\n");
            continue;
        }
        memset(buffer,0,sizeof(buffer));
        //接受客户端请求
        len = recv(client,buffer,sizeof(buffer),0);
        buffer[1024] = '\0';
        printf("receive data len : %d \n\nbuffer:\n %s\n",len,buffer);
 
        memset(buffer,0,sizeof(buffer));
        sprintf(buffer,"%s\n%s",resp_header,html_body);
        printf("send data is:\n%s\n",buffer);
        //发送响应到客户端
        if(send(client,buffer,resp_header_len+strlen(html_body),0) <= 0){
            printf("response failed !\n");
            close(client);
            return;
        }
    }
 
}
 
int str2int(char *str)
{
    int result = 0;
    int len,i,j;
    len = strlen(str);
    for ( i=0; i<len; i++){
        if(str[i] >= '0' && str[i] <= '9'){
            int tmp = 1;
            printf("%d\n",str[i] - '0');
            for ( j=0; j<len-i-1; j++){
                tmp = tmp * 10;
            }
            result += (str[i] - '0') * tmp;
            printf("tmp:%d result:%d\n",tmp,result);
        }
    }
    return result;
}
 
int main(int argc, char **argv)
{
    int server;
    char *args = argv[1];
    
    port = (args == NULL)?8080:str2int(args);
 
    server = get_socketfd();
    parse_request(server);
 
    close(server);
 
    return 0;
}

运行
服务端:

客户端


码云链接:https://gitee.com/Yushino/system-job/tree/master/socket

posted @ 2021-12-12 14:49  20191318王泽文  阅读(58)  评论(0编辑  收藏  举报