[英]Apache Mina TCP session tracking from client
I have created a TCP client using Apache Mina. 我已经使用Apache Mina创建了一个TCP客户端。 I have added a while loop to constantly check the liveness of the port.
我添加了一个while循环来不断检查端口的活动性。 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? 另一个问题是,如果服务器关闭,并且我希望服务器一直等待连接直到启动,我该怎么办?
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);
一种修改是代替
Thread.sleep(1000);
we can add future.join()
in version 1.0+ or add a listener for the future in case of 2.0+ 我们可以在1.0+版本中添加
future.join()
,或者在2.0+版本中添加针对未来的监听器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.