boost 编写finger服务

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

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

1 最简单的链接

复制代码
 1 #include <ctime>
 2 #include <iostream>
 3 #include <string>
 4 #include <boost/bind.hpp>
 5 #include <boost/shared_ptr.hpp>
 6 #include <boost/enable_shared_from_this.hpp>
 7 #include <boost/asio.hpp>
 8 
 9 
10 using boost::asio::ip::tcp;
11 
12 
13 int main()
14 {
15     try{
16         boost::asio::io_service io_service;
17         io_service.run();
18     }
19     catch (std::exception& e) {
20         std::cerr << e.what() << std::endl;
21     }
22 
23     std::cout << "finished!" << std::endl;
24     return 0;
25 }
复制代码

 运行显示如下

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 = 1123;

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 0;
}
复制代码

 运行显示如下:

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 = 1123;

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 0;
}
复制代码

 运行并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 = 1123;

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")+1),
                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 0;
}
复制代码

  运行后显示如下

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 = 1123;

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 0;
}
复制代码

 

  运行后显示如下

new a connection
new a connection
dda
new a connection
DEF

 

telnet客户端显示

PASS

遗失对主机的连接。

 

 

 

 

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

telnet 127.0.0.1 1123
正在连接127.0.0.1...

posted on   itdef  阅读(386)  评论(1编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示