1 23 24 28 29 49 50 package com.sun.enterprise.util.net; 51 import java.net.*; 52 import java.util.*; 53 import java.io.*; 54 55 public class NetUtils 56 { 57 private NetUtils() 58 { 59 } 60 61 public static final int MAX_PORT = 65535; 62 63 65 public static String getHostName() 66 { 67 try 68 { 69 return InetAddress.getLocalHost().getHostName(); 70 } 71 catch(Exception e) 72 { 73 return null; 74 } 75 } 76 77 79 80 87 public static String getCanonicalHostName() throws UnknownHostException { 88 String hostname=null; 89 String defaultHostname=InetAddress.getLocalHost().getHostName(); 90 hostname=InetAddress.getLocalHost().getCanonicalHostName(); 92 93 if (hostname.equals(InetAddress.getLocalHost().getHostAddress()) || 97 !hostname.startsWith(defaultHostname)) { 98 hostname=defaultHostname; 101 } 102 103 return hostname; 104 } 105 106 108 public static InetAddress[] getHostAddresses() 109 { 110 try 111 { 112 String hname = getHostName(); 113 114 if(hname == null) 115 return null; 116 117 return InetAddress.getAllByName(hname); 118 } 119 catch(Exception e) 120 { 121 return null; 122 } 123 } 124 125 127 public static String [] getHostIPs() 128 { 129 try 130 { 131 InetAddress[] adds = getHostAddresses(); 132 133 if(adds == null) 134 return null; 135 136 String [] ips = new String [adds.length]; 137 138 for(int i = 0; i < adds.length; i++) 139 { 140 String ip = trimIP(adds[i].toString()); 141 ips[i] = ip; 142 } 143 144 return ips; 145 } 146 catch(Exception e) 147 { 148 return null; 149 } 150 } 151 152 154 public static String trimIP(String ip) 155 { 156 if(ip == null || ip.length() <= 0) 157 return ip; 158 159 int index = ip.lastIndexOf('/'); 160 161 if(index >= 0) 162 return ip.substring(++index); 163 164 return ip; 165 } 166 167 169 public static byte[] ip2bytes(String ip) 170 { 171 try 172 { 173 175 ip = trimIP(ip); 176 StringTokenizer stk = new StringTokenizer(ip, "."); 177 178 byte[] bytes = new byte[stk.countTokens()]; 179 180 for(int i = 0; stk.hasMoreTokens(); i++) 181 { 182 String num = stk.nextToken(); 183 int inum = Integer.parseInt(num); 184 bytes[i] = (byte)inum; 185 } 187 return bytes; 188 } 189 catch(NumberFormatException nfe) 190 { 191 return null; 192 } 193 } 194 195 197 public static boolean isLocalHost(String ip) 198 { 199 if(ip == null) 200 return false; 201 202 ip = trimIP(ip); 203 204 return ip.equals(LOCALHOST_IP); 205 } 206 207 209 public static boolean isLocal(String ip) 210 { 211 if(ip == null) 212 return false; 213 214 ip = trimIP(ip); 215 216 if(isLocalHost(ip)) 217 return true; 218 219 String [] myIPs = getHostIPs(); 220 221 if(myIPs == null) 222 return false; 223 224 for(int i = 0; i < myIPs.length; i++) 225 { 226 if(ip.equals(myIPs[i])) 227 return true; 228 } 229 230 return false; 231 } 232 233 235 public static boolean isRemote(String ip) 236 { 237 return !isLocal(ip); 238 } 239 240 241 247 public static int getNextFreePort(String hostName, int port) 248 { 249 while (port++ < MAX_PORT) { 250 if (isPortFree(hostName, port)) { 251 return port; 252 } 253 } 254 return 0; 255 } 256 257 264 public static int getFreePort(String hostName, int startingPort, int endingPort) 265 { 266 int range = endingPort - startingPort; 267 int port = 0; 268 if (range > 0) { 269 Random r = new Random(); 270 while (true) { 271 port = r.nextInt(range + 1) + startingPort; 272 if (isPortFree(hostName, port)) { 273 break; 274 } 275 } 276 } 277 return port; 278 } 279 280 public static boolean isPortValid(int portNumber) 281 { 282 if (portNumber >=0 && portNumber <= MAX_PORT) { 283 return true; 284 } else { 285 return false; 286 } 287 } 288 289 public static boolean isPortStringValid(String portNumber) 290 { 291 try { 292 return isPortValid(Integer.parseInt(portNumber)); 293 } catch (NumberFormatException ex) { 294 return false; 295 } 296 } 297 298 300 public static boolean isPortFree(String hostName, int portNumber) 301 { 302 if(portNumber <= 0 || portNumber > MAX_PORT) 303 return false; 304 305 if(hostName == null || isThisMe(hostName)) 306 return isPortFreeServer(portNumber); 307 else 308 return isPortFreeClient(hostName, portNumber); 309 } 310 311 public static boolean isPortFree(int portNumber) 312 { 313 return isPortFree(null, portNumber); 314 } 315 316 private static boolean isPortFreeClient(String hostName, int portNumber) 317 { 318 try 319 { 320 326 if (hostName == null) { 328 hostName = getHostName(); 329 } 330 Socket socket = new Socket(hostName, portNumber); 331 OutputStream os = socket.getOutputStream(); 332 InputStream is = socket.getInputStream(); 333 os.close(); 334 os = null; 335 is.close(); 336 is = null; 337 socket.close(); 338 socket = null; 339 } 340 catch (Exception e) 341 { 342 return true; 344 } 345 346 return false; 347 } 348 349 private static boolean isPortFreeServer(int port) 350 { 351 try 352 { 353 ServerSocket ss = new ServerSocket(port); 354 ss.close(); 355 return true; 356 } 357 catch (Exception e) 358 { 359 return false; 360 } 361 } 362 363 364 384 public static int getFreePort() 385 { 386 int freePort = 0; 387 boolean portFound = false; 388 ServerSocket serverSocket = null; 389 390 synchronized (NetUtils.class) 391 { 392 try 393 { 394 396 serverSocket = new ServerSocket(0); 397 freePort = serverSocket.getLocalPort(); 398 portFound = true; 399 } 400 catch(Exception e) 401 { 402 } 404 finally 405 { 406 if (!portFound) 407 { 408 freePort = 0; 409 } 410 try 411 { 412 if (serverSocket != null) 413 { 414 serverSocket.close(); 415 if (! serverSocket.isClosed()) 416 { 417 throw new Exception ("local exception ..."); 418 } 419 } 420 } 421 catch(Exception e) 422 { 423 freePort = 0; 425 } 426 } 427 return freePort; 428 } 429 } 430 431 433 private static final String LOCALHOST_IP = "127.0.0.1"; 434 435 437 private static boolean isThisMe(String hostname) 438 { 439 try 440 { 441 InetAddress[] myadds = getHostAddresses(); 442 InetAddress[] theiradds = InetAddress.getAllByName(hostname); 443 444 for(int i = 0; i < theiradds.length; i++) 445 { 446 if(theiradds[i].isLoopbackAddress()) 447 return true; 448 449 for(int j = 0; j < myadds.length; j++) 450 { 451 if(myadds[j].equals(theiradds[i])) 452 return true; 453 } 454 } 455 } 456 catch(Exception e) 457 { 458 } 459 460 return false; 461 } 462 463 465 public static void main(String [] args) 466 { 467 System.out.println("80: " + isPortFree(80)); 468 System.out.println("777: " + isPortFree(777)); 469 System.out.println("8000: " + isPortFree(8000)); 470 } 471 } 472 473 474 475 | Popular Tags |