libuv-interface

libuv interface example


更新記錄

item note
20160517 第一版

目錄


intreface example

範例說明

範例內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 int main() {
char buf[512];
uv_interface_address_t *info;
int count, i;

uv_interface_addresses(&info, &count);
i = count;

printf("Number of interfaces: %d\n", count);
while (i--) {
uv_interface_address_t interface = info[i];

printf("Name: %s\n", interface.name);
printf("Internal? %s\n", interface.is_internal ? "Yes" : "No");

if (interface.address.address4.sin_family == AF_INET) {
uv_ip4_name(&interface.address.address4, buf, sizeof(buf));
printf("IPv4 address: %s\n", buf);
}
else if (interface.address.address4.sin_family == AF_INET6) {
uv_ip6_name(&interface.address.address6, buf, sizeof(buf));
printf("IPv6 address: %s\n", buf);
}

printf("\n");
}

uv_free_interface_addresses(info, count);
return 0;
}

範例測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[ubuntu](master-48df99a)0h29m root@e8b0cd737680:[t10_interface]$ ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2465 errors:0 dropped:0 overruns:0 frame:0
TX packets:78 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:739856 (739.8 KB) TX bytes:5742 (5.7 KB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:67 errors:0 dropped:0 overruns:0 frame:0
TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4268 (4.2 KB) TX bytes:4268 (4.2 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[ubuntu](master-48df99a)0h26m root@e8b0cd737680:[t10_interface]$ ./t10-main-ubuntu 
Number of interfaces: 4
Name: eth0
Internal? No
IPv6 address: fe80::42:acff:fe11:2

Name: lo
Internal? Yes
IPv6 address: ::1

Name: eth0
Internal? No
IPv4 address: 172.17.0.2

Name: lo
Internal? Yes
IPv4 address: 127.0.0.1

參考來源