简体   繁体   English

在C和C ++中发送有关UDP套接字的信息

[英]send about UDP socket in C and C++

  1. A non-block UDP socket called "connect" and use "send" to send data, is it necessary to use "select/epoll/kqueue" to test whether it is writable? 一个名为“ connect”并使用“ send”发送数据的无阻塞UDP套接字,是否有必要使用“ select / epoll / kqueue”来测试其是否可写? In the case, the other side recv buffer is full, "send" may fail, so I can use "select" to test that it's writable so that "send" may return success? 在这种情况下,另一端的recv缓冲区已满,“发送”可能会失败,因此我可以使用“选择”来测试它是否可写,以便“发送”可以返回成功? Or that's unnecessary, because it just sends data out and does not care about whether the recv buff of the side is full? 还是没有必要,因为它只是发送数据,而不在乎侧面的recv buff是否已满? If that's true, the "send" may only return success unless the other side is down? 如果是这样,那么“发送”可能只会返回成功,除非另一端失败了?
  2. For the UDP socket, what's the return value of "send", for example, I send 100 bytes, it may only return -1(error) or 100, no other value? 对于UDP套接字,“ send”的返回值是多少,例如,我发送100个字节,它可能仅返回-1(错误)或100,没有其他值? For TCP may return 50 ~~~~ 对于TCP可能会返回50 ~~~~

In the case, the other side recv buffer is full, "send" may fail, so i can use "select" to test that it's writable so that "send" may return success ? 在这种情况下,另一端的recv缓冲区已满,“发送”可能会失败,因此我可以使用“选择”来测试其是否可写,以便“发送”可以返回成功?

You're using UDP. 您正在使用UDP。 The packet gets sent, and if the receive buffer is full on the receiving end, the receiving system will just drop it. 数据包被发送,并且如果接收端的接收缓冲区已满,则接收系统将丢弃它。 Read this: https://en.wikipedia.org/wiki/User_Datagram_Protocol#Reliability_and_congestion_control_solutions 阅读此内容: https : //en.wikipedia.org/wiki/User_Datagram_Protocol#Reliability_and_congestion_control_solutions

The result of the send() call will be completely independent of the state of the receiver. send()调用的结果将完全独立于接收者的状态。 It will return success even if the receive side doesn't exist. 即使接收方不存在,它也会返回成功。

See this for how UDP handles datagrams larger than the underlying network's packet size: UDP Sockets on Linux; 有关UDP如何处理大于基础网络的数据包大小的数据报,请参见此内容。 send successfully but unable to receive large buffer 发送成功但无法接收大缓冲区

In general, send() will not block, and always return the number of bytes written, unless the local (sending) buffer in the kernel is full. 通常,除非内核中的本地(发送)缓冲区已满,否则send()不会阻塞,并且始终返回写入的字节数。 For a UDP socket that will basically never happen. 对于UDP套接字,这基本上不会发生。 For a TCP socket that will happen if you write faster than the link or receiving side can handle. 对于TCP套接字,如果您编写的速度超出链接或接收方的处理能力,则会发生这种情况。

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

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