本文简要介绍ruby语言中 Socket.getifaddrs
的用法。
用法
getifaddrs → [ifaddr1, ...]
返回一个接口地址数组。数组的一个元素是 Socket::Ifaddr
的一个实例。
此方法可用于查找multicast-enabled 接口:
pp Socket.getifaddrs.reject {|ifaddr|
!ifaddr.addr.ip? || (ifaddr.flags & Socket::IFF_MULTICAST == 0)
}.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr] }
#=> [["eth0", 2, #<Addrinfo: 221.186.184.67>],
# ["eth0", 2, #<Addrinfo: fe80::216:3eff:fe95:88bb%eth0>]]
GNU/Linux 上的示例结果:
pp Socket.getifaddrs
#=> [#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 PACKET[protocol=0 lo hatype=772 HOST hwaddr=00:00:00:00:00:00]>,
# #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=00:16:3e:95:88:bb] broadcast=PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=ff:ff:ff:ff:ff:ff]>,
# #<Socket::Ifaddr sit0 NOARP PACKET[protocol=0 sit0 hatype=776 HOST hwaddr=00:00:00:00]>,
# #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 127.0.0.1 netmask=255.0.0.0>,
# #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 221.186.184.67 netmask=255.255.255.240 broadcast=221.186.184.79>,
# #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>,
# #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 fe80::216:3eff:fe95:88bb%eth0 netmask=ffff:ffff:ffff:ffff::>]
FreeBSD 上的示例结果:
pp Socket.getifaddrs
#=> [#<Socket::Ifaddr usbus0 UP,0x10000 LINK[usbus0]>,
# #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 LINK[re0 3a:d0:40:9a:fe:e8]>,
# #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 10.250.10.18 netmask=255.255.255.? (7 bytes for 16 bytes sockaddr_in) broadcast=10.250.10.255>,
# #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 fe80:2::38d0:40ff:fe9a:fee8 netmask=ffff:ffff:ffff:ffff::>,
# #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 2001:2e8:408:10::12 netmask=UNSPEC>,
# #<Socket::Ifaddr plip0 POINTOPOINT,MULTICAST,0x800 LINK[plip0]>,
# #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST LINK[lo0]>,
# #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>,
# #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST fe80:4::1 netmask=ffff:ffff:ffff:ffff::>,
# #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST 127.0.0.1 netmask=255.?.?.? (5 bytes for 16 bytes sockaddr_in)>]
相关用法
- Ruby Socket.getservbyport用法及代码示例
- Ruby Socket.gethostname用法及代码示例
- Ruby Socket.getaddrinfo用法及代码示例
- Ruby Socket.getnameinfo用法及代码示例
- Ruby Socket.gethostbyaddr用法及代码示例
- Ruby Socket.getservbyname用法及代码示例
- Ruby Socket.gethostbyname用法及代码示例
- Ruby Socket.pair用法及代码示例
- Ruby Socket.udp_server_sockets用法及代码示例
- Ruby Socket.unpack_sockaddr_in用法及代码示例
- Ruby Socket.udp_server_recv用法及代码示例
- Ruby Socket.new用法及代码示例
- Ruby Socket.tcp用法及代码示例
- Ruby Socket.listen用法及代码示例
- Ruby Socket.unix_server_loop用法及代码示例
- Ruby Socket.sockaddr_un用法及代码示例
- Ruby Socket.accept用法及代码示例
- Ruby Socket.recvfrom_nonblock用法及代码示例
- Ruby Socket.tcp_server_sockets用法及代码示例
- Ruby Socket.bind用法及代码示例
- Ruby Socket.unix_server_socket用法及代码示例
- Ruby Socket.accept_nonblock用法及代码示例
- Ruby Socket.recvfrom用法及代码示例
- Ruby Socket.unix用法及代码示例
- Ruby Socket.ip_address_list用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Socket.getifaddrs。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。