zoukankan      html  css  js  c++  java
  • vc 实现一个 http Server

    实现 http 的协议解析 以及封装 , 对应不同的访问地址, 调用绑定的处理程序 , 

    #include "stdafx.h"
    
    #include "lyocommwebServer.h"
    #include "lyocommwebServer.cpp"
    
    
    // http://127.0.0.1:10000/
    
    
    
    unsigned int __stdcall favicon(void* requ )
    {
    	lyoRequest* req = (lyoRequest*) requ;
    	char* buf;
    	int bufSize;
    
    
    	FILE * file = NULL;
    	errno_t e = fopen_s(&file, "E:\测试代码\cWeb\Debug\favicon.ico", "r");
    	if (!e && file)
    	{
    		fseek(file, 0,SEEK_END );
    		bufSize = ftell(file);
    
    		buf = new char[bufSize];
    
    		fseek(file, 0, SEEK_SET );
    		fread(buf,   bufSize, 1 , file) ;
    
    		fclose(file);
    
    	}
    
    	lyoResponse resp;
    	resp.status = http_200;
    	resp.type = content_html ;
    	resp.content = buf ;
    	resp.length =  bufSize;
    
    	lyoServer::doResponse(resp, req->sock);
    	delete[] buf;
    	delete req;
    
    	return 1;
    
    }
    
    
    unsigned int __stdcall index(void* requ )
    {
    	lyoRequest* req = (lyoRequest*) requ;
    	char* con = "<head><title>index.html</title></head><body><b>web server 测试页面</b></body>";
    
    
    	lyoResponse resp;
    	resp.status = http_200;
    	resp.type = content_html ;
    	resp.content = con ;
    	resp.length = strlen( con);
    
    	lyoServer::doResponse(resp, req->sock);
    	delete req;
    
    	return 1;
    
    }
    
    
    void main(int argc, char * argv[]) {
    
    
    	lyoServer server = lyoServer("127.0.0.1" , 10000);
    
    	void* p = (PVOID)&server ;
    
    	server.handlers["/index.php"] = &index;
    	server.handlers["/"] = &index;
    	server.handlers["/favicon.ico"] = &favicon;
    
    
    	if (server.valid)
    	{
    		server.listen();
    	}
    
    	
    
    }
    

      以下是测试页面:

    如无特别说明, 文章为本人原创, 转载请注明出处, 也欢迎批评指正
    -------------------------------------------------------------------
    如有问题, 欢迎邮箱讨论: lyogogo@outlook.com
  • 相关阅读:
    《信息学奥赛一本通》提高版题解索引
    QUERY [ 单调栈 ]
    [ 模拟退火 ] bzoj3860 平衡点
    [ 考试 ] 7.12
    离线和简单分治
    [ 校内OJ ] NOIP2019模拟赛(九)
    校内模拟考 (一)
    Codeforces 808E
    学习笔记—点分治
    [ 线段树+哈希 ] 反等差数列
  • 原文地址:https://www.cnblogs.com/lyo1005/p/4393029.html
Copyright © 2011-2022 走看看