boost asio的fork子进程处理网络请求

本文介绍了一个使用Boost.Asio库实现的服务器程序,该程序通过子进程处理客户端连接,利用信号处理机制确保父进程可以等待并处理子进程的结束。服务器在接收到新的连接请求时会创建一个新的子进程来处理,而父进程继续监听新的连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
#include <string>
#include <array>
#include <boost/asio.hpp>
class server {
public:
    server(boost::asio::io_service &ios, unsigned short port) : ios_(ios), acceptor_(ios_, {boost::asio::ip::tcp::v4(), port}), socket_(ios_), signal_(ios_, SIGCHLD) {
        wait_for_signal();
        accept();
    }
private:
    void wait_for_signal() {
        signal_.async_wait([this] (boost::system::error_code, int) {        // 当lamba函数返回true 则调用 后面在需要调用wait_for_signal()继续执行
            if (true == acceptor_.is_open()) {  // parent process
                std::cout << "start to wait child process." << std::endl;
                int status = 0;
                while (waitpid(-1, &status, WNOHANG) > 0) {
                }
                wait_for_signal();
            }
        });
    }
    void accept() {
        acceptor_.async_accept([this] (boost::system::error_code ec, boost::asio::ip::tcp::socket new_socket) {
            if (ec) {
                std::cerr << "accpetor error:" << ec.message() << std::endl;
                accept();
                return;
            }
            socket_ = std::move(new_socket);
            ios_.notify_fork(boost::asio::io_service::fork_prepare);
            int pid = fork(); 
            if (0 == pid) {
                std::cout << "fork a sub process to go." << std::endl;
                ios_.notify_fork(boost::asio::io_service::fork_child);
                acceptor_.close();
                signal_.cancel();
                read();
            }
            if (pid > 0) {
                std::cout << "parent process to close socket and reaccept." << std::endl;
                ios_.notify_fork(boost::asio::io_service::fork_parent);
                socket_.close();
                accept();
            }
        });
    }
    void read() {
        socket_.async_read_some(boost::asio::buffer(data_), [this] (boost::system::error_code ec, size_t length) {
            if (!ec) {
                std::cout << "server read data." << std::endl;
                write(length);
            }
        });
    }
    void write(size_t length) {
        boost::asio::async_write(socket_, boost::asio::buffer(data_, length), [this] (boost::system::error_code ec, size_t length) {
            if (!ec) {
                read();
            }
        });
    }
private:
    static constexpr size_t BUFF_SIZE = 1024;
private:
    std::array<char, BUFF_SIZE>data_;
    boost::asio::io_service &ios_;
    boost::asio::signal_set signal_;
    boost::asio::ip::tcp::acceptor acceptor_;
    boost::asio::ip::tcp::socket socket_;
};
int main(int argc, const char **argv) {
    if (2 != argc) {
        std::cerr << "usage ./programe port." << std::endl;
        return -1;
    }
    boost::asio::io_service ios;
    try {
        server my_server(ios, atoi(argv[1]));
        ios.run();
    }
    catch (std::exception &e) {
        std::cerr << e.what() << std::endl;
    }
    
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值