vjame

          優化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數據加載中……

          NetUtil 網絡服務器端口輔助類


          package com.strongit.emp.common.utils;

          import java.io.IOException;
          import java.net.InetAddress;
          import java.net.InetSocketAddress;
          import java.net.Socket;
          import java.net.UnknownHostException;


          //TODO doc
          public class NetUtil {

              
          public static boolean telnetStringPort(String ip, String port, int timeout) {
                  
          if (port == null || !isValidPort(port)) {
                      
          return NetUtil.ping(ip, timeout);
                  }

                  
          return NetUtil.telnet(ip, Integer.valueOf(port.trim()).intValue(),
                          timeout);
              }

              
          public static boolean ping(String ip, int timeout) {
                  AssertUtil.assertNull(
          "IP is null.", ip);
                  
                  
          try {
                      
          return InetAddress.getByName(ip.trim()).isReachable(timeout);
                  } 
          catch (UnknownHostException e) {
                      System.err.println(
          "UnknownHostException:" + e.getMessage());
                      
          return false;
                  } 
          catch (IOException e) {
                      System.err.println(
          "IOException:" + e.getMessage());
                      
          return false;
                  }
              }

              
          public static boolean telnet(String ip, int port, int timeout) {
                  AssertUtil.assertNull(
          "IP is null.", ip);
                  
                  Socket server 
          = null;
                  
          try {
                      server 
          = new Socket();
                      server.connect(
          new InetSocketAddress(ip.trim(), port), timeout);
                      
          return true;
                  } 
          catch (UnknownHostException e) {
                      System.err.println(
          "UnknownHostException:" + e.getMessage());
                      
          return false;
                  } 
          catch (IOException e) {
                      System.err.println(
          "IOException:" + e.getMessage());
                      
          return false;
                  } 
          finally {
                      
          if (server != null)
                          
          try {
                              server.close();
                          } 
          catch (IOException e) {
                              
                          }
                  }
              }

              
          public static boolean isValidPort(String port) {
                  
          if (port != null && port.trim().matches("^[1-9][0-9]{0,3}$|^[1-5][0-9]{0,4}$|^6[0-5]{2}[0-3][0-5]$")) {
                      
          int portInt = Integer.valueOf(port.trim()).intValue();
                      
          if(portInt > 0 && portInt <= 0xFFFFreturn true;
                  }

                  
          return false;
              }
              
              
          public static void main(String arg[]){
                  
          boolean b = telnetStringPort("192.168.50.181","10242",1);
                  
                  System.out.println(b);
              }
          }

          posted on 2009-07-28 17:01 lanjh 閱讀(337) 評論(0)  編輯  收藏 所屬分類: Java App

          主站蜘蛛池模板: 射阳县| 阿克| 太仆寺旗| 蓬莱市| 莫力| 巧家县| 扎赉特旗| 通河县| 阳江市| 三穗县| 阿瓦提县| 上饶县| 合山市| 修武县| 华坪县| 包头市| 阿瓦提县| 乌鲁木齐县| 连云港市| 滨州市| 义乌市| 珲春市| 汤原县| 买车| 项城市| 井陉县| 遵义市| 哈尔滨市| 大邑县| 尚志市| 获嘉县| 蚌埠市| 佛山市| 叶城县| 如皋市| 长丰县| 濮阳县| 吉水县| 安溪县| 潮州市| 禄丰县|