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.



This textbox defaults to using Markdown to format your answer.

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

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello all,

for anyone that still wants to find the solution:

The EADDRNOTAVAIL error indicates that the requested IP address cannot be assigned. This could be due to various reasons such as the IP address not being valid for the host or the IP address being unavailable.

In such cases, you’ll need to do some troubleshooting. Here is how I’ll go about this:

Check the IP Address

Make sure the IP address you are using is correct and assigned to the host on which you are running the program. If you want to bind the socket to all available network interfaces on the host, you can use the INADDR_ANY constant instead of specifying an IP address:

  1. sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

If you do this, the socket will bind to any available IP address on the host, and you don’t need to specify a specific IP address.

Check if the port is available

Make sure the port number you are trying to bind to is available and not already in use by another process. You can use the netstat or lsof command to check for any processes using the same port:

  1. netstat -tuln | grep 1234

If the port is already in use, you will need to choose a different port number or stop the process using the same port.

Check for permission issues

Binding to a port number lower than 1024 requires root privileges. If you are trying to bind to a port number in this range, make sure you are running the program with root privileges (e.g., using sudo).

Verify that the device is on the same network

Double-check that the device trying to bind the socket is indeed on the same network as the device you are trying to connect to. You can verify this by checking the IP addresses and netmasks of both devices.

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.