运行 另一个线程中的 tcp 服务器

Run tcp server in another thread

我在不同线程中与 运行 tcp 服务器作斗争。所以我有这样的东西:

#include <ctime>
#include <iostream>
#include <string>

#include <boost/asio.hpp>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}

class TcpServer
{
private:
    boost::asio::io_context& io_context_;
    boost::asio::ip::tcp::acceptor acceptor_;
    boost::asio::ip::tcp::socket socket_;
    
public:
    TcpServer(boost::asio::io_context& io_context)
        : io_context_{io_context}
        , socket_(io_context_)
        , acceptor_(io_context_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 512))
    {
        acceptor_.accept(socket_);
        std::string message = make_daytime_string();

        boost::system::error_code ignored_error;
        socket_.write_some(boost::asio::buffer(message), ignored_error);
    }
};


int main()
{
    boost::asio::io_context io_cont;
    std::thread t([&io_cont] ()
    {
        TcpServer server(io_cont);
    });

    std::cout << "I want to display it while server is still running";

    t.join();
    return 0;
}

基本上这个 TcpServer 一直是 运行,直到它获得连接、发送日期时间然后退出。 我想做的是:

  1. 运行服务器
  2. 继续在主函数中处理我的东西
  3. 发送指令nc 127.0.0.1 512
  4. 节目结束

关于使用 io_context 有一些困惑;你似乎认为操作 运行 在上下文中,但它们不会 除非你使用 async_ 版本

其他旁注:

  • write_some 不(需要)发送整个缓冲区。请改用 boost::asio::write

这里有一个更简单的例子可以澄清混淆:

Live On Coliru

#include <boost/asio.hpp>
#include <ctime>
#include <iostream>
#include <string>
using namespace std::chrono_literals;
using boost::asio::ip::tcp;

static std::string make_daytime_string()
{
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}

class TcpServer {
  public:
    void run() {
        tcp::acceptor acceptor_(io_context_, {{}, 5120});

        boost::system::error_code ec;
        while (!ec) {
            auto socket_ = acceptor_.accept();
            std::string message = make_daytime_string();
            boost::asio::write(socket_, boost::asio::buffer(message), ec);
        }
    }
  private:
    boost::asio::io_context io_context_;
};

int main()
{
    auto server = TcpServer{};
    std::thread t([&server]() { server.run(); });

    for (;; std::this_thread::sleep_for(1s))
        std::cout << "I want to display it while server is still running" << std::endl;

    t.join();
}

版画

./a.out& for a in {1..9}; do sleep .5; nc 127.0.0.1 5120 <<< ''; done
kill %1
I want to display it while server is still running
Mon May 24 17:41:35 2021
I want to display it while server is still running
Mon May 24 17:41:35 2021
Mon May 24 17:41:36 2021
I want to display it while server is still running
Mon May 24 17:41:36 2021
Mon May 24 17:41:37 2021
I want to display it while server is still running
Mon May 24 17:41:37 2021
Mon May 24 17:41:38 2021
I want to display it while server is still running
Mon May 24 17:41:38 2021
Mon May 24 17:41:39 2021