简体   繁体   English

持续140个TCP连接?

[英]Persisting 140 TCP connections?

We are currently investigating the most efficient way of communicating between 120-140 embedded hardware devices running on the .NET Micro framework and a server. 我们目前正在研究在.NET Micro框架上运行的120-140个嵌入式硬件设备与服务器之间进行通信的最有效方式。

Each embedded device needs to send to, and request information from the server on a fairly regular basis all in real time through TCP. 每个嵌入式设备都需要通过TCP实时发送并从服务器请求信息。

My question is this: Would it be better to initialise 140 TCP connections to the server, and then hang on to these connections, or initialise a new connection for each requests to and from the devices? 我的问题是:初始化到服务器的140个TCP连接,然后挂起这些连接,或者为每个进出设备的请求初始化一个新连接会更好吗? Would holding on to and managing 140 TCP connections put a lot of strain on the server? 坚持和管理140个TCP连接会给服务器带来很大压力吗?

When the server detects new data in the database it needs to send this new info to 1..* devices (information is targeted to specific devices), if I held on to the 140 connections I would need to do a lookup for the correct connection each time I needed to send information instead of just sending to an IP:PORT associated with the new data. 当服务器检测到数据库中的新数据时,它需要将此新信息发送到1 .. *设备(信息针对特定设备),如果我保持140个连接,我需要查找正确的连接每次我需要发送信息而不是仅发送到IP:与新数据相关联的PORT。

I guess another possibly stupid question would be is it actually possibly to hang on to 140 TCP connections on a single port? 我想另一个可能是愚蠢的问题是它实际上可能挂在单个端口上的140个TCP连接上吗?

Any suggestions/comments are appreciated! 任何建议/意见表示赞赏!

In general you are better maintaining the connections for as long as possible. 通常,您最好尽可能长时间地保持连接。 If you have each device opening a connection each time it sends a message you can end up effectively DoS'ing the server as it ends up with lots of sockets in the TIME_WAIT state taking up space in it's tables. 如果每次设备发送消息时每个设备都打开一个连接,那么最终可以有效地执行服务器操作,因为它最终会在TIME_WAIT状态下占用许多套接字占用其表中的空间。

I worked on a system where there were a bunch of clients talking to a server and while they could be turned on and off regularly, it was still better to maintain the connection (and re-establish it when it had dropped and a new message needed to be sent). 我在一个系统上工作,其中有许多客户端与服务器通信,虽然可以定期打开和关闭,但仍然可以更好地维护连接(并在连接丢失并需要新消息时重新建立连接)被发送)。 You may end up needing to write slightly more complex code, but I've found it to be well worth the effort for the reduced load on the server. 您可能最终需要编写稍微复杂的代码,但我发现在服务器上减少负载是值得的。

Modern operating systems may have bigger buffers than the ones I actually encountered the DoS effect on, but it's fundamentally not the best idea to be using lots of connections like that. 现代操作系统可能比我实际遇到的DoS效果具有更大的缓冲区,但从根本上讲,使用大量连接并不是最好的想法。

Things can get relatively complicated on the client side, especially when the device tends to go to sleep transparently to the application because that means connections will time out while the app thinks they are still open. 在客户端,事情可能变得相对复杂,特别是当设备倾向于透明地睡眠到应用程序时,因为这意味着当应用程序认为它们仍处于打开状态时连接会超时。 When we did this we ended up with relatively complex network code because we needed to deal with the fact that the sockets could (and would) fail as a matter of course and we simply needed to setup a new connection and re-attempt sending the message. 当我们这样做时,我们最终得到了相对复杂的网络代码,因为我们需要处理套接字可能(并且会)失败的事实,我们只需要设置一个新连接并重新尝试发送消息。 You just tuck this code away into your libraries and forget about it once it's done though. 你只需将这些代码放入你的库中,一旦完成它就会忘记它。

In actual fact in practice our initial application had even more complex code because it was dealing with a network library that was semi-aware of the stop start nature of the devices and tried to resend failed messages, sometimes meaning that the same message got sent twice. 事实上,在实践中我们的初始应用程序具有更复杂的代码,因为它处理的是一个网络库,它半知道设备的停止启动性质,并试图重新发送失败的消息,有时意味着相同的消息被发送两次。 We ended up doing an extra layer of communication on top in order to ensure duplicates got rejected. 我们最终在顶部进行了额外的通信层,以确保重复被拒绝。 If you're using C# or regular BSD style sockets you shouldn't have that problem though I'm guessing. 如果您正在使用C#或常规BSD样式的套接字,尽管我猜测你应该没有这个问题。 This was a proprietary library that managed the reconnects but caused headaches with the resends and it's inappropriate default time-outs. 这是一个专有的库,管理重新连接,但导致重新发送令人头痛,并且它是不合适的默认超时。

You usually can connect much more than 140 "clients" to a server (that is with decent network / HW / RAM)... 您通常可以将超过140个“客户端”连接到服务器(具有良好的网络/硬件/ RAM)...

I recommend always to test this sort of thing with real scenarios (load etc.) to decide since there are aspects like network (performance, stability...), HW (server RAM etc.) and SW (what does the server exactly do?) that can only be checked by you. 我建议总是用真实场景(负载等)来测试这类事情,因为有网络(性能,稳定性......),硬件(服务器RAM等)和SW(服务器究竟做什么)等方面?)只能由您检查。

Depending on the protocol you could/should even put some timeout/reconnect mechanism in there. 根据协议,你可以/甚至应该在那里放置一些超时/重新连接机制。

The lookup you mean would be really fast - just use ConcurrentDictionary to hold the needed information with IP:PORT as the key (assuming the server runs on a full .NET 4). 您的意思是非常快 - 只需使用ConcurrentDictionary来保存IP所需的信息:PORT作为密钥(假设服务器在完整的.NET 4上运行)。

For some references see: 有些参考资料请参阅:

EDIT - as per comments: 编辑 - 根据评论:

Holding on to a TCP/IP connection doesn't take much processing client-side... it costs a bit of memory. 坚持TCP / IP连接并不需要客户端处理太多...它需要一点内存。 I would recommend to do a small test (1-2 clients) to check this assumption for your specific case. 我建议做一个小测试(1-2个客户)来检查你的具体情况的假设。

If you are talking about a system with hardware devices then I suggest to go with closing the connection every time the client finishes sending data. 如果您正在谈论具有硬件设备的系统,那么我建议每次客户端完成发送数据时关闭连接。

To make sure the client gets some update from the server, the client can wait for a 5 second period for any data to arrive from the server. 为了确保客户端从服务器获得某些更新,客户端可以等待5秒钟,以便从服务器到达任何数据。 If the data is received within/before this timeframe, then close the connection and process the data. 如果在此时间范围内/之前接收数据,则关闭连接并处理数据。 If not, close the connection and wait after sending next set of data. 如果没有,请关闭连接并在发送下一组数据后等待。

This way scaling becomes much easier. 这种方式缩放变得更容易。 Keeping the connections open always leads to strain on the resources and in my opinion is not necessary unless it is some life-saving device like heart rate monitor, oxygen supply monitor etc., 保持连接打开总是会导致资源紧张,除非是心率监测器,氧气供应监视器等一些救生设备,否则我认为没有必要。

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

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