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")
Code by IncludeHelp, on August 03, 2022
Reference: stackoverflow.com
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

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.