Apache Mina: how to get the IP from a connected client
Asked Answered
O

1

8

Can anyone tell me how the get the IP address from a connected client?

So far I've found

session.getRemoteAddress().toString()

and returns something like

/192.168.1.100:49879

is this ok? Can I do something that can return only 192.168.1.100 ?

When I used Sockets I was using something like:

socket.getInetAddress().getHostAddress();

is there something similar using IoSession in apache mina?

Olympus answered 3/8, 2011 at 20:31 Comment(0)
S
18

Downcast the SocketAddress returned by getRemoteAddress() to a InetSocketAddress. You can then call getAddress() which will return an InetAddress object that has the getHostAddress() method you're used to.

e.g.

InetSocketAddress socketAddress = (InetSocketAddress) session.getRemoteAddress();
InetAddress inetAddress = socketAddress.getAddress();

inetAddress.getHostAddress();
Sirreverence answered 3/8, 2011 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.