简体   繁体   English

在物理断开连接的情况下UDP与TCP

[英]UDP vs. TCP in case of physical loss of connection

I am currently working on my bachelor thesis in computer science on a project where i need a reliable communication for data transfer from several micro-computers that read sensor-values to another computer that stores the values in a database. 我目前正在一个项目上研究计算机科学的学士论文,在该项目中,我需要可靠的通信来将数据从几台读取传感器值的微型计算机传输到另一台将这些值存储在数据库中的计算机。 The thing is that it will be deployed in a quite rough environment and the possibility of physical loss of connection is substantial. 问题是它将部署在非常恶劣的环境中,并且物理断开连接的可能性很大。

I have searched for a while about the differences in UDP and TCP but most of the articles and forums talks about the actual reliability in terms of packet-loss and other things but not about the re-connection that could come in this case. 我搜索了有关UDP和TCP的区别的一段时间,但是大多数文章和论坛都讨论了有关丢包等方面的实际可靠性,但没有讨论这种情况下可能发生的重新连接。

Mostly it looks like TCP would be the correct way for this project since it is a question about reliable communication but I have been thinking about the steps to bind the connection in TCP and UDP and there I would prefer UDP but then having a protocol like DCCP using acknowledgement to make sure that no packets are lost. 通常情况下,TCP似乎是该项目的正确方法,因为这是有关可靠通信的问题,但是我一直在考虑将连接绑定到TCP和UDP的步骤,在那里我更喜欢UDP,但后来有了像DCCP这样的协议使用确认来确保没有数据包丢失。

I would really appreciate some inputs and also reliable references if possible. 如果可能的话,我将不胜感激。

It really depends on how important the data is. 这实际上取决于数据的重要性。 If you don't care about the loss of all packets of information during the outage, go UDP. 如果您不关心中断期间所有信息包的丢失,请使用UDP。 Your life will be easier because you can create a connection (for UDP, this really just means remembering the destination rather than specifying it each time) and blast away without a care in the world. 您的生活将会变得更加轻松,因为您可以创建一个连接(对于UDP,这实际上仅意味着记住目的地,而不是每次都指定目的地),而不必担心世界。

However, if it is important that all data reach the database, then go TCP and adjust the system's retry timers on the sending machine such that it is longer than any practical network outage. 但是,如果所有数据都到达数据库重要,请使用TCP并在发送机上调整系统的重试计时器,以使其比任何实际的网络中断都更长。 The system will happily keep doing retries until the physical connection returns at which point it will deliver all back-data in a flurry. 系统将很乐意继续进行重试,直到物理连接返回为止,此时它将一整天都传递所有后台数据。

From https://drupal.star.bnl.gov/STAR/blog-entry/jeromel/2009/feb/18/tcp-parameters-linux-kernel ... https://drupal.star.bnl.gov/STAR/blog-entry/jeromel/2009/feb/18/tcp-parameters-linux-kernel ...

tcp_retries1 - INTEGER How many times to retry before deciding that something is wrong and it is necessary to report this suspection to network layer. tcp_retries1-INTEGER在确定有问题之前重试多少次,有必要将此怀疑报告给网络层。 Minimal RFC value is 3, it is default, which corresponds to ~3sec-8min depending on RTO. 最小RFC值为3,它是默认值,取决于RTO对应于〜3sec-8min。

tcp_retries2 - INTEGER How may times to retry before killing alive TCP connection. tcp_retries2-INTEGER在终止有效的TCP连接之前重试的时间。 RFC1122 says that the limit should be longer than 100 sec. RFC1122表示限制应大于100秒。 It is too small number. 数量太少。 Default value 15 corresponds to ~13-30min depending on RTO. 根据RTO,默认值15对应于〜13-30min。

Setting these into the hundreds or thousands would effectively retry forever. 将它们设置成成百上千将有效地永远重试。 I didn't see a "maximum retry interval" but it's not as important. 我没有看到“最大重试间隔”,但它并不那么重要。

You can do the same on Windows but you don't actually have to if the data is flowing all one way. 您可以在Windows上执行相同的操作, 但是实际上不必一定要以一种唯一的方式传输数据。 Why? 为什么? Because if there is no data to send then there is no data to be lost and retried and thus there is no way to detect that the connection has been interrupted and thus it'll never be considered "broken". 因为如果没有要发送的数据,那么就没有丢失和重试的数据,因此无法检测到连接已被中断,因此永远不会将其视为“断开”。 As long as keep-alive is is disabled, anyway, and you certainly don't want to go about enabling it for this use-case. 无论如何,只要保持活动被禁用,您当然也不想在此用例中启用它。

If for some reason you are unable to do this (limited machine control, for example, then you'll have to implement your own protocol over UDP that sends acknowledgements and does retries without ever giving up. There is code for " pseudo tcp " that can do this but it won't be as good as real TCP if you can get that solution to work. 如果由于某种原因您不能执行此操作(例如,有限的计算机控制,则必须通过UDP实现自己的协议,该协议发送确认并重试,而不会放弃。存在用于“ 伪tcp ”的代码,可以做到这一点,但是如果您能够使该解决方案正常工作,它将不如真正的TCP好。

If the data is to important I would definatly use TCP! 如果数据很重要,我肯定会使用TCP! But on top of that I would also build in a function for the application to determine the connectivity to the server and only send information when the server is reachable. 但除此之外,我还将为应用程序内置一个函数,以确定与服务器的连接性,并仅在服务器可访问时发送信息。 This would also mean making sure to buffer the data which is collected while there is no connectivity. 这也意味着确保在没有连接的情况下缓冲收集的数据。

This would make sure you don't loose data no matter if it's a physical och logical connection issue. 这将确保您无论物理问题还是逻辑连接问题都不会丢失数据。

My suggestion is do not use raw TCP or UDP (TCP/IP socket programming) directly. 我的建议是不要直接使用原始TCP或UDP(TCP / IP套接字编程)。 Choose a reliable communication framework and it will handle all the unreliable connection loss issue underneath. 选择一个可靠的通信框架,它将处理下面所有不可靠的连接丢失问题。 For example, if it's on Windows platform, you can choose WCF. 例如,如果它在Windows平台上,则可以选择WCF。 HTH. HTH。

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

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