/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.rubypeople.rdt.core;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
/**
* Utility class to find a port to debug on.
*/
public class SocketUtil
{
/**
* Returns a free port number on localhost, or -1 if unable to find a free port.
*
* @return a free port number on localhost, or -1 if unable to find a free port
* @since 0.7
*/
public static int findFreePort()
{
ServerSocket socket = null;
try
{
socket = new ServerSocket(0);
return socket.getLocalPort();
}
catch (IOException e)
{
}
finally
{
if (socket != null)
{
try
{
socket.close();
}
catch (IOException e)
{
}
}
}
return -1;
}
public static boolean portFree(int port)
{
return portFree("127.0.0.1", port);
}
public static boolean portFree(String host, int port)
{
ServerSocket socket = null;
try
{
socket = new ServerSocket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(host, port));
}
catch (Exception e)
{
// ignore
return false;
}
finally
{
try
{
if (socket != null)
socket.close();
}
catch (IOException e)
{
// ignore
}
}
return true;
}
}