首页 > 代码库 > boost 编写finger服务

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...

boost 编写finger服务