JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Tools  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
己加入精華區
by koji at 2008-06-09 13:34
本主題所含的標籤
無標籤
作者 Telnet Client connect timeout設定問題 [精華]
RR

~Nintendo64~



發文: 686
積分: 3
於 2008-06-05 14:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟寫了個telnet連線測試的小程式,部分如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  private final int TELNET__DEFAULT_PORT=23;
  private final int CONNECT_TIMEOUT=3000; //ms
 
...
...
...
 
    boolean isOK=false;
    TelnetClient tc=new TelnetClient();
 
    try
    {
      tc=new TelnetClient();
      tc.setDefaultTimeout(this.CONNECT_TIMEOUT);
      tc.connect(this.IP, this.port);
      isOK=true;
    }
    catch(Exception e)
    {
      logger.error("Error occurred when telnet to ["+this.IP+":"+this.port+"], -> "+e.toString());
      isOK=false;
    }

紅字的部分,好像不管怎麼設定都沒用。
如果連到一個有開telnet service的端點,假設192.168.1.23
則在第15行會很快就連上,1秒以內就ok。

如果是一個沒有telnet服務,假設是192.168.1.254
則會在約2秒以內顯示無法連線
java.net.ConnectException: Connection refused: connect

如果是指定一個個不存在的IP,假設是192.168.1.2 (網路上的設備無人用此IP)
則會到20秒左右後才顯示無法連線
java.net.ConnectException: Connection refused: connect

由於網路環境特殊,很可能會阻擋ICMP封包,我無法用ping command來確定IP是否存在
請問大大們有沒有解決的辦法,可以讓連到不存在IP(或是機器死掉)在3秒內可以timeout,讓下面的程式繼續跑?


reply to postreply to post

Focus TDCi Powershift,純正德國血統
作者 Re:Telnet Client connect timeout設定問題 [Re:RR]
RR

~Nintendo64~



發文: 686
積分: 3
於 2008-06-09 11:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
嗯....有大大們用過Common net telnet這個套件嗎?

reply to postreply to post

Focus TDCi Powershift,純正德國血統
作者 Re:Telnet Client connect timeout設定問題 [Re:RR]
RR

~Nintendo64~



發文: 686
積分: 3
於 2008-06-09 12:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
和上次一樣,再度發問後10分鐘內就找到solution:

首先加個實作SocketFactory的private class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  /**
   * 改寫SocketFactory給TelnetClient用
   * @author http://www.ioncannon.net/java/132/connection-timeouts-with-the-apache-commons-telnetclient/
   * @author RoseRoo (modify)
   */
  private static class TimeoutSockectFactory implements SocketFactory
  {
    private int CONNECT_TIMEOUT=0;
 
    /**
     * @deprecated
     */
    public TimeoutSockectFactory()
    {}
 
    /**
     * Init. a new socket factory
     * @param int - time out in millisecond
     */
    public TimeoutSockectFactory(int timeOut)
    {this.CONNECT_TIMEOUT=timeOut;}
 
    public Socket createSocket(String hostname, int port) throws IOException
    {
      Socket socket=new Socket();
      socket.connect(new InetSocketAddress(hostname, port), this.CONNECT_TIMEOUT);
      return socket;
    }
 
    public Socket createSocket(InetAddress hostAddress, int port) throws IOException
    {
      Socket socket=new Socket();
      socket.connect(new InetSocketAddress(hostAddress, port), this.CONNECT_TIMEOUT);
      return socket;
    }
 
    public Socket createSocket(String remoteHost, int remotePort, InetAddress localAddress, int localPort) throws IOException
    {return new Socket();}
 
    public Socket createSocket(InetAddress remoteAddress, int remotePort, InetAddress localAddress, int localPort) throws IOException
    {return new Socket();}
 
    public ServerSocket createServerSocket(int port) throws IOException
    {return new ServerSocket();}
 
    public ServerSocket createServerSocket(int port, int backlog) throws IOException
    {return new ServerSocket();}
 
    public ServerSocket createServerSocket(int port, int backlog, InetAddress bindAddress) throws IOException
    {return new ServerSocket();}
  }


然後替TelnetClient指定一個SocketFactory
1
TelnetClient.setSocketFactory(new TimeoutSockectFactory(毫秒逾時設定));


以上~


reply to postreply to post

Focus TDCi Powershift,純正德國血統
» JWorld@TW »  Java Tools

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8