本文介绍了多 TCP 连接 vs 单连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个数据分发器(比如生成随机数),它将为多个客户端提供服务.

I am designing a Data Distributor (say generating random number) where it will serve multiple clients.

客户端 C 首先通过 TCP 向 DD 发送它感兴趣的数字列表,并在 UDP 上侦听数据.一段时间后(几分钟),客户端可能会通过向 DD 发送更多号码来更新其订阅列表.

The client C first sends the list of numbers in which it is interested to DD over TCP and listens for data on UDP. After some time (few minutes) the client may renew its subscription list by sending more numbers to DD.

我可以通过两种方式设计它.

I can design this in 2 ways.

首先:

New_Client_Connected_Thread(int sock_fd)
{
    --Get Subscription
    --Add to UDP Publisher List
    --close(sock_fd)
}

每次客户端想要订阅新的数据集时,它都会建立一个新的 TCP 连接.

Everytime client wants to subscribe to new set of data it will establish a new TCP connection.

第二:

New_Client_Connected_Thread(int sock_fd)
    {
        while(true)
        {
            --wait for new subscription list
            --Get subscription
            --Add to UDP Publisher List.

        }
    }

这里每个客户端只需要 1 个 TCP 连接.

Here only 1 TCP connection per client would be required.

但是,如果客户端不发送新的请求,Client_Thread 将不必要地等待很长时间.

However if the client does not send new request, the Client_Thread would be waiting unnecessarily for long time.

鉴于我的数据分发服务器将为大量客户提供服务,其中哪种方式似乎是有效的方式?

Given that my Data Distributor would be serving lots of clients which of them seems to be the efficient way?

推荐答案

Libevent 或 libev,支持事件驱动循环可能更适合 TCP 部分.

Libevent, or libev, which supports an event driven loop is probably more appropriate for the TCP portion of this.

您可以避免线程,并为 TCP 部分设置一个循环,以将您的客户端添加到发布者列表中.Libevent 在每秒管理大量连接和套接字拆卸方面非常有效,并且被 Tor(洋葱路由器)之类的东西使用

You can avoid the threading, and have a single loop for the TCP portion to add your clients to the Publishers' list. Libevent is very efficient at managing lots and lots of connections and socket teardowns per second and is used by things like Tor (The onion router)

您的应用程序中的 TCP 连接似乎更像是一个控制平面"连接,因此它可能取决于您的客户端需要控制"您的服务器的频率,该服务器将决定是否离开套接字在控制后打开或关闭它.请记住,保持数千个 TCP 连接永久打开会占用主机上的内核资源,但在其他打开和关闭连接上,由于连接设置时间,整个时间都会引入一些延迟.

It seems like the TCP connection in your application is more of a "Control Plane" connection, and thus it's probably going to depend on how often your clients need to "control" your server thats going to make the decision whether to leave the socket open or close it after controlling. Keep in mind that keeping thousands of TCP connections open permanently does it kernel resource on the host, but on the other opening and closing connections the whole time introduces some latency due to the connection setup time.

参见 https://github.com/libevent/libevent/blob/master/sample/hello-world.c 一个 libevent TCP 服务器的例子.

See https://github.com/libevent/libevent/blob/master/sample/hello-world.c for an example of a libevent TCP server.

由于您使用 C++ 进行编码,因此您可能会对 http://llucax.com 感兴趣.ar/proj/eventxx/ libevent 的包装器

Since you're coding in C++, you will probably interested in the http://llucax.com.ar/proj/eventxx/ wrapper for libevent

这篇关于多 TCP 连接 vs 单连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:08