使用 libevent 或 boost::asio 在单线程中建立多个 tcp 连接。有可能吗?
是否可以使用 libevent 在一个线程中创建到不同服务器的多个 tcp 连接?您能编写此类任务的示例实现吗?
我已经这样做了,但不确定是否正确:
...
int num_of_connect = 5; /*for example*/
struct event_base *evbase;
struct bufferevent *bev[num_of_connect];
struct sockaddr_in sin[num_of_connect];
evbase = event_base_new();
for(int i=0;i<=(num_of_connect-1);i++){
sin[i].sin_family = AF_INET;
sin[i].sin_addr.s_addr = inet_addr(/*some addr*/);
sin[i].sin_port = htons(/*some port*/);
bev[i] = bufferevent_socket_new(evbase, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev[i], cb_evread, cb_evwrite, cb_event, NULL);
bufferevent_socket_connect(bev[i], (struct sockaddr *)&sin[i], sizeof(struct sockaddr_in));
}
event_base_dispatch(evbase);
...
另外,类似可以使用boost::asio来实现?例子?)
Is it possible to use libevent for create multiple tcp connections to different servers in one thread? Could you write a sample implementation of such a task?
I have done so, but not sure that it's right:
...
int num_of_connect = 5; /*for example*/
struct event_base *evbase;
struct bufferevent *bev[num_of_connect];
struct sockaddr_in sin[num_of_connect];
evbase = event_base_new();
for(int i=0;i<=(num_of_connect-1);i++){
sin[i].sin_family = AF_INET;
sin[i].sin_addr.s_addr = inet_addr(/*some addr*/);
sin[i].sin_port = htons(/*some port*/);
bev[i] = bufferevent_socket_new(evbase, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev[i], cb_evread, cb_evwrite, cb_event, NULL);
bufferevent_socket_connect(bev[i], (struct sockaddr *)&sin[i], sizeof(struct sockaddr_in));
}
event_base_dispatch(evbase);
...
In addition, similar can be implemented using the boost::asio? Example?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是完全可能的。这是 Boost.Asio 推广的前摄器设计模式的基础。它通过避免诸如
connect
、accept
、read
和write
等阻塞操作来实现并发,而无需使用显式线程。 >。您可能会发现我之前的一些答案在这里很有用正如托尼指出的在 他的答案,Boost.Asio 有精彩的示例,详细解释了异步概念。本教程,特别是异步日间服务器,也是一个很好的起点。
Yes, it is entirely possible. This is the basis of the proactor design pattern promoted by Boost.Asio. It achieves concurrency without the use of explicit threads by avoiding blocking operations such as
connect
,accept
,read
, andwrite
. You might find some of my previous answers useful hereAs Tony pointed out in his answer, the Boost.Asio has fantastic examples explaining the asynchronous concepts in detail. The tutorial, specifically the asynchronous daytime server, is also a good place to start.
您可以使用 boost::asio 在异步接受连接的线程中运行。 boost::asio 文档页面上有示例这将向您展示如何设置一个在单个线程上接受多个连接的服务器。
我不熟悉 libevent 来帮助你。
You could use boost::asio to run in a thread which accepts the connection asynchronously. There is examples on the boost::asio documentation page which will show you how to setup a server which accepts multiple connections on a single thread.
I'm not familiar with libevent to help you there.
是的,有可能。
您还可以使用
evconnlistener_new_bind
创建一个侦听多个端口的服务器。如果您想处理一个或多个信号,那么您可以使用
evsignal_new
将信号添加到事件库。在每种情况下(
bufferevent_socket_new
、evconnlistener_new_bind
和evsignal_new
,每个事件的回调可能会有所不同。Yes, its possible.
You could also create a server that listened on multiple ports with
evconnlistener_new_bind
.And if you wanted to handle one or more signals, then you could use
evsignal_new
to add signals to the event base.In each case (
bufferevent_socket_new
,evconnlistener_new_bind
andevsignal_new
, the callbacks for each event would be likely different.