scala get ipv4 address

scala 用 isInstanceOf 会报错(instanceof 这个函数就没有),java 下使用  instanceof 来判断是否是 Inet4Address

 

test("get ipv4 address") {

val ipAddr = InetAddress.getLocalHost.getHostAddress

val interfaces: util.Enumeration[NetworkInterface] = NetworkInterface.getNetworkInterfaces

while(interfaces.hasMoreElements) {
val ele = interfaces.nextElement

val addrs: util.Enumeration[InetAddress] = ele.getInetAddresses

while(addrs.hasMoreElements) {
val addr: InetAddress = addrs.nextElement()

addr match {
case some: Inet4Address if !addr.isLoopbackAddress =>
println(some.getHostAddress)
case _ =>
// do nothing
}

}
}

}
posted @ 2015-07-14 08:27  SangS  阅读(600)  评论(0编辑  收藏  举报