来自客户端的 Apache Mina TCP 会话跟踪

发布于 2024-10-11 05:04:35 字数 844 浏览 5 评论 0原文

我使用 Apache Mina 创建了一个 TCP 客户端。我添加了一个 while 循环来不断检查端口的活跃度。一旦服务器端建立连接,循环就会被打破并建立连接。我从未来获取会话并用它进行通信。 有没有更好的方法来做到这一点。我可以要求连接等待直到其启动,而不是循环。

 while(true){
   try {
 ConnectFuture future = ioConnector.connect(new InetSocketAddress(Port),
            new TriggerReceiverHandler(), SOCKET_CONFIG);
    System.out.println("Message Receiver started and listening on port "+ Port);
Thread.sleep(1000);
                session = future.getSession();
                if(session != null)
                    break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }catch(Exception ce){
                if(ce.getCause() instanceof ConnectException)
                System.out.println("Retrying connection");
            }
        }

另一个问题是,如果服务器宕机了,我希望服务器一直等待连接直到上线,我该怎么办?

I have created a TCP client using Apache Mina. I have added a while loop to constantly check the liveness of the port. Once the connection is up on the server side, the loop is broken and the connection is made. i get the session from future and use it to communicate.
Is there a better way to do this. instead of loop can i ask the connection to wait till its up.

 while(true){
   try {
 ConnectFuture future = ioConnector.connect(new InetSocketAddress(Port),
            new TriggerReceiverHandler(), SOCKET_CONFIG);
    System.out.println("Message Receiver started and listening on port "+ Port);
Thread.sleep(1000);
                session = future.getSession();
                if(session != null)
                    break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }catch(Exception ce){
                if(ce.getCause() instanceof ConnectException)
                System.out.println("Retrying connection");
            }
        }

Another question is, If the server is down and I want the server to keep waiting for the connection till its up, what should i do?

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

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

发布评论

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

评论(1

瞎闹 2024-10-18 05:04:35

答案是,到目前为止这是不可能的,因为只有当我们尝试连接时才知道连接状态。一项修改是代替 Thread.sleep(1000);,我们可以在 1.0+ 版本中添加 future.join(),或者在 2.0 版本中添加未来的侦听器+

The answer is, As of now its not possible, as the connection state is known only when we attempt to connect. One modification is instead of the Thread.sleep(1000); we can add future.join() in version 1.0+ or add a listener for the future in case of 2.0+

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