Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind failed: Cannot assign requested address

Tags:

c

sockets

I'm trying to write a C program that listens on a port on my machine. I'm running into a strange error.

Whenever I try to bind the socket to a fixed ip (either 127.0.0.1 or my actual IP) I get a "bind failed: Cannot assign requested address" error. However when I pass INADDR_ANY to the bind as the address to bind to, it works.

These are the only two IPs I have so it can't be that the 0.0.0.0 works because of some other IP address I have available.

Here is the code:

#include<sys/types.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int port = 1234;    /* port number */
    int rqst;       /* socket accepting the request */
    socklen_t alen;       /* length of address structure */
    struct sockaddr_in my_addr;    /* address of this service */
    struct sockaddr_in client_addr;  /* client's address */
    int sockoptval = 1;

    int svc;


    /* create a TCP/IP socket */
    if ((svc = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("cannot create socket");
        exit(1);
    }

    /* allow immediate reuse of the port */
    setsockopt(svc, SOL_SOCKET, SO_REUSEADDR, &sockoptval, sizeof(int));

    /* bind the socket to our source address */
    memset((char*)&my_addr, 0, sizeof(my_addr));  /* 0 out the structure */
    my_addr.sin_family = AF_INET;   /* address family */
    my_addr.sin_port = htons(port);
    //my_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Works! */
    my_addr.sin_addr.s_addr = htonl(inet_addr("127.0.0.1")); /* Fails! */

    if (bind(svc, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
        perror("bind failed");
        exit(1);
    }

    printf("Listening on %d\n", my_addr.sin_addr.s_addr);
    /* set the socket for listening (queue backlog of 5) */
    if (listen(svc, 5) < 0) {
        perror("listen failed");
        exit(1);
    }

    /* loop, accepting connection requests */
    for (;;) {
        while ((rqst = accept(svc, (struct sockaddr *)&client_addr, &alen)) < 0) {
            /* we may break out of accept if the system call */
                        /* was interrupted. In this case, loop back and */
                        /* try again */
                        if ((errno != ECHILD) && (errno != ERESTART) && (errno != EINTR)) {
                                perror("accept failed");
                                exit(1);
                        }
                }
        /* the socket for this accepted connection is rqst */
    }
}
like image 832
ffledgling Avatar asked Mar 18 '13 22:03

ffledgling


People also ask

Can't grab with bind Cannot assign requested address?

The message “Cannot assign requested address” suggests that the hostname/IP you are trying to bind does not resolve to a local network interface. A few things to check: Are the full hostnames (eg 01.mongodb.DOMAIN.nl ) resolvable on all of your replica set members?

What does Cannot assign requested address mean?

The error says Cannot assign requested address . This means that you need to use the correct address for one of your network interfaces or 0.0. 0.0 to accept connections from all interfaces.

Can't assign requested address nested exception is Java net Bindexception Cannot assign requested address?

There are some different possible causes for this problem. One is that the local (loopback) socket configuration has been removed from the IP configuration for the system that is logging this message. Another possible cause is that the default IP configuration for the JVM is IPv6 and the IP address is in IPv4 format.


1 Answers

The function inet_addr returns the address already in network order:

The inet_addr() function converts the Internet host address cp from IPv4 numbers-and-dots notation into binary data in network byte order

So drop the htonl.

like image 141
cnicutar Avatar answered Nov 15 '22 11:11

cnicutar