这是我做的算是第二个项目了吧…
第一个项目应该是扫雷,代码之前弄丢了,改天再写一份上传。
这个项目的需求是:
从浏览器访问服务器,然后获取一个网页,该网页需要有文本文件和图片文件。
思路:
1、制作index.html文件、404.html文件、下载一个mypic.jpeg文件,放入工作路径下。
2、编写头文件t_net.h,并写头文件卫士、include、和typedef以及函数声明。
3、封装服务器的socket和bind函数,函数名为s_bind,封装文件为:t_net.c
4、封装服务器的业务处理文件、函数名为do_main,封装文件尾:do_main.c
5、编写主函数server.c,并调用t_net.c和do_main.c
核心的业务注意事项:
1、对浏览器发送的连接请求,先向浏览器回应标准http协议头,然后再将index.html文件发送给浏览器。
2、这里涉及到2次请求(一次网页,一次图片),所以在服务器端需要accept两次,才能正常接收到所有的网页请求。
3、若没有找到页面文件,需要返回一个404.html,我这里取了个巧,用open的flags里边的互斥标签,O_CREAT和O_EXCL进行判断这个文件是否存在。可能会出现一些问题,但目前还不得而知。
好,业务缕清楚了。代码如下:
index.html文件就随便写了,代码参见:
<html>
<head><title>Test Page</title></head>
<body>
<p>This is my wife:</p>
<img src='mypic.jpeg'>
</body>
</html>
404.html代码参见:
<html>
<body>request file not found</body>
</html>
t_net.h:
#ifndef T_NET_H__
#define T_NET_H__
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
typedef struct sockaddr SA;
typedef struct sockaddr_in SA4;
int s_bind(int domain, int type, unsigned short port);
#endif
封装的socket和bind函数文件t_net.c参见:
#include <stdio.h>
#include "t_net.h"
int s_bind(int domain,int type,unsigned short port){
SA4 serv;
//创建一个通讯端点,返回一个文件描述符
int lfd=socket(domain,type,0);
if(lfd==-1){
perror("socket");
return -1;
}
//需要对serv变量的成员进行初始化
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
//将serv绑定到lfd上.
int b=bind(lfd,(SA *)&serv,sizeof(serv));
if(b==-1){
perror("bind");
return -1;
}
return lfd;
}
domain还没来得及封装,一块参见server.c吧。
#include <stdio.h>
#include "t_net.h"
#include <stdlib.h>
#include <string.h>
int main(void){
int conn_fd;//用于和客户端连接的描述符
char IP[64];
char buf[102400];
SA4 cli;
socklen_t cli_len;
int lfd=s_bind(AF_INET,SOCK_STREAM,8000);
if(lfd==-1) return -1;
listen(lfd,5);
while(1){
cli_len=sizeof(cli);
conn_fd=accept(lfd,(SA *)&cli,&cli_len);
if(conn_fd==1){
perror("accept");
return -1;
}
//业务处理
pid_t pid=fork();
if(pid==-1){perror("fork");return -1;}
if(pid==0){
close(lfd);
char *html="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
write(conn_fd, html,strlen(html));
int web=open("index.html",O_RDONLY|O_CREAT|O_EXCL);
if(web==-1){
web=open("index.html",O_RDONLY);
int r=read(web, buf, 102400);
write(conn_fd, buf,r);
close(conn_fd);
exit(0);
}else{
close(web);
remove("index.html");
int web=open("404.html",O_RDONLY);
if(web==-1){perror("open");return -1;}
int r=read(web, buf, 102400);
write(conn_fd, buf,r);
close(conn_fd);
exit(0);
}
}else{
close(conn_fd);
}
conn_fd=accept(lfd,(SA *)&cli,&cli_len);
if(conn_fd==1){
perror("accept");
return -1;
}
pid_t pid1=fork();
if(pid1==-1){perror("fork");return -1;}
if(pid1==0){
close(lfd);
char *img="HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\n\r\n";
write(conn_fd, img,strlen(img));
int web1=open("mypic.jpeg",O_RDONLY);
int r1=read(web1, buf, 102400);
write(conn_fd, buf,r1);
close(conn_fd);
exit(0);
}else{
close(conn_fd);
}
}
close(lfd);
return 0;
}
整个项目到这里就全部结束了。
这里用的操作系统是:Ubuntu 16.04
先把服务器起了,然后打开127.0.0.1:8000
返回的效果如下:
当然没显示全啦哈哈。