本篇是模仿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. MySql开启远程账户登陆总结

    1.更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"127.0.0.1"改成"% ...

  2. 正点原子stm32f103mini版串口下载BOOT0引脚与与CH340G芯片引脚RTS、DTR、的关系原理

    在做串口实验时,一直搞不明白一键下载是怎么回事,于是自己就去捉摸CH340G这块芯片,那么这里我将详细的讲解一下这块芯片怎么与stm32配合使用的. 1.由CH340G芯片资料可以知道这两个引脚的功能 ...

  3. pytorch下的lib库 源码阅读笔记(2)

    2017年11月22日00:25:54 对lib下面的TH的大致结构基本上理解了,我阅读pytorch底层代码的目的是为了知道 python层面那个_C模块是个什么东西,底层完全黑箱的话对于理解pyt ...

  4. 记一次sshd启动报错,Failed to start OpenSSH server daemon.

    sshd -t [root@mysql5-slave proj]# sshd -t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  5. (转发)storm 入门原理介绍

    1.hadoop有master与slave,Storm与之对应的节点是什么? 2.Storm控制节点上面运行一个后台程序被称之为什么?3.Supervisor的作用是什么?4.Topology与Wor ...

  6. cxf+spring+soap简单接口开发

    最近学了cxf框架开发webservice,简单搭了个接口,方便后续翻阅,本人才疏学浅,若有不足,请多多谅解! 一.服务端: 1.所用到的jar包: maven的pom.xml配置: <proj ...

  7. svn Advanced

    1.Eclipse last 1.Eclipse 安装subversive插件: 安装连接器: 打开视图"SVN Repositories": 创建“资源库位置”: 配置全局范围文 ...

  8. Android开发 assets目录

    Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.Java里面自动生成该资源文件的ID,所以访问这种资源文件 ...

  9. react portals

    来源:https://segmentfault.com/a/1190000011668286 Portals是react 16.3 提供的官方解决方案,使得组件可以脱离父组件层级挂载在DOM树的任何位 ...

  10. ---dd io测试

    下面是一个简单测试,虽然不够准确但是简单立即可行, 当前目录的IO写读测试: (写) dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync ( ...