应用层数据传递之http协议
2017-09-05 20:42
281 查看
一、应用层:(上层)决定发送什么东西。http https ftp dns telnet
http://www.baidu.com:80 (:80一般为默认的,省略) ,将域名解析为ip+port(80),据此实现网络通信。
二、自己写服务器作为应答报文段步骤
(1)获取请求报文段
(2)解析请求报文段,拿到客户请求的网页名
(3)搜索本服务器,查看请求网页是否存在,如果不存在,返回404
(4)如果请求网页存在,拼接应答报文段(由报头和网页内容组成)
代码示例:
4000
http://www.baidu.com:80 (:80一般为默认的,省略) ,将域名解析为ip+port(80),据此实现网络通信。
二、自己写服务器作为应答报文段步骤
(1)获取请求报文段
(2)解析请求报文段,拿到客户请求的网页名
(3)搜索本服务器,查看请求网页是否存在,如果不存在,返回404
(4)如果请求网页存在,拼接应答报文段(由报头和网页内容组成)
代码示例:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <sys/stat.h> #include <fcntl.h> //1.获取请求报文段 //2.解析请求报文段,拿到客户请求的网页名 //3.搜索本服务器,查看那请求网页是否存在,如果不存在,返回404 //4.如果请求网页存在,拼接应答报文段。(应答报文段由报头和网页内容组成) void main() { int sock=socket(PF_INET,SOCK_STREAM,0); assert(sock!=-1); struct sockaddr_in ser,cli; memset(&ser,0,sizeof(ser)); ser.sin_family=AF_INET; ser.sin_port=htons(80); ser.sin_addr.s_addr=inet_addr("192.168.1.129"); int res=bind(sock,(struct sockaddr*)&ser,sizeof(ser)); assert(res!=-1); listen(sock,5); while(1) { int len=sizeof(cli); int c=accept(sock,(struct sockaddr*)&cli,&len); assert(c!=-1); //1.获取数据,请求报文段 char recvbuff[1024]={0}; int n=recv(c,recvbuff,1024,0); if(n<=0) { printf("error\n"); continue; } //以普通用户执行不了,因为是80端口,所以要切换到用户su //printf("%s\n",recvbuff);打印不显示 //所以改为2.解析请求报文段 char *p=strtok(recvbuff," ");//做切割 if(p!=NULL) { p=strtok(NULL," "); } //搜索本服务器 char path[128]="/var/www/html"; strcat(path,p);//var/www/html/login.html连接成这样 int fd=open(path,O_RDONLY);//以只读方式打开html文件 if(fd==-1) { //404回馈错误信息 char sendbuff[1024]={0}; strcpy(sendbuff,"HTTP/1.1 404 Not Found\r/n");//必须以/r/n结尾 strcat(sendbuff,"Server:http\r\n"); strcat(sendbuff,"Content_Length:14\r\n"); strcat(sendbuff,"Contend_Type:text/html;charset=utf-8\r\n"); strcat(sendbuff,"\r\n"); strcat(sendbuff,"404 Not Found"); send(c,sendbuff,strlen(sendbuff),0); } else { //回馈正确信息(报文段没有现成的,必须自己动手做(按http应答的步骤严格要求)) char sendbuff[1024]={0}; strcpy(sendbuff,"HTTP/1.1 200 OK\r/n");//必须以/r/n结尾 strcat(sendbuff,"Server:http\r\n"); strcat(sendbuff,"Content_Length:14\r\n"); struct stat st; fstat(fd,&st);//查看文件大小 sprintf(sendbuff+strlen(sendbuff),"%d",st.st_size); strcat(sendbuff,"Contend_Type:text/html;charset=utf-8\r\n"); strcat(sendbuff,"\r\n"); //文件中的数据 send(c,sendbuff,strlen(sendbuff),0);//发头部信息 memset(sendbuff,0,1024); int n=0; while(n=read(fd,sendbuff,1023)!=0)//获取文件中数据 { send(c,sendbuff,strlen(sendbuff),0);//发送文件中数据 memset(sendbuff,0,1024); } close(fd); } close(c); } close(sock); }
4000
相关文章推荐
- 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用
- 解决HTTP数据不更新问题——no-cache在HTTP通信协议中的应用
- 解决HTTP数据不更新问题——no-cache在HTTP通信协议中的应用收藏
- 使用Fiddler抓取android应用http数据
- 应用系统之间数据传递的几种方式
- 天天记录 - Android抓包 - 抓取HTTP,TCP协议数据
- Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据
- 详解iPhone应用中HTTP上传请求协议
- 【HTTP】另类的POST头数据 RFC1867协议格式简析
- 网络动态服务器-2-传递数据给应用
- 天天记录 - Android抓包 - 抓取HTTP,TCP协议数据
- 使用asx3m与xstream配合解决flex与java利用httpservice传递xml数据问题
- java实现http协议发送和接收数据
- iOS开发拓展篇—应用之间的跳转和数据传递
- 使用分享扩展在应用之间传递数据
- iOS开发拓展篇—应用之间的跳转和数据传递
- HTTP协议下Android web应用GET和POST请求方法的请求、响应、传参
- Android与服务器端数据交互(http协议整合struts2+android)
- android HTTp协议获取数据总结
- 应用c#读取带cookie的http数据