菜鸟笔记
提升您的技术认知

Boost.Asio C++ 网络编程之七:基于TCP的同步客户端

阅读 : 38

      从本篇开始,我们会深入学习怎样使用Boost.Asio建立更加复杂的客户端和服务端应用。你可以运行并测试它们,而且在理解之后,你可以把它们做为框架来构造自己的应用。

在接下来的例子中:

1.客户端使用一个用户名(无密码)登录到服务端

2.所有的连接由客户端建立,当客户端请求时服务端回应

3.所有的请求和回复都以换行符结尾(’\n’)

4.对于5秒钟没有ping操作的客户端,服务端会自动断开其连接

客户端可以发送如下请求:

1.获得所有已连接客户端的列表

2.客户端可以ping,当它ping时,服务端返回ping ok或者pingclient_list_chaned

为了更有趣一点,我们增加了一些难度:

1.每个客户端登录6个用户连接,比如Johon,James,Lucy,Tracy,Frank和Abby

2.每个客户端连接随机地ping服务端(随机7秒;这样的话,服务端会时不时关闭一个连接)

基于TCP的同步客户端

1.流程图


2.实现

#ifdef WIN32
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#endif

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace boost::asio;
io_service service;

/** simple connection to server:
- logs in just with username (no password)
- all connections are initiated by the client: client asks, server answers
- server disconnects any client that hasn't pinged for 5 seconds

Possible requests:
- gets a list of all connected clients
- ping: the server answers either with "ping ok" or "ping client_list_changed"
*/
struct talk_to_svr {
    talk_to_svr(const std::string & username)
        : sock_(service), started_(true), username_(username) {}
    void connect(ip::tcp::endpoint ep) {
        sock_.connect(ep);
    }
    void loop() {
        // read answer to our login
        write("login " + username_ + "\n");
        read_answer();
        while (started_) {
            // 循环发送ping请求
            write_request();
            read_answer();
            int millis = rand() % 7000;
            std::cout << username_ << " postpone ping "
                << millis << " ms" << std::endl;
            boost::this_thread::sleep(boost::posix_time::millisec(millis));
        }
    }
    std::string username() const { return username_; }
private:
    void write_request() {
        write("ping\n");
    }
    void read_answer() {
        already_read_ = 0;
        read(sock_, buffer(buff_),
            boost::bind(&talk_to_svr::read_complete, this, _1, _2));
        process_msg();
    }
    void process_msg() {
        std::string msg(buff_, already_read_);
        if (msg.find("login ") == 0) on_login();
        else if (msg.find("ping") == 0) on_ping(msg);
        else if (msg.find("clients ") == 0) on_clients(msg);
        else std::cerr << "invalid msg " << msg << std::endl;
    }

    void on_login() {
        std::cout << username_ << " logged in" << std::endl;
        do_ask_clients();
    }
    void on_ping(const std::string & msg) {
        std::istringstream in(msg);
        std::string answer;
        in >> answer >> answer;
        if (answer == "client_list_changed")
            do_ask_clients();
    }
    void on_clients(const std::string & msg) {
        std::string clients = msg.substr(8);
        std::cout << username_ << ", new client list:" << clients;
    }
    // 获得所有已连接客户端的列表
    void do_ask_clients() {
        write("ask_clients\n");
        read_answer();
    }

    void write(const std::string & msg) {
        sock_.write_some(buffer(msg));
    }
    size_t read_complete(const boost::system::error_code & err, size_t bytes) {
        if (err) return 0;
        already_read_ = bytes;
        bool found = std::find(buff_, buff_ + bytes, '\n') < buff_ + bytes;
        return found ? 0 : 1;
    }

private:
    ip::tcp::socket sock_;
    enum { max_msg = 1024 };
    int already_read_;
    char buff_[max_msg];
    bool started_;
    std::string username_;
};

ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);
void run_client(const std::string & client_name) {
    talk_to_svr client(client_name);
    try {
        client.connect(ep);
        client.loop();
    }
    catch (boost::system::system_error & err) {
        // 捕获socket断开原因
        std::cout << "client terminated " << client.username()
            << "——" << err.what() << std::endl;
    }
}

int main(int argc, char* argv[]) {
    boost::thread_group threads;
    char* names[] = { "John", "James", "Lucy", "Tracy", "Frank", "Abby", 0 };
    for (char ** name = names; *name; ++name) {
        threads.create_thread(boost::bind(run_client, *name));
        boost::this_thread::sleep(boost::posix_time::millisec(100));
    }
    threads.join_all();
    system("pause");
}