本篇是模仿PYTHON TWISTED写一个FINGER示例。

从最简单的链接到通过接收字符串返回不同的内容

1 最简单的链接

 #include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp> using boost::asio::ip::tcp; int main()
{
try{
boost::asio::io_service io_service;
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
} std::cout << "finished!" << std::endl;
return ;
}

运行显示如下

finished!
请按任意键继续. . .

2 接收ACCEPT请求 然后丢弃

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp> using boost::asio::ip::tcp; static USHORT defaultPort = ; class tcp_server {
public:
tcp_server(boost::asio::io_service& io_service) :
io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), defaultPort)) {
start_accept();
}
private:
void start_accept() {
boost::shared_ptr<tcp::socket> new_connection(new tcp::socket(io_service_)); acceptor_.async_accept(*new_connection,boost::bind(&tcp_server::handle_accept,this,
new_connection,boost::asio::placeholders::error));
} void handle_accept(boost::shared_ptr<tcp::socket> new_connection,
const boost::system::error_code error) {
if (!error) {
std::cout << "recv a connection!" << std::endl;
new_connection->shutdown(boost::asio::socket_base::shutdown_type::shutdown_both);
}
start_accept();
} boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; int main()
{
try {
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
} std::cout << "finished!" << std::endl;
return ;
}

运行显示如下:

recv a connection!

客户端运行 显示

telnet 127.0.0.1 1123

3 接收连接发送过来的内容 显示 然后关闭

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp> using boost::asio::ip::tcp; static USHORT defaultPort = ; class tcp_connection :public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service) {
return pointer(new tcp_connection(io_service));
} tcp::socket& socket() {
return socket_;
} void start() {
boost::asio::async_read_until(socket_, input_buffer_, '\n',
boost::bind(&tcp_connection::handle_read, shared_from_this(), _1));
}
private:
void handle_read(const boost::system::error_code& err) {
if (!err) {
std::string line;
std::istream is(&input_buffer_);
std::getline(is,line);
if (!line.empty())
{
std::cout << "Received: " << line << "\n";
}
socket_.shutdown(boost::asio::socket_base::shutdown_both);
}
} tcp_connection(boost::asio::io_service& io_service)
:socket_(io_service) {} boost::asio::streambuf input_buffer_;
tcp::socket socket_;
std::string message_;
}; class tcp_server {
public:
tcp_server(boost::asio::io_service& io_service) :
io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), defaultPort)) {
start_accept();
}
private:
void start_accept() {
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service()); acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
std::cout << "new a connection" << std::endl;
} void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error) {
if (!error) {
new_connection->start();
}
start_accept();
} boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; int main()
{
try {
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
} std::cout << "finished!" << std::endl;
return ;
}

运行并TELNET连接后显示如下

new a connection
new a connection
Received: sadasd

4 接收连接发送内容 并返回指定内容

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp> using boost::asio::ip::tcp; static USHORT defaultPort = ; class tcp_connection :public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service) {
return pointer(new tcp_connection(io_service));
} tcp::socket& socket() {
return socket_;
} void start() {
boost::asio::async_read_until(socket_, input_buffer_, '\n',
boost::bind(&tcp_connection::handle_read, shared_from_this(), _1));
}
private:
void handle_read(const boost::system::error_code& err) {
if (!err) {
std::string line;
std::istream is(&input_buffer_);
std::getline(is, line);
if (!line.empty())
{
std::cout << line << "\n";
}
boost::asio::async_write(socket_,
boost::asio::buffer("no this user", strlen("no this user")+),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)); }
} void handle_write(const boost::system::error_code& error) {
if (!error) {
socket_.shutdown(boost::asio::socket_base::shutdown_both);
}
} tcp_connection(boost::asio::io_service& io_service)
:socket_(io_service) {} boost::asio::streambuf input_buffer_;
tcp::socket socket_;
std::string message_;
}; class tcp_server {
public:
tcp_server(boost::asio::io_service& io_service) :
io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), defaultPort)) {
start_accept();
}
private:
void start_accept() {
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service()); acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
std::cout << "new a connection" << std::endl;
} void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error) {
if (!error) {
new_connection->start();
}
start_accept();
} boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; int main()
{
try {
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
} std::cout << "finished!" << std::endl;
return ;
}

运行后显示如下

new a connection
new a connection
asdas

telnet客户端显示

no this user

遗失对主机的连接。

5 接收发送的内容 并在记录中查找是否有该内容的记录

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <map> using boost::asio::ip::tcp; static USHORT defaultPort = ; typedef std::map<std::string, std::string> UserMap;
UserMap users; class tcp_connection :public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service) {
return pointer(new tcp_connection(io_service));
} tcp::socket& socket() {
return socket_;
} void start() {
boost::asio::async_read_until(socket_, input_buffer_, '\n',
boost::bind(&tcp_connection::handle_read, shared_from_this(), _1));
}
private:
void handle_read(const boost::system::error_code& err) {
if (!err) {
std::istream is(&input_buffer_);
is >> line_;
if (!line_.empty())
{
std::cout << line_ << "\n";
UserMap::iterator it = users.find(line_);
if (it != users.end())
{
line_ = it->second;
}
else {
line_ = "no this user";
}
}
else {
line_ = "string error!";
}
boost::asio::async_write(socket_,
boost::asio::buffer(line_, line_.size()),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)); }
} void handle_write(const boost::system::error_code& error) {
if (!error) {
socket_.shutdown(boost::asio::socket_base::shutdown_both);
}
} tcp_connection(boost::asio::io_service& io_service)
:socket_(io_service) {} std::string line_;
boost::asio::streambuf input_buffer_;
tcp::socket socket_;
std::string message_;
}; class tcp_server {
public:
tcp_server(boost::asio::io_service& io_service) :
io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), defaultPort)) {
start_accept();
}
private:
void start_accept() {
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service()); acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
std::cout << "new a connection" << std::endl;
} void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error) {
if (!error) {
new_connection->start();
}
start_accept();
} boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
}; int main()
{
try {
users["DEF"] = "PASS";
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
} std::cout << "finished!" << std::endl;
return ;
}

运行后显示如下

new a connection
new a connection
dda
new a connection
DEF

telnet客户端显示

PASS

遗失对主机的连接。

以上代码所有连接 均以telnet作为客户端

telnet 127.0.0.1 1123
正在连接127.0.0.1...

boost 编写finger服务的更多相关文章

  1. C#编写window服务,一步一步(1)

    Window服务是啥,这里就不废话了,如何用在哪里用也不废话了,这里我这篇文章只是详述了我在vs2012中创建window服务的经过,希望对你有所帮助. 另外:我在编写服务过程中参考了 Profess ...

  2. C#编写windows服务

    项目要求: 数据库用有一张表,存放待下载文件的地址,服务需要轮训表将未下载的文件下载下来. 表结构如下: 过程: VS--文件-->新建项目-->windows-->windows服 ...

  3. 在python中编写socket服务端模块(二):使用poll或epoll

    在linux上编写socket服务端程序一般可以用select.poll.epoll三种方式,本文主要介绍使用poll和epoll编写socket服务端模块. 使用poll方式的服务器端程序代码: i ...

  4. 编写WCF服务时右击配置文件无“Edit WCF Configuration”(编辑 WCF 配置)远程的解决办法

    原文:编写WCF服务时右击配置文件无“Edit WCF Configuration”远程的解决办法 今天在看<WCF揭秘>书中看到作者提出可以在一个WCF Host应用程序的App.Con ...

  5. 使用C语言编写windows服务一般框架

    原文:使用C语言编写windows服务一般框架 编写windows服务和编写windows应用程序一样,有一些回调函数必须填写且向windows 服务管理器(service manager)进行注册, ...

  6. C#编写WINNT服务

    C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题 需求分析: 最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另 ...

  7. C#编写Windows 服务

    C#编写Windows 服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时 ...

  8. python实现编写windows服务

    使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...

  9. 如何在Ruby中编写微服务?

    [编者按]本文作者为 Pierpaolo Frasa,文章通过详细的案例,介绍了在Ruby中编写微服务时所需注意的方方面面.系国内 ITOM 管理平台 OneAPM 编译呈现. 最近,大家都认为应当采 ...

随机推荐

  1. 不常用的vi命令

    vi u 撤回ctrl+r 撤回的撤回 全文替换%s/old/new/g 指定行区间替换12,15s/old/new/g c替换前确认12,15s/old/new/gc 用#代替分隔符,用户关键字有/ ...

  2. python3学习笔记五(列表2)

    参考http://www.runoob.com/python3/python3-list.html 嵌套列表 a = ['a','b','c']b = [1,2,3]x = [a, b]print(x ...

  3. OS&SYS&Shuti模块

    #sys.argv   主要针对脚本可以读取参数 Shuti模块 import shutil f1=open('笔记',encoding='utf-8') f2=open('笔记2','w',enco ...

  4. LDAP认证模式简介

    今天发现公共服务中有ldap数据库服务,先大概了解一下ldap,转载下面的文章.原文链接:https://www.jianshu.com/p/d3f8c8f5d661 另外记录一篇文章地址:https ...

  5. windows本地eclispe运行linux上hadoop的maperduce程序

    继续上一篇博文:hadoop集群的搭建 1.将linux节点上的hadoop安装包从linux上下载下来(你也可以从网上直接下载压缩包,解压后放到自己电脑上) 我的地址是: 2.配置环境变量: HAD ...

  6. ubuntu18.04 安装mysql server

    mysql 5.7支持的最高版本是Ubuntu17 ,即使安装成功后,也会出现各种妖蛾子,本人就被这种问题困扰了好一会.在Ubuntu 18.04下安装mysql,建议安装8.0以上版本! 1. 配置 ...

  7. 利用SharpZipLib进行字符串的压缩和解压缩

    http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...

  8. 【练习】@property练习题

    请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution: 写了一段代码 class Screen(object): #利用property ...

  9. django之normalize函数的功能

    from django.utils.regex_helper import normalize pat=r'^(?P<id>\d+)/(?P<name>\d+)$' bits= ...

  10. 图释SQL的Join

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...