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:no.antares.clutil.hitman.MessageChannel.java

/** Open a channel - close when done */
public static MessageChannel openInbound(int port) {
    try {/*  www. j  a  v  a 2 s. com*/
        return new MessageChannel(new ServerSocket(port));
    } catch (IOException e) {
        logger.fatal("(): " + port, e);
        throw new RuntimeException("Could not listen on port: " + port, e);
    }
}

From source file:com.miraclelinux.historygluon.ConnectionThread.java

@Override
public void run() {
    ServerSocket server = null;//  ww w  .j a  va  2s.  co m
    try {
        server = new ServerSocket(m_port);
        m_log.info("start listening on port: " + server.getLocalPort());
        while (true) {
            Socket client = server.accept();
            StorageDriver driver = m_driver.createInstance();
            BridgeWorker bridge = new BridgeWorker(client, driver);
            bridge.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
        m_log.error(e);
    }
}

From source file:com.ning.billing.dbi.MysqlTestingHelper.java

public void startMysql() throws IOException {
    // New socket on any free port
    final ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();//w  ww .j ava 2 s.c  o  m
    socket.close();

    dbDir = File.createTempFile("mysql", "");
    dbDir.delete();
    dbDir.mkdir();
    mysqldResource = new MysqldResource(dbDir);

    final Map<String, String> dbOpts = new HashMap<String, String>();
    dbOpts.put(MysqldResourceI.PORT, Integer.toString(port));
    dbOpts.put(MysqldResourceI.INITIALIZE_USER, "true");
    dbOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, USERNAME);
    dbOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, PASSWORD);

    mysqldResource.start("test-mysqld-thread", dbOpts);
    if (!mysqldResource.isRunning()) {
        throw new IllegalStateException("MySQL did not start.");
    } else {
        log.info("MySQL running on port " + mysqldResource.getPort());
    }
}

From source file:flens.input.GraphiteInput.java

@Override
protected ServerSocket makeListener() throws IOException {
    return new ServerSocket(port);
}

From source file:com.ning.metrics.collector.processing.db.CollectorMysqlTestingHelper.java

public void startMysql() throws IOException {
    ServerSocket socket = new ServerSocket(0);

    port = socket.getLocalPort();/*from   w w  w  .  j ava 2  s.co m*/
    socket.close();

    dbDir = File.createTempFile("mysqldb", ".db");
    Assert.assertTrue(dbDir.delete());
    Assert.assertTrue(dbDir.mkdir());

    mysqldResource = new MysqldResource(dbDir);

    Map<String, String> dbOpts = new HashMap<String, String>();

    dbOpts.put(MysqldResourceI.PORT, Integer.toString(port));
    dbOpts.put(MysqldResourceI.INITIALIZE_USER, "true");
    dbOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, USERNAME);
    dbOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, PASSWORD);

    mysqldResource.start("test-mysqld-thread", dbOpts);

    if (!mysqldResource.isRunning()) {
        throw new IllegalStateException("MySQL did not start.");
    }
}

From source file:com.github.avarabyeu.restendpoint.http.GuiceTestModule.java

private static int findFreePort() {
    ServerSocket socket = null;//w w w.  ja  v  a 2s  .  co  m
    try {
        socket = new ServerSocket(0);
        System.out.println("Used Port: " + socket.getLocalPort());
        return socket.getLocalPort();
    } catch (IOException e) {
        throw new RuntimeException("Unable to find free port", e);
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}

From source file:com.ning.arecibo.dao.MysqlTestingHelper.java

public MysqlTestingHelper() {
    // New socket on any free port
    final ServerSocket socket;
    try {//w ww.  j a  va2 s  .c om
        socket = new ServerSocket(0);
        port = socket.getLocalPort();
        socket.close();
    } catch (IOException e) {
        Assert.fail();
    }
}

From source file:de.xwic.appkit.core.cluster.impl.InboundConnectionHandler.java

@Override
public void run() {

    ServerSocket srvSocket;/*ww w .  j a v a2s .  c  om*/
    try {
        srvSocket = new ServerSocket(port);

    } catch (IOException e) {
        log.error("Can not open server socket", e);
        return; // exit
    }

    int conHandlerCount = 0;
    int errCount = 0;
    long lastErr = 0;
    while (true) {
        try {
            Socket socket = srvSocket.accept();

            log.debug("Accepted connection from " + socket.getInetAddress().getHostAddress());

            ClientHandler ch = new ClientHandler(cluster, socket);

            Thread t = new Thread(ch,
                    "ConnectionHandler-" + socket.getInetAddress().getHostAddress() + "-" + conHandlerCount++);
            t.setDaemon(true); // launch as a Deamon
            t.start();

        } catch (IOException e) {
            log.error("Error accepting incoming connection...", e);
            if ((System.currentTimeMillis() - lastErr) < 3000) { // the last error was just 3 seconds ago
                errCount++;
                if (errCount > 100) {
                    log.error("More than 100 errors occured within the last 3 seconds. Giving up.");
                    break; // break the loop
                }
            } else {
                errCount = 0;
            }
            lastErr = System.currentTimeMillis();
        }
    }

}

From source file:br.com.i9torpedos.model.service.serverSocket.DSSocketSIM2.java

public DSSocketSIM2() throws SocketServerException {
    this.listaSMS = new LinkedList<>();
    this.random = new Random();
    this.ponte = new BridgeSendSMSMessage();

    if (server == null) {

        try {/*from   w w w .  ja  va 2s .  com*/
            this.server = new ServerSocket(PORTA_SERVERSOCKET);
            log.info("Socket Server Iniciado com Sucesso porta: " + PORTA_SERVERSOCKET);

        } catch (IOException ex) {

            log.fatal("Falha ao iniciar o Servidor Socket na porta: " + PORTA_SERVERSOCKET + " "
                    + ex.getMessage());
            throw new SocketServerException(
                    "Falha ao iniciar o Servidor Socket na porta: " + PORTA_SERVERSOCKET, ex);

        }
    }
}

From source file:br.com.i9torpedos.model.service.serverSocket.DSSocketSIM1.java

public DSSocketSIM1() throws SocketServerException {
    this.listaSMS = new LinkedList<>();
    this.random = new Random();
    this.ponte = new BridgeSendSMSMessage();

    if (server == null) {

        try {/*from   www .  ja  v a 2  s .c om*/
            this.server = new ServerSocket(PORTA_SERVERSOCKET);
            log.info("Socket Server Iniciado com Sucesso porta: " + PORTA_SERVERSOCKET);

        } catch (IOException ex) {

            log.fatal("Falha ao iniciar o Servidor Socket na porta: " + PORTA_SERVERSOCKET + " "
                    + ex.getMessage());
            throw new SocketServerException(
                    "Falha ao iniciar o Servidor Socket na porta: " + PORTA_SERVERSOCKET, ex);

        }
    }
}