Report this

What is the reason for this report?

A bind socket request gives a EADDRNOTAVAIL

Posted on June 19, 2018

I have a C program where I am trying to bind to a socket at a certain ip:port. Here it the program -

int main () { int udp_fd = -1; struct sockaddr_in sockaddr;

char *ip = (char *)"xx.yyy.zzz.aaa";
int port = 1234;
udp_fd = socket(AF_INET, SOCK_DGRAM, 0);

if (udp_fd == -1) {
   printf("Could not create socket\n");
   return -1;
}

sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = inet_addr(ip);
sockaddr.sin_port = htons(port);

if (bind(udp_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
    printf("Could not bind to %s: %d: %d: %d\n", ip, port, errno, udp_fd);
    return -1;
}

if (fcntl(udp_fd, F_SETFL, O_NONBLOCK | O_ASYNC) < 0) {
    printf("Error setting socket as non-blocking \n");
    return -1;
}

return 0;

}

This fails with EADDRNOTAVAIL define EADDRNOTAVAIL 99 /* Cannot assign requested address */

I try to connect to the same server from another device that is on the same network as the other device that fails the bind and it is successful.

There are no firewalls enabled on the failing device.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.