/*

    这个示例是一个使用了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. Excel 2016 Power View选项卡不显示的问题

    https://zhuanlan.zhihu.com/p/43543442 PowerView是Excel中的Power系列插件之一,可以基于excel制作交互式仪表板. 初学者在使用Power Vi ...

  2. DICOM医学图像处理:WEB PACS初谈

    背景: 周末看到了一篇原公司同事的文章,讲的是关于新的互联网形势下的PACS系统.正好上一篇专栏文章也提到了有想搭建一个worklist服务器的冲动,所以就翻箱倒柜将原本学生时代做课题时搭建的简易We ...

  3. 部署Percona XtraDB Cluster高可用和多Master集群

    http://www.it165.net/admin/html/201401/2306.html http://www.oschina.net/p/percona-xtradb-cluster/ ht ...

  4. [转]Linux awk 命令 说明

    From : http://blog.csdn.net/tianlesoftware/article/details/6278273 一.  AWK 说明 awk是一种编程语言,用于在linux/un ...

  5. git别名;git配置使用shell函数;git别名使用shell函数;git获取当前分支;git alias

    获取当前分支 git symbolic-ref -q --short HEAD 2. 在git别名里使用shell函数,$1获取第一个参数的值,$2……$n依次类推,根据自己习惯需要定制 3. 提交r ...

  6. 远程连接Oracle数据库

    ylbtech-Oracle:远程连接Oracle数据库  所谓远程连接Oracle数据库,是指Oracle数据库服务器和Oracle客户端分别安装在2台电脑上,我们使用Oracle客户端来连接在另一 ...

  7. 用非递归、不用栈的方法,实现原位(in-place)的快速排序

    大体思路是修改Partition方法将原本枢数的调整放到方法结束后去做.这样因为数组右侧第一个大于当前枢数的位置就应该是未划分的子数组的边界.然后继续进行Partition调整.这种写法照比递归的写法 ...

  8. Word Search leetcode java

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  9. 判断小米 魅族 华为 系统 MIUI EMUI FLYME

    获取系统信息 public class SimpleDeviceUtils { public enum SystemType { /** * 小米手机(MIUI系统) */ SYS_MIUI, /** ...

  10. MFC中如何显示颜色选择对话框

    其实很简单,使用MFC现有的类CColorDialog 即可实现 核心代码如下: void CCColorDialogView::OnGraphSetting() { CColorDialog m_s ...