如何确定MINA中的服务器端口

发布于 2024-11-24 21:49:52 字数 644 浏览 1 评论 0原文

在我的应用程序中,我有一台监听多个 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)。

提前致谢。

in my application i have a server that listen in more than one udp port. i used MINA in this application.

// code looks like this.
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
...
...
acceptor.bind(new InetSocketAddress(9911));
acceptor.bind(new InetSocketAddress(9912));

now in handler when i receive the data i have following code.

@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);
    }
}

in the code i have no idea how to get the port no ( 9911 or 9912 in this case) in which the data is received.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2024-12-01 21:49:52

参考: 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);
}

Reference: 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文