简体   繁体   中英

How do I obtain the client's IPv6 address in the Akka/Spray framework?

I am currently using the below code to get the IPv4 address:

import spray.http.RemoteAddress
...
val ipV4: String = remoteAddress.toOption.map(_.getHostAddress).getOrElse("<unknown>")

However, I cannot seem to find any information on doing the same thing for IPv6. Any assistance on this would be greatly appreciated.

It is not depend from your method - it depends from the configuration of your network.

Look at the source code of spray.http.RemoteAddress :

def apply(bytes: Array[Byte]): RemoteAddress = {
    require(bytes.length == 4 || bytes.length == 16)
    try IP(InetAddress.getByAddress(bytes)) catch { case _: UnknownHostException ⇒ Unknown }
}

This code wraps both IPv4 (4 byte) and IPv6 (16 byte). As a result you will receive IPv4 address, IPv6 address or special Pv4-mapped-to-IPv6 address depends from your network settings.

Try to run your code on the local pc where network interface configured to use IPv6 and you will see.

This is the object that you actually receive from Java: http://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM