使用 Apache Mina 通过 UDP 将数据发送回客户端

发布于 2024-11-03 18:40:35 字数 346 浏览 0 评论 0原文

我正在使用 Apache Mina 创建一个服务器来接受 UDP 客户端请求。我已阅读 Apache Mina 提供的有关 UDP Server & 的官方文档。 UDP 客户端。但是,我想知道当服务器收到消息时,我可以使用相同的会话写回UDP客户端吗(我知道UDP在网络层是无连接的,但是我可以在应用层获取远程主机的IP和端口) ),这样 UDP 客户端就会收到消息。我知道 TCP 是可能的,但我对 UDP 有点困惑。我知道这可能不完全是基于 Java 的,而是更多基于网络层的问题。如果有人能为我解决这个问题,我将不胜感激。

I am using Apache Mina to create a server to accept UDP Client requests. I have read the Official documentation provided by Apache Mina regarding UDP Server & UDP Client. However, I wished to know when the server receives a message, can I write back to the UDP Client using the same session(I know UDP is connectionless at Network Layer, however I can get the IP and PORT of the remote host at Application Layer) such that UDP Client receives a message. I know this is possible is TCP but am a little confused about UDP. I know this may not exactly be Java based but more Network Layer based question. Would appreciate if somebody could clear this for me.

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

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

发布评论

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

评论(3

新人笑 2024-11-10 18:40:35

我得到了同样的答案并认为我会分享。

UDP 是无连接的,但是我可以使用 Apache Mina 中的相同会话来写入会话。我也尝试过将其作为示例,并且有效。

I got the answer to the same and thought i would share.

UDP is connectionless however I can use the same session which I have in Apache Mina to write to the session. I tried it as a sample also and it worked.

风筝有风,海豚有海 2024-11-10 18:40:35
@Override
    public void messageReceived(IoSession session, Object message) throws Exception {

            for (int i = 0; i < session.getService().getManagedSessions().values().toArray().length; i++) {

                IoSession aSession=(IoSession) session.getService().getManagedSessions().values().toArray()[i];
                aSession.write("Any Message");
            }



    }
@Override
    public void messageReceived(IoSession session, Object message) throws Exception {

            for (int i = 0; i < session.getService().getManagedSessions().values().toArray().length; i++) {

                IoSession aSession=(IoSession) session.getService().getManagedSessions().values().toArray()[i];
                aSession.write("Any Message");
            }



    }
逆流 2024-11-10 18:40:35

试试这个你的处理程序类

@Override
public void messageReceived(IoSession session, Object message) throws Exception {
        // response every time get data 
        byte[] b = "Received".getBytes();
        final IoBuffer responsebuffer = IoBuffer.allocate(b.length);
        responsebuffer.put(b);
        responsebuffer.flip();
        session.write(responsebuffer);
        SocketAddress remoteAddress = session.getRemoteAddress();

        if (message instanceof IoBuffer) {
            IoBuffer buffer = (IoBuffer) message;
            final CharsetDecoder decoder = getCharsetDecoder(session);
            String receivedMsg = buffer.getString(decoder);
            String data = remoteAddress + " Received: " + receivedMsg;
            server.append(data);
        }

    }

Try this your handler class

@Override
public void messageReceived(IoSession session, Object message) throws Exception {
        // response every time get data 
        byte[] b = "Received".getBytes();
        final IoBuffer responsebuffer = IoBuffer.allocate(b.length);
        responsebuffer.put(b);
        responsebuffer.flip();
        session.write(responsebuffer);
        SocketAddress remoteAddress = session.getRemoteAddress();

        if (message instanceof IoBuffer) {
            IoBuffer buffer = (IoBuffer) message;
            final CharsetDecoder decoder = getCharsetDecoder(session);
            String receivedMsg = buffer.getString(decoder);
            String data = remoteAddress + " Received: " + receivedMsg;
            server.append(data);
        }

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