I'm trying to build a Java NIO-based socket server using Apache Mina. I really need to know the port of the remote host, not just the IP address, and it seems that Mina only exposes a SocketAddress (which can be downcast to InetAddress) object. I can get the IP address from InetAddress, but I normally use Socket.getPort() to get the port number, but Mina appears to obscure these low-level objects. Is there another way? Thanks!
public InetSocketAddress(int port) Creates a socket address where the IP address is the wildcard address and the port number a specified value. A valid port value is between 0 and 65535. A port number of zero will let the system pick up an ephemeral port in a bind operation.
InetAddress class provides methods to get the IP address of any hostname. An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses. There are 2 types of addresses : Unicast — An identifier for a single interface.
Downcast the SocketAddress
to InetSocketAddress
(not InetAddress
, which is not a sub-class); this exposes a port
accessor.
I have a real old version but this worked for me,
public int getPort(SocketAddress address) {
return ((InetSocketAddress) address).getPort();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With