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 udpfd = -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.

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!