[英]Apache Mina TCP session tracking from client
我已經使用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");
}
}
另一個問題是,如果服務器關閉,並且我希望服務器一直等待連接直到啟動,我該怎么辦?
答案是:到目前為止,這是不可能的,因為只有在我們嘗試連接時才知道連接狀態。 一種修改是代替Thread.sleep(1000);
我們可以在1.0+版本中添加future.join()
,或者在2.0+版本中添加針對未來的監聽器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.