我听说我可以通过创建一个UDP socket并将()连接到像谷歌这样的有效目标IP地址来获得自己的IP地址(而不是127.0.0.1)。
但是,我找不到这方面的任何参考或例子。
这个是可能的吗?如果是的话,我怎样才能做到呢?
char* get_my_ip() {
int sockfd;
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;
char remoteIP[INET6_ADDRSTRLEN];
char *ip_addr;
ip_addr = malloc(sizeof(char) * INET6_ADDRSTRLEN);
int rv;
struct addrinfo hints, *ai, *p;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo("8.8.8.8", "http", &hints, &ai)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and make a socket
for(p = ai; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("UDP: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("UDP: connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "UDP: failed to bind socket\n");
exit(2);
}
getsockname(sockfd, (struct sockaddr*)&remoteaddr, &addrlen);
// deal with both IPv4 and IPv6:
if (remoteaddr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&remoteaddr;
inet_ntop(AF_INET, &s->sin_addr, remoteIP, addrlen);
}
else { // AF_INET6
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&remoteaddr;
inet_ntop(AF_INET6, &s->sin6_addr, remoteIP, addrlen);
}
printf("IP_ADDRESS:%s", remoteIP);
freeaddrinfo(ai); // all done with this structure
close(sockfd);
strcpy(ip_addr, remoteIP);
return ip_addr;
}发布于 2014-09-16 22:26:11
调用connect(),然后调用套接字上的getsockname()。它将返回套接字现在绑定到的IP地址,IP路由表已经选择该地址作为到达目标地址的最佳路由。
但是,这并不一定是您要寻找的IP地址,除非您只有一个外部IP地址。
如果您讨论的是调制解调器或路由器的另一端的公共IP地址,则此技术根本不适用。
https://stackoverflow.com/questions/25879280
复制相似问题