int get_ip(char *ip,)
{
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("create socket fail\n");
}
return(-1);
}
struct ifreq buf[16];
struct ifconf ifc;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
ioctl(sockfd, SIOCGIFCONF, (char *)&ifc);
int intr = ifc.ifc_len / sizeof(struct ifreq);
while (intr-- > 0 && ioctl(sockfd, SIOCGIFADDR, (char *)&buf[intr])) ;
close(sockfd);
strcpy(ip, inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr));
return(0);
}
{
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("create socket fail\n");
}
return(-1);
}
struct ifreq buf[16];
struct ifconf ifc;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
ioctl(sockfd, SIOCGIFCONF, (char *)&ifc);
int intr = ifc.ifc_len / sizeof(struct ifreq);
while (intr-- > 0 && ioctl(sockfd, SIOCGIFADDR, (char *)&buf[intr])) ;
close(sockfd);
strcpy(ip, inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr));
//回环地址不用
if (strcmp(ip, "127.0.0.1") == 0){
return(-1);
}return(0);
}