简体   繁体   English

C 中的 TCP Sockets:recv() function 是否触发发送 ACK?

[英]TCP Sockets in C: Does the recv() function trigger sending the ACK?

Im working with TCP Sockets in C but yet dont really understand "how far" the delivery of data is ensured.我在 C 中与 TCP Sockets 一起工作,但还没有真正理解“多远”的数据交付得到保证。

My main problem is that in my case the server sometimes sends a message to the client and expects an answer shortly after.我的主要问题是,在我的情况下,服务器有时会向客户端发送一条消息,并希望很快得到答复。 If the client doesnt answer in time, the server closes the connection.如果客户端没有及时应答,服务器将关闭连接。 When reading through the manpages of the recv() function in C, I found the MSG_PEEK Flag which lets me look/peek into the Stream without actually reading the data.在阅读 C 中 recv() function 的联机帮助页时,我发现了 MSG_PEEK 标志,它让我可以在不实际读取数据的情况下查看/窥视 Stream。

But does the server even care if I read from the stream at all?但是服务器是否关心我是否从 stream 读取?

Lets say the server "pushes" a series of messages into the stream and a Client should receive them.假设服务器将一系列消息“推送”到 stream 中,客户端应该接收它们。 As long as the Client doesnt call recv() those messages will stay in the Stream right?只要客户端不调用 recv(),这些消息就会保留在 Stream 中,对吗? I know about ACK messages being send when receiving data, but is ACK sent when i call the recv() function or is the ACK already sent when the messsage successfully reached its destination and could (emphasising could) be received by the client if it choses to call recv()?我知道接收数据时正在发送 ACK 消息,但是当我调用 recv() function 时是否发送了 ACK,或者当消息成功到达目的地时是否已经发送了 ACK,并且如果它选择,客户端可以(强调可以)接收调用 recv()?

My hope is to trick the server into thinking the message wasnt completely send yet, because the client has not called recv() yet.我的希望是欺骗服务器认为消息还没有完全发送,因为客户端还没有调用 recv()。 Therefore the Client could already evaluate the message by using the MSG_PEEK flag and ensure it always answers in time.因此,客户端已经可以通过使用 MSG_PEEK 标志评估消息并确保它始终及时答复。 Of course I know the timout thing with my server depends on the implementation.当然我知道我的服务器超时取决于实现。 My question basically is, if PEEKING lets the server think the message hasnt reached it destination yet or if the server wont even care and when ACK is sent when using recv().我的问题基本上是,如果 PEEKING 让服务器认为消息还没有到达它的目的地,或者服务器是否甚至不关心以及何时在使用 recv() 时发送 ACK。

I read the manpages on recv() and wiki on TCP but couldnt really figure out how recv() takes part in the process.我阅读了 recv() 的联机帮助页和 TCP 上的 wiki,但无法真正弄清楚 recv() 如何参与该过程。 I found some similar questions on SO but no answer to my question.我在 SO 上发现了一些类似的问题,但没有回答我的问题。

TL;DR长话短说

Does the recv() function trigger sending the ACK? recv() function 是否触发发送 ACK?

No, not on any regular OS.不,不在任何常规操作系统上。 Possibly on an embedded platform with an inefficient.network stack.可能在具有低效网络堆栈的嵌入式平台上。 But it's almost certainly the wrong problem anyway.但无论如何,这几乎肯定是错误的问题。


Your question about finessing the details of ACK delivery is a whole can of worms.你关于处理 ACK 传递细节的问题是一大堆蠕虫。 It's an implemention detail, which means it is highly platform-specific.它是一个实现细节,这意味着它是高度特定于平台的。 For example, you may be able to modify the delayed ACK timer on some TCP stacks, but that might be a global kernel parameter if it even exists.例如,您可以修改某些 TCP 堆栈上的延迟 ACK 计时器,但这可能是全局 kernel 参数(如果它存在的话)。

However, it's all irrelevant to your actual question.但是,这与您的实际问题无关。 There's almost no chance the server is looking at when the packet was received, because it would need it's own TCP stack to even guess that, and it still wouldn't be reliable (TCP retrans can keep backing off and retrying for minutes ).服务器几乎没有机会查看何时收到数据包,因为它需要它自己的 TCP 堆栈才能猜测到这一点,而且它仍然不可靠(TCP 重传可以保持后退并重试几分钟)。 The server is looking at when it sent the data, and you can't affect that.服务器正在查看发送数据的时间,您不能影响它。

The closest you could get is if the server uses blocking writes and is single-threaded and you fill the receive window with un-acked data.您可以获得的最接近的情况是服务器使用阻塞写入并且是单线程的并且您使用未确认的数据填充接收 window。 But that will probably delay the server noticing you're late rather than actually deceiving it.但这可能会延迟服务器注意到您迟到而不是真正欺骗它。

Just make your processing fast enough to avoid a timeout instead of trying to lie with TCP.只需让您的处理速度足够快以避免超时,而不是试图撒谎 TCP。

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

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