问题描述
我在 Windows 上使用阻塞的 C 套接字.我使用它们将数据的更新从服务器发送到客户端,反之亦然.我以高频率(每 100 毫秒)发送更新.send()
函数是否会在结束前等待接收方recv()
接收到数据?
I use blocking C sockets on Windows.I use them to send updates of a data from the server to the client and vice versa. I send updates at a high frequency (every 100ms). Does the send()
function will wait for the recipient recv()
to receive the data before ending ?
如果我理解手册页,我认为不会:
I assume not if I understand well the man page:
成功完成 send() 并不能保证消息的传递."
那么如果一个运行 10 个 send()
出现而另一个只完成 1 个 recv()
会发生什么?
So what will happen if one is running 10 send()
occurences while the other has only complete 1 recv()
?
我需要使用某种确认系统吗?
Do I need to use so some sort of acknowledgement system ?
推荐答案
让我们假设您正在使用 TCP.当您调用 send 时,您正在发送的数据会立即放置在传出队列中,然后发送成功完成.但是,如果 send 无法将数据放入传出队列,则 send 将返回错误.
Lets assume you are using TCP. When you call send, the data that you are sending is immediately placed on the outgoing queue and send then completes successfully. If however, send is unable to place the data on the outgoing queue, send will return with an error.
由于 Tcp 是有保证的传递协议,因此只有在远程端收到确认后才能删除传出队列中的数据.这是因为如果没有及时收到 ack,可能需要重新发送数据.
Since Tcp is a guaranteed delivery protocol, the data on the outgoing queue can only be removed once acknowledgement has been received by the remote end. This is because the data may need to be resent if no ack has been received in time.
如果远端反应迟钝,传出队列将填满数据,然后发送将阻塞,直到有空间将新数据放入传出队列.
If the remote end is sluggish, the outgoing queue will fill up with data and send will then block until there is space to place the new data on the outgoing queue.
然而,连接可能会失败,以至于无法发送任何进一步的数据.尽管一旦 TCP 连接关闭,任何进一步的发送都会导致错误,但用户无法知道实际上有多少数据到达了另一端.(我不知道从套接字到用户应用程序检索 TCP 簿记的方法).因此,如果需要确认收到数据,您可能应该在应用程序级别实现这一点.
The connection can however fail is such a way that there is no way any further data can be sent. Although once a TCP connection has been closed, any further sends will result in an error, the user has no way of knowing how much data did actually make it to the other side. (I know of no way of retrieving TCP bookkeeping from a socket to the user application). Therefore, if confirmation of receipt of data is required, you should probably implement this on application level.
对于UDP,我认为不言而喻,必须以某种方式报告已收到或未收到的内容.
For UDP, I think it goes without saying that some way of reporting what has or has not been received is a must.
这篇关于C 套接字:发送是否等待接收结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!