Search code examples
javasocketsapache-mina

Apache Mina: Get local port to which remote client connected


If I have a server listening on ports 80 and 8080 as configured below, how would I be able to identify which local port a client connected to when i get a message?

acceptor = new NioSocketAcceptor();
acceptor.setHandler(new MyConnectionHandler());
Set<SocketAddress> addresses = new HashSet<SocketAddress>();
addresses.add(new InetSocketAddress(host, 80));
addresses.add(new InetSocketAddress(host, 8080));
acceptor.bind(addresses);

The handler's messageReceived I want to know which local port the client connected to, not the client's remote port.

@Override
public void messageReceived(IoSession session, Object message) throws Exception {
    log.info("Incomming: {}", session.getRemoteAddress().toString());

Which method should I use to access the port which should be 80 or 8080? The log statement would print something like this:

    Incomming: /219.182.172.12:37921

Solution

  • session.getLocalAddress()