#include <string.h> #include <stdio.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> #include <netdb.h> #include <net/if.h> #include <arpa/inet.h> #include <iostream> using namespace std; #define ERRORIP 0 char *ip_search() { int sfd, intr; struct ifreq buf[16]; struct ifconf ifc; sfd = socket (AF_INET, SOCK_DGRAM, 0); if (sfd < 0) return ERRORIP; ifc.ifc_len = sizeof(buf); ifc.ifc_buf = (caddr_t)buf; if (ioctl(sfd, SIOCGIFCONF, (char *)&ifc)) return ERRORIP; intr = ifc.ifc_len / sizeof(struct ifreq); while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr])); close(sfd); return inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr); } void cc(char*f) { cout<<f<<endl; } int main() { cc(ip_search()); return 0; } /*#if _WinNT_ #include <iostream> using namespace std; #include<windows.h> int main() { cout<<"wzzx test"<<endl; Sleep(10000); return 1; } #endif #if _Linux_ #include <iostream> using namespace std; #include<time.h> int main() { cout<<"wzzx test"<<endl; sleep(10000); return 1; } #endif */
待深入学习