简体   繁体   English

.NET TCP协议的可靠性如何?

[英]How reliable is .NET TCP protocol?

I'm quite new to C# so please bear with me. 我对C#很新,所以请耐心等待。 I'm writing a relatively simple client server application in C# .NET 4.0. 我在C#.NET 4.0中编写了一个相对简单的客户端服务器应用程序。 I am using TCP protocol, TCPListener and TCPClient to be more specific. 我使用TCP协议,TCPListener和TCPClient更具体。 I know how does TCP protocol work in theory. 我知道TCP协议在理论上是如何工作的。 But I must be 100% sure that there will be no (unhandled) errors during data transfers. 但我必须100%确定在数据传输过程中不会出现(未处理的)错误。

After I send data how do I know whether data was successfully received. 发送数据后,如何知道数据是否已成功接收。 Can I totally rely on the underlying implementation of TCP protocol? 我可以完全依赖TCP协议的底层实现吗? 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 guarantees that: TCP保证:

  • Data you send will arrive in the order you send it 您发送的数据将按照您发送的顺序到达
  • Data you send will be received exactly as you sent it (unmodified) 您发送的数据将与您发送的数据完全一致(未经修改)
  • No other (spurious) data will be received 不会收到其他(虚假)数据

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. 它不能保证啮齿动物不会吃掉你的电缆,建筑物的电源会继续存在,甚至当你操作系统告诉它有一些数据时,你正在谈论的另一台机器上的过程也会费心去做。到达。

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). 如果您需要确认接收到数据并采取相应措施,则需要手动发送确认(因为TCP连接是双工的,您已经有了通道来执行此操作)。

Of course all of this does is not in any way specific to .NET, Windows, or any other implementation of a network stack. 当然,所有这些都不是特定于.NET,Windows或任何其他网络堆栈实现的。

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. 更新:我想特别指出,在OS网络堆栈接受数据传输之后,您无法知道另一端的进程是否已收到该数据。 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 . 网络堆栈在大多数情况下知道数据已经到达目标(通过TCP ACK消息), 但是它不知道目标上的OS是否已经将它们馈送到它们预定的进程 So sending back your own "data received and acted upon" message is the only option. 因此,发回您自己的“收到并执行的数据”消息是唯一的选择。

With TCP/IP alone, you cannot determine whether any data is received - you would have to layer another protocol on top. 仅使用TCP / IP,您无法确定是否收到任何数据 - 您必须在顶部层叠另一个协议。

If you can find out if some particular data was received, then TCP/IP guarantees all data before that was also received. 如果您可以确定是否收到某些特定数据,那么TCP / IP会在收到之前保证所有数据。

There is no such thing as the '.NET TCP protocol'. 没有“.NET TCP协议”这样的东西。 There is just the TCP protocol, which has been running on the Internet for about 30 years. 只有TCP协议,已经在互联网上运行了大约30年。 It's reliable. 这很可靠。 That was a design goal. 这是一个设计目标。

An option would be to use WCF Reliable Sessions over TCP. 一种选择是使用TCP上的WCF可靠会话 WCF reliable sessions will mask transport failures. WCF可靠会话将掩盖传输故障。

If you really need reliability AND network transport, why not use Message Queues ? 如果您确实需要可靠性网络传输,为什么不使用消息队列 They have transactional guarantee (about as strong as the discipline of the developers working with it, but not stronger than that!) 他们有交易保证(与使用它的开发人员的纪律一样强,但不强于此!)

It's like all your traffic with database-server-type safety (and ditto performance). 这就像你的所有流量都具有数据库服务器类型的安全性(和同上性能)。 I think you can easily configure .NET Remoting to use an MSMQ channel . 我认为您可以轻松配置.NET Remoting以使用MSMQ频道

I've personally never done that , but I've used message queuing in general. 我个人从来没有这样做,但我已经使用消息队列一般。

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

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