检测客户端在 C# 中的 TCP 流上(尚未)接收到哪些数据 [英] Detect what data is (not yet) received by client on TCP stream in C#

查看:29
本文介绍了检测客户端在 C# 中的 TCP 流上(尚未)接收到哪些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产生数据的服务器,客户端接收这个数据.只有服务器太快,并且使客户端过载.最终,服务器将阻止他的发送操作.

I have a server that produces data, and the client receives this data. Only the server is too fast, and overloads the client. Eventually, the server will block on his send operations.

现在,我对来自服务器的旧数据并不感兴趣,相反,服务器可以跳过一些消息,只向客户端发送真正重要的内容.

Now, I'm not really interested in old data from the server, rather, the server could skip a few messages and only send what's really important to the client.

在客户端上,我可以使用 Client.Available 来找出流中剩余的数据量,但我无法弄清楚如何在服务器/发件人上获取此数字.我可以更改 SendBufferSize,但我会知道 SendBuffer 中有多少可用空间并对此做出相应反应.

On the client, I can use Client.Available to find out how much data is left in the stream, but I cannot figure out how to get this number on the server/sender. I can change the SendBufferSize, but rather I would know how much space is free in the SendBuffer and react to that accordantly.

我可以让客户报告他落后了多远,但这感觉就像在应用程序级别重新发明 TCP 协议.此外,我不相信已经很慢的客户端及时警告我的服务器.

I could let the client report how far he's lagging behind, but that feels like reinventing the TCP protocol at application level. Also, I do not trust a already slow client to warn my server in time.

有没有办法读取 TCP 使用/未使用的窗口大小或发送缓冲区?

Is there any way to read the TCP used/unused window size or send buffer?

推荐答案

最终,服务器将阻止他的发送操作"

"Eventually, the server will block on his send operations"

坦率地说,上面是这里真正的错误.服务器没有理由仅仅因为某些客户端接收数据的速度不够快而阻塞.服务器应该使用非阻塞、异步 I/O,否则应该继续正常工作,即使客户端读取速度不够快.

Frankly, the above is the real bug here. There's no reason a server should block just because some client isn't receiving data fast enough. The server should be using non-blocking, asynchronous I/O and should otherwise continue to work normally, even if a client isn't reading fast enough.


现在,即使您解决了阻塞问题,您也可能会遇到客户端接收数据不够快的问题.IE.正如您所提到的,您希望客户端不接收它无法处理的数据.您在这里至少有几个选择:


Now, even if you address the blocking issue, you may also have the issue of the client receiving data not quickly enough. I.e. as you mentioned, you want the client to not receive data it can't process. You have at least a few choices here:

  • 要求客户端在发送另一条消息之前主动响应已处理的消息.

优点:这是一个即时解决方案,因为服务器永远不会超过客户端可以处理的传输速率.
缺点:这会增加网络协议的带宽开销和延迟.ping 时间长的客户端会受到影响,即使它能够快速接收数据.

Pro: this is an immediate solution, in the sense that the server will never exceed the transmission rate that the client can handle.
Con: this would add bandwidth overhead and latency to your network protocol. A client with a high ping time will suffer, even if it's otherwise able to receive data quickly.

  • 跟踪客户端似乎能够处理的数据速率,并减慢消息的具体化速度,以便服务器不会超过此速率.

优点:此解决方案将始终保持客户端能够处理的最高传输速率.
缺点:至少在最初,并且可能会间歇性地随着客户自身能力的变化而变化,这可能会暂时超出客户跟上的能力

Pro: this solution will at all times maintain a transmission rate as high as the client is capable of dealing with.
Con: at least initially, and perhaps intermittently as the client's own capacity varies, this may exceed temporarily the client's ability to keep up

  • 使用 UDP,这将允许网络传输层丢弃客户端处理速度不够快的数据报.

优点:此解决方案将整个问题委托给网络传输层,让您不必担心服务器和客户端的真实细节
缺点:UDP 本质上是不可靠的.除了处理丢弃的数据报(这对您来说是一种好处)之外,您还必须准备好处理无序接收的数据报以及多次收到的单个数据报.

Pro: this solution delegates the whole problem to the network transport layer, leaving you to worry about the real details of your server and client
Con: UDP is inherently unreliable. In addition to dealing with dropped datagrams (which in your case is a benefit), you also must be prepared to handle datagrams received out of order, and individual datagrams received more than once.

鉴于问题的广泛性,这是尽可能具体的答案.

That's about as specific an answer as is possible, given the broadly stated question.

这篇关于检测客户端在 C# 中的 TCP 流上(尚未)接收到哪些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆