Destination Address Required
Production Risk
Programming error; UDP sockets require an explicit destination.
EDESTADDRREQ (errno 89) is returned when a send operation is performed on an unconnected socket without providing a destination address.
- 1Calling send() on a UDP socket without calling connect() first
- 2Using write() on an unconnected SOCK_DGRAM socket
- 3sendmsg() with a NULL msg_name on an unconnected socket
send() on an unconnected UDP socket.
int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // send() without connect() or sendto() send(sockfd, buf, len, 0); // Returns -1, errno = EDESTADDRREQ
expected output
send: Destination address required (EDESTADDRREQ)
Fix 1
Use sendto() to specify destination address
WHEN Sending on an unconnected UDP socket
struct sockaddr_in dest = {
.sin_family = AF_INET,
.sin_port = htons(12345),
.sin_addr.s_addr = inet_addr("192.168.1.1")
};
sendto(sockfd, buf, len, 0, (struct sockaddr*)&dest, sizeof(dest));Why this works
sendto() accepts a destination address directly, avoiding the need for connect().
Fix 2
Connect the socket first
WHEN Sending to the same address repeatedly
connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)); // Now send() and write() work send(sockfd, buf, len, 0);
Why this works
connect() on UDP sets the default destination; send() and write() then work without an explicit address.
Linux Programmer Manual send(2)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev