Reputation: 63
When I create my UDP client I use this preparation for the client address and port:
struct sockaddr_in caddr;
/ * prepare client address structure */
caddr.sin_family = AF_INET;
caddr.sin_port = htons(0); /* ask bind for an unused port number */
caddr.sin_addr.s_addr = htonl(INADDR_ANY);
/* bind */
printf("Binding to an unused port number\n");
result = bind(s, (struct sockaddr *) &caddr, sizeof(caddr) );
if (result == -1)
err_fatal("bind() failed");
namelen = sizeof(caddr);
getsockname(s, (struct sockaddr *) &caddr, &namelen );
showAddr("done. Bound to addr: ", &caddr);
Like this a get IP address 0.0.0.0 and I always use the local loop interface to contact my server.
Now I want to use another interface and bind to my client another IP address so I try to contact my server from another IP address, not just using local loop.
Can you please show me how to do this?
I tried using something like this:
#define DEF "10.0.0.1"
caddr.sin_addr.s_addr = htonl(DEF);
but I get errors.
Upvotes: 1
Views: 71
Reputation: 3206
If you want another IP address, you'll need to configure it on your system first. If you're using Linux, see the "ip" man page. (e.g. ip addr add 10.0.0.1 ...)
INADDR_ANY allows your endpoint to receive requests that arrive to any of the IP addresses which you have configured on your system. If you want to only receive requests on 10.0.0.1, first configure that IP address on your system, then use it instead of INADDR_ANY, like you tried.
Upvotes: 1