KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > server > SocketUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.server;
12
13 import java.io.IOException JavaDoc;
14 import java.net.InetAddress JavaDoc;
15 import java.net.InetSocketAddress JavaDoc;
16 import java.net.ServerSocket JavaDoc;
17 import java.net.SocketAddress JavaDoc;
18 import java.net.UnknownHostException JavaDoc;
19 import java.util.Random JavaDoc;
20
21 /**
22  * Utility class to find a port for local help.
23  */

24 public class SocketUtil {
25
26     private static final Random JavaDoc RANDOM = new Random JavaDoc();
27
28     /**
29      * Returns a free port number, or -1 if none found.
30      */

31     public static int findUnusedLocalPort() {
32         InetAddress JavaDoc address;
33         try {
34             address = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
35         } catch (UnknownHostException JavaDoc uhe) {
36             return -1;
37         }
38
39         int port = findUnusedPort(address, 49152, 65535);
40         if (port == -1) {
41             port = findFreePort();
42         }
43         return port;
44     }
45
46     private static int findUnusedPort(InetAddress JavaDoc address, int from, int to) {
47         for (int i = 0; i < 12; i++) {
48             ServerSocket JavaDoc ss = null;
49             int port = getRandomPort(from, to);
50             try {
51                 ss = new ServerSocket JavaDoc();
52                 SocketAddress JavaDoc sa = new InetSocketAddress JavaDoc(address, port);
53                 ss.bind(sa);
54                 return ss.getLocalPort();
55             } catch (IOException JavaDoc e) {
56             } finally {
57                 if (ss != null) {
58                     try {
59                         ss.close();
60                     } catch (IOException JavaDoc ioe) {
61                     }
62                 }
63             }
64         }
65         return -1;
66     }
67
68     private static int getRandomPort(int low, int high) {
69         return (int)(RANDOM.nextFloat() * (high - low)) + low;
70     }
71
72     private static int findFreePort() {
73         ServerSocket JavaDoc socket = null;
74         try {
75             socket = new ServerSocket JavaDoc(0);
76             return socket.getLocalPort();
77         } catch (IOException JavaDoc e) {
78         } finally {
79             if (socket != null) {
80                 try {
81                     socket.close();
82                 } catch (IOException JavaDoc e) {
83                 }
84             }
85         }
86         return -1;
87     }
88 }
89
Popular Tags