Example usage for java.net ServerSocket close

List of usage examples for java.net ServerSocket close

Introduction

In this page you can find the example usage for java.net ServerSocket close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:org.springframework.xd.gemfire.CacheServer.java

public static boolean portAvailable(int port) {
    InetSocketAddress addr = new InetSocketAddress(port);
    try {/*from  w  w  w .ja va2 s .  c  om*/
        ServerSocket socket = new ServerSocket();
        socket.bind(addr);
        socket.close();
    } catch (IOException e) {
        return false;
    }
    return true;

}

From source file:Main.java

public static final void close(ServerSocket closeable) {
    if (closeable != null) {
        try {/*from   w  ww  .j ava  2  s .  c om*/
            closeable.close();
        } catch (IOException e) {

        }
    }
}

From source file:com.tcp.client.Main.java

public static GenericXmlApplicationContext setupContext() throws InterruptedException, IOException {

    int availableServerSocket = 5682;
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.println("===== Player 2 =====");

    while (true) {
        try {/* w  w w.  j a v  a2  s. co m*/
            ServerSocket s = new ServerSocket(availableServerSocket);
            s.close();
            System.out.println("Player 1 is unavailable.. trying again!!");
            Thread.sleep(2000);

        } catch (IOException e) {
            System.out.println("Found player 1!!");
            break;
        }
    }

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);
    context.load("classpath:META-INF/context.xml");
    context.registerShutdownHook();
    context.refresh();

    return context;
}

From source file:Main.java

/**
 * Simple utility to close {@link ServerSocket} instance.
 * See {@link MozcUtil#close(Closeable,boolean)} for details.
 *//*from  www  .  ja va 2  s .c om*/
public static void close(ServerSocket socket, boolean ignoreIOException) throws IOException {
    try {
        socket.close();
    } catch (IOException e) {
        if (!ignoreIOException) {
            throw e;
        }
    }
}

From source file:Main.java

public static void closeQuietly(ServerSocket s) {
    if (s != null) {
        try {//www .  ja v  a2  s  .  c  om
            s.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

public static void closeQuietly(ServerSocket serverSocket) {
    if (serverSocket != null) {
        try {/*from   ww  w .ja  v  a  2s . c  o  m*/
            serverSocket.close();
        } catch (RuntimeException rethrown) {
            throw rethrown;
        } catch (Exception ignored) {
        }
    }
}

From source file:Main.java

/**
 * Closes {@code serverSocket}, ignoring any checked exceptions. Does nothing if
 * {@code serverSocket} is null.//from   w w  w  .  j  a  va 2 s  .c om
 */
public static void closeQuietly(ServerSocket serverSocket) {
    if (serverSocket != null) {
        try {
            serverSocket.close();
        } catch (RuntimeException rethrown) {
            throw rethrown;
        } catch (Exception ignored) {
        }
    }
}

From source file:Main.java

public static void closeQuietly(ServerSocket sock) {
    if (sock != null) {
        try {//from w w w. j a v  a  2s .c om
            sock.close();
        } catch (IOException var2) {
            ;
        }
    }

}

From source file:Main.java

/**
 * Closes a server socket while suppressing any {@code IOException} that
 * occurs.//from   ww  w .j  ava  2s .co m
 * @param serverSocket the socket to close
 */
public static void closeQuietly(ServerSocket serverSocket) {
    if (serverSocket == null)
        return;
    try {
        serverSocket.close();
    } catch (IOException ex) {
        assert true; // avoid an empty catch
    }
}

From source file:dk.netarkivet.common.utils.SystemUtils.java

/** Check that a given port is not in use.  If this method returns
 * normally, the port is safe to bind.// www .  j  a  v  a 2s. c  om
 *
 * @param port Port to check
 * @throws IOFailure if the port cannot be bound.
 */
public static void checkPortNotUsed(int port) {
    try {
        ServerSocket s = new ServerSocket(port, 1);
        s.close();
    } catch (BindException e) {
        throw new IOFailure("Port " + port + " already in use, or " + "port is out of range", e);
    } catch (IOException e) {
        throw new IOFailure("IO error testing port " + port, e);
    }
}