Java: How to get a connected port from InetAddress?

I am trying to create 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 provides only the SocketAddress object (which can be disabled for InetAddress). I can get the IP address from InetAddress, but I usually use Socket.getPort () to get the port number, but Mina seems to be hiding these low level objects. Is there another way? Thanks!

+3
source share
2 answers

Drag SocketAddressto InetSocketAddress(not InetAddresswhich is not a subclass); it provides portaccessor .

+8
source

I have a real old version, but it worked for me,

public int getPort(SocketAddress address) {
    return ((InetSocketAddress) address).getPort();
}
+6
source

Source: https://habr.com/ru/post/1714339/


All Articles