klein fabien wrote:

> OK ,I removed the bind call because I allways received the same
> message :
> 
> Cannot assign requested address
> 
> here is the bind call
> 
> 
> ....
>       Destination.sin_family = AF_INET;
>       //Destination.sin_addr.s_addr = 0xC7215111;
>       Destination.sin_addr.s_addr = 0xC621518B; 
>       Destination.sin_port = htons (TEST_PORT);
> 
>       //
>       //Open a socket
>       //
>       Socket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
>       if (Socket < 0)
>       {
>               printf("socket Error %d\n",Socket);
>               return 0;
>       }
> if(bind(Socket,&Destination,sizeof(Destination))==-1)
> {
> perror("bind");
> exit(1)
> }
> ......
> 
> idem for the other program
> 
> what is wrong???

You have to specify either a *local* IP address, or the constant
INADDR_ANY. E.g.

        Local.sin_family = AF_INET;
        Local.sin_addr.s_addr = INADDR_ANY;
        Local.sin_port = htons(TEST_PORT);

        if (bind(Socket, &Local, sizeof(Local)) < 0)
        {
                perror("bind");
                exit(1)
        }

will cause the socket to use TEST_PORT instead of a randomly-allocated
port number.

You can specify a specific *local* IP address for the sin_addr field,
in which case it will only accept packets destined for that specific
address. If you use INADDR_ANY, it will accept packets destined for
any address.

The main reason for using a specific IP address is for virtual-hosted
UDP servers (getsockname() doesn't work for UDP).

-- 
Glynn Clements <[EMAIL PROTECTED]>
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]

Reply via email to