- 论坛徽章:
- 0
|
原帖由 wuzhao 于 2007-11-5 17:54 发表 ![]()
我也遇到这样的问题,不知LZ怎么解决的?
以前写过一个获取本机IP的代码. 你看看
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<fcntl.h>
- #include<string.h>
- #include<net/if.h>
- #include<net/if_arp.h>
- #include<sys/ioctl.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<netdb.h>
- int main()
- {
- int socktemp;
- struct ifreq ifr;
- struct sockaddr_in clientadd;
- char myip[20] = {0};
- socktemp = socket(AF_INET, SOCK_DGRAM, 0);
- if (socktemp == -1)
- {
- printf("socket dgram erron \n");
- return -1;
- }
- strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
- ifr.ifr_name[IFNAMSIZ-1] = 0;
- if (ioctl(socktemp, SIOCGIFADDR, &ifr) < 0)
- {
- printf("ioctl error\n");
- return -1;
- }
- memcpy(&clientadd, &ifr.ifr_addr, sizeof(clientadd));
- sprintf(myip, "%s", inet_ntoa(clientadd.sin_addr));
- printf("ip is :%s\n", myip);
- return 0;
- }
复制代码 |
|