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() throws IOException 

Source Link

Document

Creates an unbound server socket.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create an unbound server socket
    ServerSocket serverSocket = new ServerSocket();

    // Create a socket address object
    InetSocketAddress endPoint = new InetSocketAddress("localhost", 12900);

    // Set the wait queue size to 100
    int waitQueueSize = 100;

    // Bind the server socket to localhost and at port 12900 with
    // a wait queue size of 100
    serverSocket.bind(endPoint, waitQueueSize);

}

From source file:Main.java

public Main() throws IOException {
    serverSocket = new ServerSocket();
    serverSocket.setSoTimeout(10000);//from   w w  w  .  ja v  a 2 s.  co m

    serverSocket.bind(InetSocketAddress.createUnresolved("java2s.com", 8080), 1000);
}

From source file:Main.java

public Main() throws IOException {
    serverSocket = new ServerSocket();
    serverSocket.setSoTimeout(10000);

    serverSocket.setPerformancePreferences(1000, 2000, 2000);
}

From source file:com.nesscomputing.service.discovery.client.TestServiceDiscovery.java

private static final int findUnusedPort() {
    int port;/*w  w  w. ja v a2s . c o  m*/

    ServerSocket socket = null;
    try {
        socket = new ServerSocket();
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
            // GNDN
        }
    }

    return port;
}

From source file:com.nokia.dempsy.mpcluster.zookeeper.ZookeeperTestServer.java

public static int findNextPort() throws IOException {
    // find an unused ehpemeral port
    InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true); // this allows the server port to be bound to even if it's in TIME_WAIT
    serverSocket.bind(inetSocketAddress);
    port = serverSocket.getLocalPort();/*from  w w w. java  2  s.co m*/
    serverSocket.close();
    return port;
}

From source file:com.sharneng.net.misc.EchoServer.java

public EchoServer(InetSocketAddress from) throws IOException {
    this.from = from;
    serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true);//from  w  w  w.ja  v  a2  s  .c o m
    serverSocket.bind(from);
    String hostname = from.getHostName();
    if (hostname == null)
        hostname = "*";
    log.info("Ready to accept client connection on " + hostname + ":" + from.getPort());
}

From source file:gobblin.tunnel.MockServer.java

public MockServer start() throws IOException {
    _server = new ServerSocket();
    _server.setSoTimeout(5000);// ww  w.ja v a  2 s .co m
    _server.bind(new InetSocketAddress("localhost", 0));
    _serverSocketPort = _server.getLocalPort();
    _threads.add(new EasyThread() {
        @Override
        void runQuietly() throws Exception {
            runServer();
        }
    }.startThread());
    return this;
}

From source file:com.sharneng.net.portforward.Listener.java

/**
 * Constructs a new instance of Listener.
 * //  w  w  w  .  j  a va 2s . c  om
 * @param from
 *            the address to listen for connections
 * @param to
 *            the address to forward connections to
 * @throws IOException
 *             when something is not going write when network operation
 */
public Listener(InetSocketAddress from, InetSocketAddress to) throws IOException {
    this.from = from;
    this.to = to;
    serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true);
    serverSocket.bind(from);
    String hostname = from.getHostName();
    if (hostname == null)
        hostname = "*";
    log.info("Ready to accept client connection on " + hostname + ":" + from.getPort());
}

From source file:org.cloudfoundry.caldecott.client.TunnelServer.java

public TunnelServer(InetSocketAddress local, TunnelFactory tunnelFactory, TaskExecutor taskExecutor) {
    this.local = local;
    this.tunnelFactory = tunnelFactory;
    this.taskExecutor = taskExecutor;
    try {/*from  w  ww .j  av a  2 s  .  c o m*/
        this.serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(local);
    } catch (IOException e) {
        throw new TunnelException("Error configuring server socket", e);
    }
}

From source file:org.apache.servicemix.http.HttpWsdlTest.java

/**
 * determines a free port number/*from w w w .  j av  a  2s  .c  o  m*/
 * @return a free port number
 */
private static int getFreePortNumber() {

    int portNumber = usedPortNumber + 1;
    boolean inUse = true;

    while (inUse) {
        try {
            ServerSocket serverSocket = new ServerSocket();
            SocketAddress endpoint = new InetSocketAddress(portNumber);
            serverSocket.bind(endpoint);
            serverSocket.close();
            inUse = false;
            usedPortNumber = portNumber;
        } catch (IOException e) {
            inUse = true;
        }
    }

    return portNumber;

}