您的位置:首页 > 其它

项目需要的内容   获取ip+ 条件编译

2016-06-19 15:15 232 查看
#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
*/
待深入学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: