boost::asio 服务器多进程
我想制作一个简单的多进程(不是线程)服务器。我见过一个迭代示例,其中它一次处理一个请求。 相反,我需要同时处理更多请求(更少 10 个)。 在经典的 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
}
是否可以使用 boost 来做类似的事情?我真的不知道如何获取两个不同的套接字,因为在 boost 中所有函数(listen
、bind
、accept
等)返回无效。
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.
In the classic c and c++ examples, I've seen that the server is designed like the following:
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? I really don't know how obtain the two different socket, because in boost all the function (listen
, bind
, accept
, etc.) return void.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,可以使用 Boost.Asio 为每个连接分叉一个进程。请参阅 BSD 套接字 API Boost.Asio 文档,用于将
bind
、listen
、accept
映射到相关 Boost.Asio 类型。不过,正如我在评论中指出的那样,我觉得这种设计根本无法很好地扩展。您最好学习异步设计模式并利用 Boost.Asio 库的真正优势。有关详细信息,请参阅C10K 问题。
Yes, it's possible to use Boost.Asio to fork a process for each connection. See the BSD Socket API Boost.Asio documentation for the mappings for
bind
,listen
,accept
into the relevant Boost.Asio types.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. For more information, see the C10K problem.