获取本地IP
来源:互联网 发布:java 解析json 编辑:程序博客网 时间:2024/04/27 22:04
本机可能会有多个IP,下面这段程序可以获得本机IP:
struct hostent *h;
char host[100];
int i;
gethostname(host, 100);
if ((h = gethostbyname(host)) == NULL) {
printf("Error : %s!/n", hstrerror(h_errno));
return;
};
printf("Default IP: %s/n", inet_ntoa (*((struct in_addr *)h->h_addr)));
for (i = 0; i< h->h_length / sizeof(int); i++) {
printf("IP %d : %s/n", i+1, inet_ntoa (*((struct in_addr *)h->h_addr_list[i])));
};
需要注意的是,h->h_addr实际上就是h->h_addr_list[0],是为了向前兼容而保留的。
DNS操作时的错误处理与普通程序不同,gethostbyname通过设置h_errno代表出错号,对应的错误函数有hstrerror()和herror(),分别对应于strerror()和perror()这两个普通的错误函数。
- sqlplus获取本地IP
- 获取本地ip
- DEPHI 获取本地IP
- 获取本地IP
- QT获取本地ip
- 获取本地IP地址
- 获取本地IP
- 获取本地IP
- php 获取本地IP
- 获取本地IP方法
- 获取本地IP地址
- 获取本地IP
- qt获取本地ip
- ioctl获取本地IP
- java获取本地ip
- 获取本地ip方法
- 获取本地ip
- vc获取本地IP
- VS2008 错误提示框不见了?
- 挺腾讯反360的基本都是托
- 每天读一遍,不久你就会变
- 获取本地IP
- register component and get object --- illustrated by MSSG
- 获取本地IP
- 003_修改右键单击鼠标时显示的内容
- arm9板子上写了一个小程序:手写数字及英文大小写字符的识别
- A,B,C类地址
- request.getRequestURI()
- linq基础概念
- 神话新手卡ny0002
- 游戏服务器的架构设计
- android layout_weight 属性