/*

    这个示例是一个使用了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程序示例的更多相关文章

  1. Android : 跟我学Binder --- (3) C程序示例

    目录: Android : 跟我学Binder --- (1) 什么是Binder IPC?为何要使用Binder机制? Android : 跟我学Binder --- (2) AIDL分析及手动实现 ...

  2. mysql的启动脚本mysql.server及示例配置文件

    以MySQL-server-4.0.14-0.i3862881064151.rpm为例,放在/data目录下 cd /data rpm -ivh MySQL-server-4.0.14-0.i386. ...

  3. 小程序-demo:小程序示例

    ylbtech-小程序-demo:小程序示例     1.返回顶部 0. 1.app.js const openIdUrl = require('./config').openIdUrl App({ ...

  4. 《Unix 网络编程》05:TCP C/S 程序示例

    TCP客户/服务器程序示例 系列文章导航:<Unix 网络编程>笔记 目标 ECHO-Application 结构如下: graph LR; A[标准输入/输出] --fgets--> ...

  5. [计算机网络]简易http server程序

    好久没输出了,知识还是要写下总结才能让思路更加清晰.最近在学习计算机网络相关的知识,来聊聊如何编写一个建议的HTTP服务器. 这个http server的实现源代码我放在了我的github上,有兴趣的 ...

  6. 【转】推荐介绍几款小巧的Web Server程序

    原博地址:http://blog.csdn.net/heiyeshuwu/article/details/1753900 偶然看到几个小巧有趣的Web Server程序,觉得有必要拿来分享一下,让大家 ...

  7. Arduino 入门程序示例之一个 LED(2015-06-11)

    前言 答应了群主写一些示例程序,一直拖延拖延拖延唉.主要还是害怕在各大高手面前班门弄斧……(这也算是给拖延症找一个美好的理由吧),这几天终于下决心要写出来了,各位高手拍砖敬请轻拍啊. 示例程序 首先是 ...

  8. WinDBG调试.NET程序示例

    WinDBG调试.NET程序示例 好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Co ...

  9. [图形学] Chp10 OpenGL三维观察程序示例

    10.10节书中给出了一个程序示例,有一个填充正方形,从侧面的角度观察并画到屏幕上. 图0 这里进一步画出一个立方体,将相机放入立方体中心,旋转相机,达到在立方体中旋转看到不同画面的效果. 步骤: 1 ...

随机推荐

  1. Apache Jmeter 教程

    Jmeter是一款优秀的开源测试工具, 是每个资深测试工程师,必须掌握的测试工具,熟练使用Jmeter能大大提高工作效率. 熟练使用Jmeter后, 能用Jmeter搞定的事情,你就不会使用LoadR ...

  2. 本地git库gitlab库链接服务器库 idea git 配置 gitlab 配置 git生成ssh公钥

    下载git  https://git-scm.com/ 安装以后会右键菜单出现Git Base Here 点击Git Base Here   输入 cd ~ 进入你的用户根目录 创建文件夹 mkdir ...

  3. 如何回收vRealize Automation里被分配出去了的IP地址

    在vRealize里写代码部署虚机,时间长了,便出现了很多虚机在vCenter里不存在,但在vRealize里还存在的这台虚机的注册信息的现象.最直接的后果是,这些影子虚机会占着IP池里的IP地址不放 ...

  4. Populating Next Right Pointers in Each Node II leetcode java

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  5. JS里关于事件的常被考察的知识点:事件流、事件广播、原生JS实现事件代理

    1.JS里面的事件流 DOM2级事件模型中规定了事件流的三个阶段:捕获阶段.目标阶段.冒泡阶段,低版本IE(IE8及以下版本)不支持捕获阶段 捕获事件流:Netscape提出的事件流,即事件由页面元素 ...

  6. 文本分类(六):使用fastText对文本进行分类--小插曲

    http://blog.csdn.net/lxg0807/article/details/52960072 环境说明:python2.7.linux 自己打自己脸,目前官方的包只能在linux,mac ...

  7. linux里tmpfs文件系统

    linux里tmpfs文件系统 是一个虚拟内存文件系统,它不同于传统的用块设备形式来实现的Ramdisk,也不同于针对物理内存的Ramfs.Tmpfs可以使用物理内存,也可以使用交换分区. umoun ...

  8. w3cscholl的在线代码编辑工具

    https://www.w3cschool.cn/tryrun/runcode?lang=c

  9. 最全的spark基础知识解答

    原文:http://www.36dsj.com/archives/61155 一. Spark基础知识 1.Spark是什么? UCBerkeley AMPlab所开源的类HadoopMapReduc ...

  10. Mongoose的分页功能

    来自: https://github.com/edwardhotchkiss/mongoose-paginate   拷贝如下: Note: This plugin will only work wi ...