http://blog.csdn.net/favormm/article/details/6858330
用到了ASIHTTPRequest这个网络开源库。
http://automation.whatismyip.com/n09230945.asp这个http url就可以获取你当前的ip地址。
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <netdb.h>
10 #include <arpa/inet.h>
11 #include <sys/sockio.h>
12 #include <net/if.h>
13 #include <errno.h>
14 #include <net/if_dl.h>
15
16 //#include "GetAddresses.h"
17
18 #define min(a,b) ((a) < (b) ? (a) : (b))
19 #define max(a,b) ((a) > (b) ? (a) : (b))
20
21 #define BUFFERSIZE 4000
22
23 char *if_names[MAXADDRS];
24 char *ip_names[MAXADDRS];
25 char *hw_addrs[MAXADDRS];
26 unsigned long ip_addrs[MAXADDRS];
27
28 static int nextAddr = 0;
29
30 void InitAddresses()
31 {
32 int i;
33 for (i=0; i<MAXADDRS; ++i)
34 {
35 if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
36 ip_addrs[i] = 0;
37 }
38 }
39
40 void FreeAddresses()
41 {
42 int i;
43 for (i=0; i<MAXADDRS; ++i)
44 {
45 if (if_names[i] != 0) free(if_names[i]);
46 if (ip_names[i] != 0) free(ip_names[i]);
47 if (hw_addrs[i] != 0) free(hw_addrs[i]);
48 ip_addrs[i] = 0;
49 }
50 InitAddresses();
51 }
52
53 void GetIPAddresses()
54 {
55 int i, len, flags;
56 char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
57 struct ifconf ifc;
58 struct ifreq *ifr, ifrcopy;
59 struct sockaddr_in *sin;
60
61 char temp[80];
62
63 int sockfd;
64
65 for (i=0; i<MAXADDRS; ++i)
66 {
67 if_names[i] = ip_names[i] = NULL;
68 ip_addrs[i] = 0;
69 }
70
71 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
72 if (sockfd < 0)
73 {
74 perror("socket failed");
75 return;
76 }
77
78 ifc.ifc_len = BUFFERSIZE;
79 ifc.ifc_buf = buffer;
80
81 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
82 {
83 perror("ioctl error");
84 return;
85 }
86
87 lastname[0] = 0;
88
89 for (ptr = buffer; ptr < buffer + ifc.ifc_len; )
90 {
91 ifr = (struct ifreq *)ptr;
92 len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
93 ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer
94
95 if (ifr->ifr_addr.sa_family != AF_INET)
96 {
97 continue; // ignore if not desired address family
98 }
99
100 if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)
101 {
102 *cptr = 0; // replace colon will null
103 }
104
105 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)
106 {
107 continue; /* already processed this interface */
108 }
109
110 memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
111
112 ifrcopy = *ifr;
113 ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
114 flags = ifrcopy.ifr_flags;
115 if ((flags & IFF_UP) == 0)
116 {
117 continue; // ignore if interface not up
118 }
119
120 if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);
121 if (if_names[nextAddr] == NULL)
122 {
123 return;
124 }
125 strcpy(if_names[nextAddr], ifr->ifr_name);
126
127 sin = (struct sockaddr_in *)&ifr->ifr_addr;
128 strcpy(temp, inet_ntoa(sin->sin_addr));
129
130 ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);
131 if (ip_names[nextAddr] == NULL)
132 {
133 return;
134 }
135 strcpy(ip_names[nextAddr], temp);
136
137 ip_addrs[nextAddr] = sin->sin_addr.s_addr;
138
139 ++nextAddr;
140 }
141
142 close(sockfd);
143 }
144
145 void GetHWAddresses()
146 {
147 struct ifconf ifc;
148 struct ifreq *ifr;
149 int i, sockfd;
150 char buffer[BUFFERSIZE], *cp, *cplim;
151 char temp[80];
152
153 for (i=0; i<MAXADDRS; ++i)
154 {
155 hw_addrs[i] = NULL;
156 }
157
158 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
159 if (sockfd < 0)
160 {
161 perror("socket failed");
162 return;
163 }
164
165 ifc.ifc_len = BUFFERSIZE;
166 ifc.ifc_buf = buffer;
167
168 if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
169 {
170 perror("ioctl error");
171 close(sockfd);
172 return;
173 }
174
175 ifr = ifc.ifc_req;
176
177 cplim = buffer + ifc.ifc_len;
178
179 for (cp=buffer; cp < cplim; )
180 {
181 ifr = (struct ifreq *)cp;
182 if (ifr->ifr_addr.sa_family == AF_LINK)
183 {
184 struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
185 int a,b,c,d,e,f;
186 int i;
187
188 strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));
189 sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
190 sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
191
192 for (i=0; i<MAXADDRS; ++i)
193 {
194 if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
195 {
196 if (hw_addrs[i] == NULL)
197 {
198 hw_addrs[i] = (char *)malloc(strlen(temp)+1);
199 strcpy(hw_addrs[i], temp);
200 break;
201 }
202 }
203 }
204 }
205 cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
206 }
207
208 close(sockfd);
209 }
1 - (NSString *)deviceIPAdress {
2 InitAddresses();
3 GetIPAddresses();
4 GetHWAddresses();
5
6 return [NSString stringWithFormat:@"%s", ip_names[1]];
7 }
1 - (void)getCurrentIP
2 {
3 NSURL *url = [NSURL URLWithString:@"http://automation.whatismyip.com/n09230945.asp"];
4 __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
5 [request setCompletionBlock:^{
6 NSString *responseString = [request responseString];
7 if (responseString) {
8
9 NSString *ip = [NSString stringWithFormat:@"%@", responseString];
10
11 NSLog(@"responseString = %@", ip);
12 };
13
14 }];
15
16 [request setFailedBlock:^{
17 }];
18 }