使用 libevent 或 boost::asio 在单线程中建立多个 tcp 连接。有可能吗?

发布于 2024-11-01 09:10:28 字数 782 浏览 2 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-11-08 09:10:28

此外,类似的还可以
使用 boost::asio 实现?
例子?)

是的,这是完全可能的。这是 Boost.Asio 推广的前摄器设计模式的基础。它通过避免诸如 connectacceptreadwrite 等阻塞操作来实现并发,而无需使用显式线程。 >。您可能会发现我之前的一些答案在这里很有用

正如托尼指出的在 他的答案,Boost.Asio 有精彩的示例,详细解释了异步概念。本教程,特别是异步日间服务器,也是一个很好的起点。

In addition, similar can be
implemented using the boost::asio?
Example?)

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, and write. You might find some of my previous answers useful here

As 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.

半葬歌 2024-11-08 09:10:28

您可以使用 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.

冰火雁神 2024-11-08 09:10:28

是否可以使用 libevent 在一个线程中创建到不同服务器的多个 tcp 连接?您能编写此类任务的示例实现吗?

是的,有可能。

您还可以使用 evconnlistener_new_bind 创建一个侦听多个端口的服务器。

如果您想处理一个或多个信号,那么您可以使用 evsignal_new 将信号添加到事件库。

在每种情况下(bufferevent_socket_newevconnlistener_new_bindevsignal_new,每个事件的回调可能会有所不同。

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?

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 and evsignal_new, the callbacks for each event would be likely different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文