By rohana021
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!
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:
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:
- 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.
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:
- 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.
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).
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.