简体   繁体   中英

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? 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? Or that's unnecessary, because it just sends data out and does not care about whether the recv buff of the side is full? 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? For TCP may return 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 ?

You're using 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

The result of the send() call will be completely independent of the state of the receiver. 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; 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. For a UDP socket that will basically never happen. For a TCP socket that will happen if you write faster than the link or receiving side can handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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