本文介绍了.NET TCP 协议的可靠性如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C# 很陌生,所以请耐心等待.我正在用 C# .NET 4.0 编写一个相对简单的客户端服务器应用程序.我正在使用 TCP 协议,TCPListener 和 TCPClient 更具体.我知道 TCP 协议在理论上是如何工作的.但我必须 100% 确定在数据传输过程中不会出现(未处理的)错误.

I'm quite new to C# so please bear with me. I'm writing a relatively simple client server application in C# .NET 4.0. I am using TCP protocol, TCPListener and TCPClient to be more specific. I know how does TCP protocol work in theory. But I must be 100% sure that there will be no (unhandled) errors during data transfers.

我发送数据后如何知道数据是否成功接收.我可以完全依赖TCP协议的底层实现吗?所以不需要我向对方确认收到数据了吗?

After I send data how do I know whether data was successfully received. Can I totally rely on the underlying implementation of TCP protocol? So there is no need that I confirm from the other side that data was received?

我真正知道哪些数据被发送和成功接收是至关重要的.我知道这是一个愚蠢的问题,但我真的很想确定.感谢您的时间和回答.

It is crucial that I truly know which data was sent&successfully received.I know it is a dumb question, but I really want to be sure. Thank you for your time and answers.

推荐答案

TCP 保证:

  • 您发送的数据将按照您发送的顺序到达
  • 您发送的数据将与您发送的数据完全相同(未修改)
  • 不会收到其他(虚假)数据

它不能保证啮齿动物不会吃掉你的电缆,建筑物的电源会继续存在,甚至当操作系统告诉它一些数据时,你正在与之交谈的另一台机器上的进程会费心去做任何事情因为它已经到了.

It does not guarantee that rodents will not eat your cables, the power to the building will stay on, or even that the process on the other machine you are talking to will bother to do anything when the OS tells it that some data for it has arrived.

如果您需要对接收到的数据并对其进行操作的肯定确认,您需要手动发回确认(因为 TCP 连接是双工的,您已经有一个通道可以做到这一点).

If you need a positive acknowledgement that data was received and acted upon, you need to send the acknowledgement back manually (since TCP connections are duplex, you already have a channel to do that).

当然,所有这些都不是特定于 .NET、Windows 或任何其他网络堆栈实现的.

Of course all of this does is not in any way specific to .NET, Windows, or any other implementation of a network stack.

更新:我想特别指出的是,操作系统网络栈接受数据传输后,你无法知道过程另一端已收到该数据.在大多数情况下,网络堆栈知道数据已到达目标(通过 TCP ACK 消息),但它不知道目标上的操作系统是否已将它们馈送到它们的目的地进程.因此,发回您自己的数据接收和操作"消息是唯一的选择.

Update: I 'd like to point out specifically that, after the OS network stack accepts data for transmission, there is no way for you to know that the process at the other end has received that data. The network stack knows in most cases that the data has reached the target (through TCP ACK messages), but it does not know if the OS on the target has fed them to the process they are destined for. So sending back your own "data received and acted upon" message is the only option.

这篇关于.NET TCP 协议的可靠性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 00:45