简体   繁体   English

TCP连接是否占用大量资源?

[英]Are TCP Connections resource intensive?

I have a TCP server that gets data from one (and only one) client. 我有一台TCP服务器,该服务器从一个(只有一个)客户端获取数据。 When this client sends the data, it makes a connection to my server, sends one (logical) message and then does not send any more on that connection. 当此客户端发送数据时,它将与我的服务器建立连接,发送一条(逻辑)消息,然后不再通过该连接发送任何消息。

It will then make another connection to send the next message. 然后它将建立另一个连接以发送下一条消息。

I have a co-worker who says that this is very bad from a resources point of view. 我有一位同事说,从资源的角度来看,这是非常糟糕的。 He says that making a connection is resource intensive and takes a while. 他说建立连接需要大量资源,并且需要一段时间。 He says that I need to get this client to make a connection and then just keep using it for as long as we need to communicate (or until there is an error). 他说,我需要让该客户端建立连接,然后在我们需要沟通的时间内(或者直到出现错误)一直使用它。

One benefit of using separate connections is that I can probably multi-thread them and get more throughput on the line. 使用单独的连接的好处之一是,我可以对它们进行多线程处理,从而获得更多的吞吐量。 I mentioned this to my co-worker and he told me that having lots of sockets open will kill the server. 我向我的同事提到了这一点,他告诉我,打开许多套接字会杀死服务器。

Is this true? 这是真的? Or can I just allow it to make a separate connection for each logical message that needs to be sent. 或者我可以允许它为每个需要发送的逻辑消息建立单独的连接。 (Note that by logical message I mean an xml file that is of variable length.) (请注意,逻辑消息是指长度可变的xml文件。)

The initiation sequence of a TCP connection is a very simple 3 way handshake which has very low overhead. TCP连接的启动序列是一个非常简单的三向握手,开销很低。 No need to maintain a constant connection. 无需保持恒定的连接。

Also having many TCP connections won't kill your server so fast. 另外,拥有许多TCP连接不会很快杀死您的服务器。 modern hardware and operating systems can handle hundreds of concurrect TCP connections, unless you are afraid of Denial of service attacks which are out of the scope of this question obviously. 现代硬件和操作系统可以处理数百个并发的TCP连接,除非您担心拒绝服务攻击显然不在此问题范围之内。

If your server has only a single client, I can't imagine in practice there'd be any issues with opening a new TCP socket per message. 如果您的服务器只有一个客户端,那么在实践中我无法想象每条消息打开一个新的TCP套接字会出现任何问题。 Sounds like your co-worker likes to prematurely optimize. 听起来您的同事喜欢过早地进行优化。

However, if you're flooding the server with messages, it may become an issue. 但是,如果您在服务器上充斥消息,则可能会成为问题。 But still, with a single client, I wouldn't worry about it. 但是,即使只有一个客户,我也不用担心。

Just make sure you close the socket when you're done with it. 完成后,只需确保关闭插座即可。 No need to be rude to the server :) 无需对服务器无礼:)

In addition to what everyone said, consider UDP. 除了大家都说的,还要考虑UDP。 It's perfect for small messages where no response is expected, and on a local network (as opposed to Internet) it's practically reliable. 它非常适用于没有响应的小消息,并且在本地网络(相对于Internet)上实际上是可靠的。

From the servers perspective, it not a problem to have a very large number of connections open. 从服务器的角度来看,打开大量连接不是问题。

How many socket connections can a web server handle? Web服务器可以处理多少个套接字连接?

From the clients perspective, if measuring shows you need to avoid the time initiate connections and you want parallelism, you could create a connection pool. 从客户端的角度来看,如果测量显示需要避免花费时间来启动连接,并且想要并行化,则可以创建一个连接池。 Multiple threads can re-use each of the connections and release them back into the pool when they're done. 多个线程可以重新使用每个连接,并在完成后将它们释放回池中。 That does raise the complexity level so once again, make sure you need it. 这确实提高了复杂性级别,因此请再次确保您需要它。 You could also have logic to shrink and grow the pool based on activity - it would be ashame to hold connections open to the server over night while the app is just sitting their idle. 您还可能具有根据活动来缩小和扩展池的逻辑-在应用程序只是闲置的情况下,整夜保持对服务器的连接保持开放是很丢脸的。

It depends entirely on the number of connections that you are intending to open and close and the rate at which you intend to open them. 这完全取决于您打算打开和关闭的连接数以及您打算打开它们的速率。

Unless you go out of your way to avoid the TIME_WAIT state by aborting the connections rather than closing them gracefully you will accumulate sockets in TIME_WAIT state on either the client or the server. 除非您通过中止连接而不是优雅地关闭连接来避免TIME_WAIT状态,否则您将在客户端或服务器上累积处于TIME_WAIT状态的套接字。 With a single client it doesn't actually matter where these accumulate as the issue will be the same. 对于单个客户端,这些在哪里累积实际上并不重要,因为问题将是相同的。 If the rate at which you use your connections is faster than the rate at which your TIME_WAIT connections close then you will eventually get to a point where you cannot open any new connections because you have no ephemeral ports left as all of them are in use with sockets that are in TIME_WAIT . 如果您使用连接的速率快于TIME_WAIT连接的关闭速率,那么您最终将到达一个无法打开任何新连接的地步,因为没有临时端口,因为所有这些临时端口都在使用TIME_WAIT套接字。

I write about this in much more detail here: http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html 我在这里对此进行了更详细的介绍: http : //www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html

In general I would suggest that you keep a single connection and simply reopen it if it gets reset. 通常,我建议您保留一个连接,并在重置后重新打开它。 The logic may appear to be a little more complex but the system will scale far better; 逻辑看似有些复杂,但系统的伸缩性会更好。 you may only have one client now and the rate of connections may be such that you do not expect to suffer from TIME_WAIT issues but these facts may not stay the same for the life of your system... 您现在可能只有一个客户端,并且连接速率可能不会导致您不希望受到TIME_WAIT问题的困扰,但是这些事实在您的系统寿命中可能并不相同...

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

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