KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > SocketUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.launching;
12
13
14 import java.io.IOException JavaDoc;
15 import java.net.ConnectException JavaDoc;
16 import java.net.ServerSocket JavaDoc;
17 import java.net.Socket JavaDoc;
18 import java.util.Random JavaDoc;
19
20 /**
21  * Utility class to find a port to debug on.
22  * <p>
23  * Clients are not intended to subclass or instantiate this class.
24  * </p>
25  */

26 public class SocketUtil {
27     private static final Random JavaDoc fgRandom= new Random JavaDoc(System.currentTimeMillis());
28     
29     /**
30      * Returns a free port number on the specified host within the given range,
31      * or -1 if none found.
32      *
33      * @param host name or IP address of host on which to find a free port
34      * @param searchFrom the port number from which to start searching
35      * @param searchTo the port number at which to stop searching
36      * @return a free port in the specified range, or -1 of none found
37      * @deprecated Use <code>findFreePort()</code> instead. It is possible that this
38      * method can return a port that is already in use since the implementation does
39      * not bind to the given port to ensure that it is free.
40      */

41     public static int findUnusedLocalPort(String JavaDoc host, int searchFrom, int searchTo) {
42
43         for (int i= 0; i < 10; i++) {
44             Socket JavaDoc s= null;
45             int port= getRandomPort(searchFrom, searchTo);
46             try {
47                 s= new Socket JavaDoc(host, port);
48             } catch (ConnectException JavaDoc e) {
49                 return port;
50             } catch (IOException JavaDoc e) {
51             } finally {
52                 if (s != null) {
53                     try {
54                         s.close();
55                     } catch (IOException JavaDoc ioe) {
56                     }
57                 }
58             }
59         }
60         return -1;
61     }
62     
63     private static int getRandomPort(int low, int high) {
64         return (int)(fgRandom.nextFloat() * (high-low)) + low;
65     }
66     
67     /**
68      * Returns a free port number on localhost, or -1 if unable to find a free port.
69      *
70      * @return a free port number on localhost, or -1 if unable to find a free port
71      * @since 3.0
72      */

73     public static int findFreePort() {
74         ServerSocket JavaDoc socket= null;
75         try {
76             socket= new ServerSocket JavaDoc(0);
77             return socket.getLocalPort();
78         } catch (IOException JavaDoc e) {
79         } finally {
80             if (socket != null) {
81                 try {
82                     socket.close();
83                 } catch (IOException JavaDoc e) {
84                 }
85             }
86         }
87         return -1;
88     }
89 }
90
Popular Tags