Home »
Code Examples »
Scala Code Examples
Scala – How to get your computer's IP address? Code Example
The code for How to get your computer's IP address?
import java.net.InetAddress
val ia: InetAddress = InetAddress.getLocalHost
val hostname: String = ia.getHostName
Code by IncludeHelp,
on August 03, 2022
import java.net._
val localhost: InetAddress = InetAddress.getLocalHost
val localIpAddress: String = localhost.getHostAddress
println(s"localIpAddress = $localIpAddress")
import java.net._
val e = NetworkInterface.getNetworkInterfaces
while(e.hasMoreElements)
{
val n = e.nextElement match {
case e: NetworkInterface => e
case _ => ???
}
val ee = n.getInetAddresses
while (ee.hasMoreElements) {
ee.nextElement match {
case e: InetAddress => println(e.getHostAddress)
case _ => ???
}
}
}
Code by IncludeHelp,
on August 03, 2022