[英]Apache Mina: how to get the IP from a connected client
Can anyone tell me how the get the IP address from a connected client? 谁能告诉我如何从连接的客户端获取IP地址?
So far I've found 到目前为止我发现了
session.getRemoteAddress().toString()
and returns something like 并返回类似的东西
/192.168.1.100:49879
/192.168.1.100:49879
is this ok? 这个可以吗? Can I do something that can return only 192.168.1.100 ?
我可以做一些只能返回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? 在apache mina中使用IoSession有类似的东西吗?
Downcast the SocketAddress
returned by getRemoteAddress()
to a InetSocketAddress
. 将
getRemoteAddress()
返回的SocketAddress
InetSocketAddress
。 You can then call getAddress()
which will return an InetAddress
object that has the getHostAddress()
method you're used to. 然后,您可以调用
getAddress()
,它将返回一个InetAddress
对象,该对象具有您习惯使用的getHostAddress()
方法。
eg 例如
InetSocketAddress socketAddress = (InetSocketAddress) session.getRemoteAddress();
InetAddress inetAddress = socketAddress.getAddress();
inetAddress.getHostAddress();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.