Example usage for java.net ServerSocket ServerSocket

List of usage examples for java.net ServerSocket ServerSocket

Introduction

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

Prototype

public ServerSocket(int port) throws IOException 

Source Link

Document

Creates a server socket, bound to the specified port.

Usage

From source file:Main.java

public Main() throws IOException {
    serverSocket = new ServerSocket(8008);
    serverSocket.setSoTimeout(10000);/*w ww. j av  a  2  s . co m*/

    ServerSocketChannel channel = serverSocket.getChannel();

}

From source file:org.apache.cloudstack.network.contrail.management.TestDbSetup.java

public static int findFreePort() throws Exception {

    int port;/*from   w  w w . j a  v a 2 s . co  m*/
    ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();
    socket.close();

    return port;
}

From source file:Main.java

public Main(int port) throws IOException {
    serverSocket = new ServerSocket(port);
}

From source file:MainClass.java

public MainClass(int port) throws IOException {
    serverSocket = new ServerSocket(port);
}

From source file:MainClass.java

public MainClass() throws IOException {
    serverSocket = new ServerSocket(2000);
}

From source file:org.apache.axis.transport.http.SimpleAxisServer.java

/**
 * Server process./*from w  w  w  .  j  av a  2 s  .c o m*/
 */
public static void main(String args[]) {

    Options opts = null;
    try {
        opts = new Options(args);
    } catch (MalformedURLException e) {
        log.error(Messages.getMessage("malformedURLException00"), e);
        return;
    }

    String maxPoolSize = opts.isValueSet('t');
    if (maxPoolSize == null)
        maxPoolSize = ThreadPool.DEFAULT_MAX_THREADS + "";

    String maxSessions = opts.isValueSet('m');
    if (maxSessions == null)
        maxSessions = MAX_SESSIONS_DEFAULT + "";

    SimpleAxisServer sas = new SimpleAxisServer(Integer.parseInt(maxPoolSize), Integer.parseInt(maxSessions));

    try {
        doThreads = (opts.isFlagSet('t') > 0);

        int port = opts.getPort();
        ServerSocket ss = null;
        // Try five times
        final int retries = 5;
        for (int i = 0; i < retries; i++) {
            try {
                ss = new ServerSocket(port);
                break;
            } catch (java.net.BindException be) {
                log.debug(Messages.getMessage("exception00"), be);
                if (i < (retries - 1)) {
                    // At 3 second intervals.
                    Thread.sleep(3000);
                } else {
                    throw new Exception(Messages.getMessage("unableToStartServer00", Integer.toString(port)));
                }
            }
        }
        sas.setServerSocket(ss);
        sas.start();
    } catch (Exception e) {
        log.error(Messages.getMessage("exception00"), e);
        return;
    }
}

From source file:DateServer.java

public DateServer(int port) {
    try {/*from  w  w  w . j  av a 2 s.  c  o  m*/
        ss = new ServerSocket(port);
    } catch (IOException ioe) {
        System.err.println("Unable to create server socket: " + ioe);
        System.exit(1);
    }
}

From source file:ExecutorHttpd.java

public void start(int port) throws IOException {
    final ServerSocket ss = new ServerSocket(port);
    while (!executor.isShutdown())
        executor.submit(new TinyHttpdConnection(ss.accept()));
}

From source file:ninja.standalone.StandaloneHelper.java

static public int findAvailablePort(int min, int max) {
    for (int port = min; port < max; port++) {
        try {//  w  w w  .j  av a2 s .  c om
            new ServerSocket(port).close();
            return port;
        } catch (IOException e) {
            // Must already be taken
        }
    }
    throw new IllegalStateException("Could not find available port in range " + min + " to " + max);
}

From source file:com.centeractive.ws.server.SimpleServerTest.java

public static boolean isPortAvailable(int port) {
    ServerSocket ss = null;//  w  w w . ja v  a2 s  .c o  m
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}