UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2011-11-01 06:25 PM

giobaxx
Member
Registered: 2011-11-01
Posts: 11

Bind Error: "Cannot assign requested address"

Hello to everyone and sorry for my english...
I'm studying socket programming, and i was trying to do some very simple example
like this:

# include <strings.h>
# include <stdlib.h>
# include <netinet/in.h>
# include <stdio.h>
# include <sys/types.h>
# include <sys/socket.h>

int main(int argc, char *argv[]){
    struct sockaddr_in myself, peer;
    int peer_len = sizeof(peer);
    int b_len;

    int s, i;
    char buf[1500];
    if (argc < 2) {
	fprintf (stderr, "Usage: %s port_number\n", argv[0]);
	exit(1);
    }
    if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
      perror("socket");
      exit (1);
    }

    bzero((char *) &myself, sizeof(myself));
    myself.sin_family=AF_INET;
    //myself.sin_addr.s_addr=INADDR_ANY;
	myself.sin_addr.s_addr=inet_addr("10.10.10.10");
    myself.sin_port= htons(atoi(argv[1]));

    if (bind(s, (struct sockaddr *) &myself, sizeof(myself))==-1) {
       perror("bind");
       exit(1);
    }

    while(1){
       if (recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &peer, &peer_len)==-1){
           perror("recvfrom()");
           exit(1);
       }
       printf("Received packet from %s:%d\nData: %s\n\n",
       inet_ntoa(peer.sin_addr), ntohs(peer.sin_port), buf);
    }
}

When i use this command "myself.sin_addr.s_addr=INADDR_ANY" to assign an ip Address i don't have problem, but when i want to try to assign any other IP address like 10.10.10.x i receive the error  "Cannot assign requested address". and i don't know where is my error...i'm a beginner:-(
I work on a Guest LINUX Ubuntu installed in Virtual BOX

Offline

#2 2011-11-01 07:47 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: Bind Error: "Cannot assign requested address"

In general, you can only bind() to IP addresses that are assigned to one of your local interfaces...  (With the exception of broadcast/multicast addresses...)  Linux allows overriding this globally via "/proc/sys/net/ipv4/ip_nonlocal_bind", or on a socket-by-socket basis via setsockopt(IP_FREEBIND) (though that seems to be undocumented on my system, and requires manual inclusion of <linux/in.h>)...

So, I'm guessing 10.10.10.10 is not one of your interfaces' IPs...  Do "ifconfig" to see all your local interfaces and their configured IPs...

Offline

#3 2011-11-01 08:22 PM

giobaxx
Member
Registered: 2011-11-01
Posts: 11

Re: Bind Error: "Cannot assign requested address"

:-( tanks a lot..that address(10.10.10.10) is not the address of my interface. i have to develop a very basic psuedo-NAT using DGRAM_SOCK.
Cliend sent a messagge, and Server receive a packet and change IP by some rule. After change ip address i thought to write a second server that will receive that packet, but if i cannot bind to a generical IP i can't realized. the problem is that the client and the two server(i suppose) are running on the same machine...with the same IP ADDRESS


TANKSSS!!

Offline

#4 2012-03-20 01:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: Bind Error: "Cannot assign requested address"

Completely off-topic, but I just had to comment on how attractive this single thread apparently is to spam-bots!  It's amazing...  At least once a week, and usually several times per week, this one thread alone is filled with spam from "guest" users...  Just this thread, though!  This morning, I just deleted 5 spam posts to this one thread...  I don't know WTF it is about it, but the spam-bots apparently dig it...  Really damn weird...

Offline

#5 2012-03-21 01:07 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,235

Re: Bind Error: "Cannot assign requested address"

I noticed that as well. We could try closing this thread to see if they move on or if it stops them,
out of morbid curiosity.

Offline

Board footer

Powered by FluxBB