简体   繁体   English

多个并行TCP连接

[英]Multiple parallel tcp connections

My (linux) server has two public IPs and I would like to make some parallel connections, to a same or different servers (my server acts as client program here; it just runs a C++ program that communicates with other servers to fetch some data). 我的(linux)服务器有两个公用IP,我想与同一台或不同的服务器建立一些并行连接(我的服务器在这里充当客户端程序;它只运行一个C ++程序,该程序与其他服务器通信以获取一些数据) 。

Let's suppose I want to stablish 100 parallel connections, is there any difference in performance or stability, from the point of view of the SO, between making 100 connections from a same IP, or 50 connections from the first IP, and another 50 conenctions from the second one? 假设我想建立100个并行连接,从SO的角度来看,在性能或稳定性上是否有任何区别,是从同一个IP进行100个连接,还是从第一个IP进行50个连接,而从另一个IP进行50个连接?第二个?

In other words, is there any difference (is it safer) if I distribute the connections between the different available (local) IPs? 换句话说,如果我在不同的可用(本地)IP之间分配连接,有什么区别(更安全)吗?

MAYBE RELATED : https://stackoverflow.com/a/3923785/1794803 . 可能相关https : //stackoverflow.com/a/3923785/1794803

Outgoing TCP connections have also port numbers assigned with them. 传出TCP连接也为其分配了端口号。 These are 16 bit numbers, resulting in 65.535 possible connections at one single point of time (port 0 has a special meaning). 这些是16位数字,导致在一个时间点有65.535个可能的连接(端口0具有特殊含义)。 After dismantling a connection the TCP protocol requires the connection to stay in a special state TIME-WAIT (see http://www.tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm for a more complete description of the finite state machine). 拆除连接后,TCP协议要求连接保持特殊状态TIME-WAIT(有关有限状态机的更完整说明,请参见http://www.tcpipguide.com/free/t_TCPOperationalOverview和TCPFiniteStateMachineF-2.htm)。 This is usually preconfigured with 60 seconds or so. 通常将其预配置为60秒左右。 With some extra tricks the period the source port ressource stays in TIME-WAIT can be significantly lowered. 通过一些额外的技巧,可以大大降低源端口资源在TIME-WAIT中的停留时间。 However, these two parameters in fact limit the number of connections at a time. 但是,这两个参数实际上限制了一次连接的数量。 All these restrictions apply to a single IP address. 所有这些限制适用于单个IP地址。 If you have n times IP addresses your TCP/IP stack is able to maintain n times many connections. 如果您有n次IP地址,则您的TCP / IP堆栈能够维护n次许多连接。

Be careful with potential NAT gateways between your client and the servers, if you run a huge number of parallel connections these routers' NAT tables may or may not be able to deal with that many connections. 注意客户端和服务器之间可能存在的NAT网关,如果运行大量并行连接,则这些路由器的NAT表可能会也可能无法处理那么多连接。

In general I am not sure if your general architecture suits your use case. 通常,我不确定您的通用体系结构是否适合您的用例。 There may be reasons your servers won't allow only for a limited number of connections. 可能由于某些原因,您的服务器将不允许仅有限数量的连接。 Coding around these shaping mechanisms might just lead to a hare and tortoise race. 围绕这些塑造机制进行编码可能只会导致野兔和乌龟赛跑。

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

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