Mustafa Chelik
Mustafa Chelik

Reputation: 2184

UDP sendto never fails

My application sends UDP packets to different hosts continuously. When a host is alive, UDP packet reaches to the host and sendto function returns number of bytes sent to it. But when a host gets down (cable unplug or reset), sendto function doesn't send UDP packet but still returns number of bytes in buffer. Instead of sending UDP packets, ARP packets get sent to find the host. Why sendto function doesn't return SOCKET_ERROR? WSAGetLastError function returns 0 also. How can I notice error on sending UDP packets to unreachable host?

P.S. my OS is Winodws 7 x64 and my application is native C++ and 64-bit.

Upvotes: 2

Views: 1565

Answers (2)

user207421
user207421

Reputation: 311050

A UDP sendto() cannot possibly return an error of that nature unless you are sending over a connected UDP socket, in which case you may get an error on a subsequent send.

Upvotes: 0

molbdnilo
molbdnilo

Reputation: 66459

The title of your question - "UDP sendto never fails" - is a pretty good one-line summary of UDP.

That's how UDP works - UDP doesn't care if the destination is reachable.
UDP sendto "succeeds" when the packet leaves your application; it doesn't even need to leave your computer as far as UDP is concerned.

The ARP packages are your computer's attempts to find a way to reach the host, but at that point sendto has already "succeeded".

If you want a reliable protocol, UDP is not what you're looking for.

Upvotes: 8

Related Questions