在我的应用程序中,我有一台监听多个udp端口的服务器。我在此应用程序中使用了MINA。
// code looks like this.
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
...
...
acceptor.bind(new InetSocketAddress(9911));
acceptor.bind(new InetSocketAddress(9912));
现在在处理程序中,当我收到数据时,我有以下代码。
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
if (message instanceof IoBuffer) {
String msg = "data received in port "+ <NEED TO GET THE PORT NO>;
System.out.println(msg);
}
}
在代码中,我不知道如何获取接收数据的端口号(在这种情况下为9911或9912)。
提前致谢。
最佳答案
参考:http://mina.apache.org/report/trunk/apidocs/org/apache/mina/core/session/IoSession.html
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
if (message instanceof IoBuffer) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) session.getServiceAddress();
int portNumber = inetSocketAddress.getPortNumber();
String msg = "data received in port "+ portNumber;
System.out.println(msg);
}