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 閱讀(331) 評論(0)  編輯  收藏 所屬分類: Java App

          主站蜘蛛池模板: 车险| 连城县| 台东市| 浮山县| 西盟| 潞西市| 米脂县| 富源县| 郑州市| 永泰县| 林口县| 轮台县| 南城县| 贵德县| 漯河市| 湾仔区| 龙南县| 鲁甸县| 衡南县| 彭阳县| 上饶市| 光山县| 徐闻县| 三穗县| 花莲市| 额济纳旗| 弥渡县| 论坛| 嘉义市| 综艺| 泊头市| 嘉义县| 南投县| 渭源县| 安顺市| 济源市| 乐亭县| 呼图壁县| 乌鲁木齐市| 陕西省| 秭归县|