5

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!

skaffman
  • 398,947
  • 96
  • 818
  • 769
DivideByHero
  • 19,715
  • 24
  • 57
  • 64

2 Answers2

11

Downcast the SocketAddress to InetSocketAddress (not InetAddress, which is not a sub-class); this exposes a port accessor.

erickson
  • 265,237
  • 58
  • 395
  • 493
7

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

public int getPort(SocketAddress address) {
    return ((InetSocketAddress) address).getPort();
}
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169