简体   繁体   English

在java中维护TCP套接字的成本有多高

[英]How expensive is maintaining a TCP socket in java

I have currently implemented a heartbeat mechanism for my project and I am using TCP as my underlying connectivity. 我目前为我的项目实现了一个心跳机制,我使用TCP作为我的底层连接。 I was wondering how expensive is maintaining a tcp connection. 我想知道维护tcp连接有多昂贵。 Each slave sends a heartbeat every 5 seconds to the master(yes i know its way to often but i have a good reason to do so). 每个从属设备每隔5秒向主设备发送一次心跳(是的,我知道经常这样做,但我有充分的理由这样做)。 So I was wondering should I constantly create a new connection or should I keep the connection open. 所以我想知道我应该不断创建一个新连接,还是应该保持连接打开。 Because if I keep the open connection then in that case I can just handle the exception. 因为如果我保持打开连接,那么在这种情况下我可以处理异常。 But since I need to know every 5 seconds wether or not a slave is down should I be re establishing the connection or just keep it open. 但是,因为我需要知道每5秒钟一个奴隶是否已关闭我应该重新建立连接还是保持打开状态。 Thanks in advance. 提前致谢。

It's more expensive to re-open the connection regularly; 定期重新打开连接会更昂贵; there is a three-way handshake on open. 开放时有三方握手 Once the socket is open, that cost can be amortized (but only if you leave it open). 套接字打开后,该费用可以摊销(但仅限于您将其保持打开状态)。

As previously stated reopening the connection is more expensive, unless there are other factors involved like mobility. 如前所述,除非涉及移动性等其他因素,否则重新打开连接会更加昂贵。 Not only do you have a three-way handshake, but you also have a four-way handshake terminating the connection. 您不仅可以进行三次握手,还可以通过四次握手来终止连接。 In addition to this, your TCP server application likely open up new threads for each new connection, which also needs to be allocated, deallocated etc. Most likely your connection will also pass through firewalls which are often NATed, which in turn opens up ports and states. 除此之外,您的TCP服务器应用程序可能为每个新连接打开新线程,这些新连接也需要分配,解除分配等。很可能您的连接也将通过防火墙,这些防火墙通常是NAT,这反过来会打开端口和状态。 This is why I personally rarely use UDP, because UDP may have problems passing through firewalls and ISP filters. 这就是我个人很少使用UDP的原因,因为UDP可能在通过防火墙和ISP过滤器时遇到问题。

Finally the maintenance of the TCP connection itself from a protocol point of view is minimal. 最后,从协议的角度来看,TCP连接本身的维护是最小的。 TCP do have the option of keep-alive , but these are rarely sent as often as every 5 seconds. TCP确实具有keep-alive选项,但这些很少每5秒发送一次。 There is a small overhead of context switching within your OS process, but that would happen regardless of you opening and closing the connection. 在您的操作系统过程中,上下文切换的开销很小,但无论您打开和关闭连接,都会发生这种情况。

Keep it open. 保持开放。

If you are doing a heartbeat, why not use UDP, TCP requires confirmation of receipt, UDP does not, then you can have a listener and just receive any packets that are broadcast to it and process. 如果你正在做一个心跳,为什么不使用UDP,TCP要求确认接收,UDP不要,然后你可以有一个监听器,只是接收任何广播给它和处理的数据包。 meaning one port open on the main system no matter how many children are sending to it. 意味着一个端口在主系统上打开,无论有多少孩子发送到它。

TCP you would require one connection per child. TCP每个孩子需要一个连接。

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

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