// Get socket's local name
memset(&sin, 0, sizeof(sin));
slen = sizeof(sin);
if(getsockname(s, (struct sockaddr *)&sin, &slen) < 0) {
perror("getsockname()");
close(s);
return -1;
}
printf("Local IP is %s\n", inet_ntoa(sin.sin_addr));
// The local port is available in ntohs(sin.sin_port)
close(s);
return 0;
}
Here's a sample run:
insight$ ./1 127.0.0.1 # Use the loopback interface
Local IP is 127.0.0.1
insight$ ./1 216.239.59.99 # Google IP, will use external interface
Local IP is 194.18.12.187
insight$
As you can see, this code will show you the IP of the outgoing
interface, which may or may not be the IP the peer sees due to NAT, as
pointed out in other posts.
getsockname(3) will get you information about the local peer and
getpeername(3) will do the same, but for the remote peer.
-- noah
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden