vjame

          優(yōu)化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          NetUtil 網(wǎng)絡服務器端口輔助類


          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

          主站蜘蛛池模板: 望都县| 托克托县| 呈贡县| 明水县| 德惠市| 中方县| 宝兴县| 清涧县| 从化市| 西和县| 株洲县| 集贤县| 德清县| 二手房| 抚顺市| 浑源县| 巧家县| 德兴市| 阿合奇县| 滕州市| 富蕴县| 台前县| 平泉县| 博白县| 哈密市| 墨脱县| 化德县| 南川市| 子洲县| 阿克陶县| 永兴县| 金昌市| 宾川县| 达尔| 教育| 阳城县| 德令哈市| 恭城| 房产| 汉寿县| 镇平县|