DLib Http Server程序示例
/*
这个示例是一个使用了Dlib C++ 库的server组件的HTTP扩展
它创建一个始终以简单的HTML表单为响应的服务器。
要查看这个页面,你应该访问 http://localhost:5000
*/
#include <iostream>
#include <sstream>
#include <string>
#include <dlib/server.h>
using namespace dlib;
using namespace std;
class web_server : public server_http
{
const std::string on_request (
const incoming_things& incoming,
outgoing_things& outgoing
)
{
ostringstream sout;
// 我们将发回一个包含HTML表单的页面,其中包含两个文本输入字段。一个字段是name(还有一个是pass)
// HTML表单使用post方法,也可以使用get方法(只需将method='post'改为method='get').
sout <<" <html><meta charset=\"utf-8\"><body> "
<< "<form action='/form_handler' method='post'> "
<< "用户名称: <input name='user' type='text'><br> "
<< "用户密码: <input name='pass' type='text'> <input type='submit'> "
<< " </form>";
// 回写这个请求传入的一些信息,以便它们显示在生成的网页上
sout << "<br> path = " << incoming.path << endl;
sout << "<br> request_type = " << incoming.request_type << endl;
sout << "<br> content_type = " << incoming.content_type << endl;
sout << "<br> protocol = " << incoming.protocol << endl;
sout << "<br> foreign_ip = " << incoming.foreign_ip << endl;
sout << "<br> foreign_port = " << incoming.foreign_port << endl;
sout << "<br> local_ip = " << incoming.local_ip << endl;
sout << "<br> local_port = " << incoming.local_port << endl;
sout << "<br> body = \"" << incoming.body << "\"" << endl;
// 如果此请求是用户提交表单的结果,则回显提交
if (incoming.path == "/form_handler")
{
sout << "<h2> 来自 query string 的信息 </h2>" << endl;
sout << "<br> user = " << incoming.queries["user"] << endl;
sout << "<br> pass = " << incoming.queries["pass"] << endl;
// 将这些表单提交作为Cookie保存
outgoing.cookies["user"] = incoming.queries["user"];
outgoing.cookies["pass"] = incoming.queries["pass"];
}
// 将所有Cookie回传到客户端浏览器
sout << "<h2>客户web浏览器发送给服务器的 Cookies</h2>";
for ( key_value_map::const_iterator ci = incoming.cookies.begin(); ci != incoming.cookies.end(); ++ci )
{
sout << "<br/>" << ci->first << " = " << ci->second << endl;
}
sout << "<br/><br/>";
sout << "<h2>客户web浏览器发送给服务器的 HTTP Headers</h2>";
// 回显从客户端web浏览器接收的所有HTTP headers
for ( key_value_map_ci::const_iterator ci = incoming.headers.begin(); ci != incoming.headers.end(); ++ci )
{
sout << "<br/>" << ci->first << ": " << ci->second << endl;
}
sout << "</body> </html>";
return sout.str();
}
};
int main()
{
try
{
// 创建一个 web server 实例对象
web_server our_web_server;
// 监听5000端口
our_web_server.set_listening_port(5000);
// 告诉服务器开始接受连接。
our_web_server.start_async();
cout << "请按Enter(回车)键去结束程序" << endl;
cin.get();
}
catch (exception& e)
{
// 上面出错会抛出异常
// 输出异常消息
cout << e.what() << endl;
}
}
DLib Http Server程序示例的更多相关文章
- Android : 跟我学Binder --- (3) C程序示例
目录: Android : 跟我学Binder --- (1) 什么是Binder IPC?为何要使用Binder机制? Android : 跟我学Binder --- (2) AIDL分析及手动实现 ...
- mysql的启动脚本mysql.server及示例配置文件
以MySQL-server-4.0.14-0.i3862881064151.rpm为例,放在/data目录下 cd /data rpm -ivh MySQL-server-4.0.14-0.i386. ...
- 小程序-demo:小程序示例
ylbtech-小程序-demo:小程序示例 1.返回顶部 0. 1.app.js const openIdUrl = require('./config').openIdUrl App({ ...
- 《Unix 网络编程》05:TCP C/S 程序示例
TCP客户/服务器程序示例 系列文章导航:<Unix 网络编程>笔记 目标 ECHO-Application 结构如下: graph LR; A[标准输入/输出] --fgets--> ...
- [计算机网络]简易http server程序
好久没输出了,知识还是要写下总结才能让思路更加清晰.最近在学习计算机网络相关的知识,来聊聊如何编写一个建议的HTTP服务器. 这个http server的实现源代码我放在了我的github上,有兴趣的 ...
- 【转】推荐介绍几款小巧的Web Server程序
原博地址:http://blog.csdn.net/heiyeshuwu/article/details/1753900 偶然看到几个小巧有趣的Web Server程序,觉得有必要拿来分享一下,让大家 ...
- Arduino 入门程序示例之一个 LED(2015-06-11)
前言 答应了群主写一些示例程序,一直拖延拖延拖延唉.主要还是害怕在各大高手面前班门弄斧……(这也算是给拖延症找一个美好的理由吧),这几天终于下决心要写出来了,各位高手拍砖敬请轻拍啊. 示例程序 首先是 ...
- WinDBG调试.NET程序示例
WinDBG调试.NET程序示例 好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Co ...
- [图形学] Chp10 OpenGL三维观察程序示例
10.10节书中给出了一个程序示例,有一个填充正方形,从侧面的角度观察并画到屏幕上. 图0 这里进一步画出一个立方体,将相机放入立方体中心,旋转相机,达到在立方体中旋转看到不同画面的效果. 步骤: 1 ...
随机推荐
- [转]用国内软件源为Ubuntu的apt-get提速方法
FROM : http://www.jb51.net/os/Ubuntu/45293.html 刚装好Ubuntu系统之后根据需要还要安装一系列的软件,最省心的办法就是通过apt-get来进行 默 ...
- PHP xhprof性能优化
xhprof window http://dev.freshsite.pl/php-extensions/xhprof.html http://php.net/manual/en/book.xhpro ...
- CSV 数字转化文本
最近遇到一个Bug问题,csv 数值转化为文本的问题. 数据如下: 运行效果 如下: 大家看到“01720” 前面的0 没有显示出来.怎样才能显示出来了, 这里的csv文件格式也没有什么问题.后来找到 ...
- 用 .NET Memory Profiler 跟踪.net 应用内存使用情况--基本应用篇(转)
.net 框架号称永远不会发生内存泄漏,原因是其引入了内存回收的机制.但实际应用中,往往我们分配了对象但没有释放指向该对象的引用,导致对象永远无法释放.最 常见的情况就是给对象添加了事件处理函数,但当 ...
- 如何在Vblock里配置Boot from SAN
啥是vBlock ============ vBlock是VCE用在包含了它的数据中心产品的组件的机架上的一个商标名. 机架中的组件都是有VCE出厂前预先组装好的, 组件的预设以及解决方案, 都是客户 ...
- 发布一个高效的JavaScript分析、压缩工具 JavaScript Analyser
发布一个高效的JavaScript分析.压缩工具 JavaScript Analyser 先发一段脚本压缩示例,展示一下JSA语法压缩和优化功能. try { //xxxx(); } catch (e ...
- jQuery EasyUI 入门简介
对于前端开发者来说,在开发过程中应用“框架”这一工具,可以极大的缩短开发时间,提高开发效率.今天我们就开介绍一款常用的框架——jQuery EasyUI. 那什么是jQuery EasyUI呢? jQ ...
- 理解TensorFlow的Queue
https://www.jianshu.com/p/d063804fb272 这篇文章来说说TensorFlow里与Queue有关的概念和用法. 其实概念只有三个: Queue是TF队列和缓存机制的实 ...
- C#字符串比较
正确写法1 bool bTemplatecontent2 = strtemplateContentInDB.Equals(strTemplateContentInDesignPanel, String ...
- form表单自动回车提交
对于使用了submit按钮的form表单,浏览器会直接建立回车与submit按钮之间的关联