##http服务-搭建简易的http服务器
仅仅实现了Post和Get,也就简单的使用了 200 OK,100-continue和发送http格式的文本而已,其实我们只要按照HTTP协议收发数据即可。
- 先来一张做http上传功能时的意外收获,也是待会的测试图片
- 浏览器访问我们所建立的链接
其实就是要返回一段http协议要求的内容即可,当然首先我们要建立一个监听
//建立监听,比较简单,看文末尾贴出的代码即可
if(xml_parse.find("GET")!=string::npos)
{//向客户端发送200 ok及相应的文本信息sprintf( buf, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s",strlen("Hello World!"),"Hello World!");send(clientsoc, buf, strlen(buf)+1, 0);printf("已回复 200 OK\n");continue;
}
//如果提交的内容过大,提交可能会发送要求100-continue,将数据分开发送。
if(xml_parse.find("100-continue")!=string::npos)
{sprintf( buf, "HTTP/1.1 100-continue\r\n");send(clientsoc, buf, strlen(buf)+1, 0);printf("已回复100-continue!\n");//这个是不太管用的,连续两次image_contimue=false;
}
-
这是用浏览器直接与http服务器建立连接后返回的文本信息。http://127.0.0.1:9000。
-
提交表单,先写一份提交表单的简单html文件
上传文件时,一定要注意 enctype=“multipart/form-data”,否则只是上传的文件路径
<html><head></head><body><!-上传文件时,一定要注意 enctype="multipart/form-data",否则只是上传的文件路径><form action="http://127.0.0.1:9000" method="post" enctype="multipart/form-data"><input type="file" name="update"/><input type="submit" value="上传"/></form><form action="http://127.0.0.1:9000" method="post"><input type="text" name="myText"/><input type="submit" /></form></body>
</html>
-
左侧使我们的http服务器,右侧是我们刚写的html文件
-
提交文本信息
-
提交图片信息
-
http服务器收到的图片
-
附上http服务器源码
#include "stdafx.h"#include <winsock2.h>#pragma comment(lib,"ws2_32.lib")#include <fstream>#include <iostream>#include <string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){SOCKET serversoc; SOCKET clientsoc;SOCKADDR_IN serveraddr;SOCKADDR_IN clientaddr;char buf[1024];int len;WSADATA wsa;WSAStartup(MAKEWORD(2,0),&wsa); //初始化WS2_32.DLL//创建套接字if((serversoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0) {printf("套接字socket创建失败!\n");}//命名协议,IP,端口serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(9000);serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//绑定套接字if(bind(serversoc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0){printf("套接字绑定失败!\n");}start:printf("开始监听...\n");//监听请求if(listen(serversoc, 1) != 0){printf("监听失败!\n");}len = sizeof(SOCKADDR_IN);//接收请求if((clientsoc = accept(serversoc, (SOCKADDR *)&clientaddr, &len))<=0){printf("接受连接失败!\n");}printf("**************连接成功*********\n");Sleep(100);//接收数据bool image=false;bool image_contimue=false;bool text=false;while(1){memset(buf,0,1024);int x=0;x=recv(clientsoc, buf, 1024, 0);if(x<= 0) {printf("关闭连接!\n");closesocket(clientsoc);goto start;}cout<<buf<<endl;string xml_parse(buf);if(xml_parse.find("GET")!=string::npos){sprintf( buf, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s",strlen("Hello World!"),"Hello World!");send(clientsoc, buf, strlen(buf)+1, 0);printf("已回复 200 OK\n");continue;}//if(xml_parse.find("POST")!=string::npos){if(xml_parse.find("100-continue")!=string::npos){sprintf( buf, "HTTP/1.1 100-continue\r\n");send(clientsoc, buf, strlen(buf)+1, 0);printf("已回复100-continue!\n");//这个是不太管用的,连续两次image_contimue=false;}else{int off_set=0;if(off_set=xml_parse.find("Content-Type: image")){if(off_set>0&&image_contimue==false){image=true;image_contimue=true;off_set=xml_parse.find_last_of("Content-Type: image");if(off_set<=0)continue;string tmp=xml_parse.substr(off_set+5);cout<<tmp<<endl;ofstream fout;fout.open("received.jpeg",ios::binary|ios::app);fout.write(buf+off_set+5,x-off_set-5);fout.close();}else if(image==true){if(image_contimue==true){ofstream fout;fout.open("received.jpeg",ios::binary|ios::app);fout.write(buf,x);fout.close();}}}}}}printf("关闭连接!\n");closesocket(clientsoc);goto start;WSACleanup(); //释放WS2_32.DLLreturn 0;
}