简体   繁体   English

boost :: asio服务器多进程

[英]boost::asio server multi-process

I would to make a simple multi process (not thread) server. 我会做一个简单的多进程(不是线程)服务器。 I've seen the iterative example in which it handles one request at a time. 我已经看到了一个迭代示例,其中它一次处理一个请求。 Instead I need to handle more requests(more on less 10) at the same time. 相反,我需要同时处理更多的请求(少于10个,更多)。 In the classic c and c++ examples, I've seen that the server is designed like the following: 在经典的c和c ++示例中,我已经看到服务器的设计如下:

int listensd, connsd; // listening socket and conection socket
pid_t pid;            //process id
listensd=socket(....); 
bind(listensd,...);
listen(listensd,...);
for(;;)
{

  connsd=accept(listensd,...);
  if((pid=fork())==0)  //child process
  {
        close(listensd);  //close the listen socket
        do_it(connsd);    //serve the request
        close(connsd);    //close the connection socket
        exit(0); 
   }
close(connsd);     //the parent closes the connection socket
}

Is it possible to do something like that with boost? 是否可以通过boost做类似的事情? I really don't know how obtain the two different socket, because in boost all the function ( listen , bind , accept , etc.) return void. 我真的不知道如何获得两个不同的套接字,因为在boost中,所有函数( listenbindaccept等)都返回void。

Yes, it's possible to use Boost.Asio to fork a process for each connection. 是的,可以使用Boost.Asio为每个连接派生一个进程。 See the BSD Socket API Boost.Asio documentation for the mappings for bind , listen , accept into the relevant Boost.Asio types. 请参阅BSD Socket API Boost.Asio文档以获取bindlistenaccept到相关Boost.Asio类型的映射。

Though, as I pointed out in my comment, I don't feel this design scales well at all. 但是,正如我在评论中指出的那样,我认为这种设计的扩展性根本不好。 You're better off learning asynchronous design patterns and using the real strengths of the Boost.Asio library. 您最好学习异步设计模式并利用Boost.Asio库的真正优势。 For more information, see the C10K problem . 有关更多信息,请参见C10K问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM