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: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 {//ww w  .  ja v  a2  s.c om
            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:com.netflix.explorers.helloworld.ExplorerAppTest.java

private static int getLocalPort() throws IOException {
    ServerSocket ss = new ServerSocket(0);
    ss.setReuseAddress(true);//from w  w w. j a  va 2s  . co  m
    return ss.getLocalPort();
}

From source file:com.alibaba.jstorm.utils.NetWorkUtils.java

/**
 * Check whether the port is available to binding
 * //  www .java2  s .  co  m
 * @param port
 * @return -1 means not available, others means available
 * @throws IOException
 */
public static int tryPort(int port) throws IOException {
    ServerSocket socket = new ServerSocket(port);
    int rtn = socket.getLocalPort();
    socket.close();
    return rtn;
}

From source file:mainserver.Server.java

private Server() {
    try {/*from   w w w .  j a v  a2s . c o  m*/
        serverSocket = new ServerSocket(Config.getPortNumber());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ninja.utils.NinjaTestServer.java

private static int findAvailablePort(int min, int max) {
    for (int port = min; port < max; port++) {
        try {//from  ww  w.  j  a  v a2  s  .  c  o  m
            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.iitb.cse.ConnectionInfo.java

public static synchronized void startlistenForClients(final Session session) {
    try {//from w w w  . ja v a2s .  c om

        if (session.connectionSocket != null) {
            stoplistenForClients(session);
            System.out.println("\nExisting Port closed");
            try {
                Thread.sleep(5000);
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
            startlistenForClients(session);
        }

        session.connectionSocket = new ServerSocket(Constants.ConnectionPORT);
        Constants.listenOnPort = true;
        while (true && acceptConnection) {
            System.out.println("\nListening for Client to Connect ......");
            final Socket sock = session.connectionSocket.accept();
            System.out.println("\nClient COnnected ......");
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    threadNo++;
                    ClientConnection.handleConnection(sock, session, threadNo);
                }
            };

            Thread t = new Thread(r);

            t.start();

        }
        System.out.println("\nStopping Listening!!!!!!!1");
    } catch (IOException ex) {
        try {
            if (session.connectionSocket != null) {
                stoplistenForClients(session);

            }
        } catch (Exception ex1) {
            System.out.println("\nException" + ex.toString() + "\n");
        }
    }
}

From source file:pl.edu.agh.BackgroundServiceConnection.java

@Override
public void afterPropertiesSet() throws Exception {

    serverSocket = new ServerSocket(serverPort);
    new Thread(this).start();
}

From source file:com.cisco.cta.taxii.adapter.TcpServer.java

public TcpServer() throws IOException {
    serverSocket = new ServerSocket(port++);
}

From source file:org.apache.axis2.transport.mail.server.POP3Server.java

public POP3Server(Storage st, int port) throws AxisFault {
    this.st = st;

    try {//from   www.  j  ava  2  s.c om
        synchronized (this) {
            serverSocket = new ServerSocket(port);
            log.info("Server started on port " + port);
        }
    } catch (IOException e) {
        throw new AxisFault(e.getMessage(), e);
    }
}

From source file:com.l2jfree.gameserver.geoeditorcon.GeoEditorListener.java

private GeoEditorListener() {
    try {/*from w  w  w.j  a  v  a 2s .com*/
        _serverSocket = new ServerSocket(PORT);
    } catch (IOException e) {
        _log.fatal("Error creating geoeditor listener! ", e);
        System.exit(1);
    }
    start();
    _log.info("GeoEditorListener Initialized.");
}