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, int backlog, InetAddress bindAddr) throws IOException 

Source Link

Document

Create a server with the specified port, listen backlog, and local IP address to bind to.

Usage

From source file:org.fourthline.cling.transport.impl.apache.StreamServerImpl.java

synchronized public void init(InetAddress bindAddress, Router router) throws InitializationException {

    try {/*from   w ww . j  a v a 2s . com*/

        this.router = router;

        this.serverSocket = new ServerSocket(configuration.getListenPort(),
                configuration.getTcpConnectionBacklog(), bindAddress);

        log.info("Created socket (for receiving TCP streams) on: " + serverSocket.getLocalSocketAddress());

        this.globalParams
                .setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
                        configuration.getDataWaitTimeoutSeconds() * 1000)
                .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
                        configuration.getBufferSizeKilobytes() * 1024)
                .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,
                        configuration.isStaleConnectionCheck())
                .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, configuration.isTcpNoDelay());

    } catch (Exception ex) {
        throw new InitializationException(
                "Could not initialize " + getClass().getSimpleName() + ": " + ex.toString(), ex);
    }

}

From source file:com.oakesville.mythling.util.MediaStreamProxy.java

public void init() throws IOException {
    localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
    socket = new ServerSocket(port, 0, localhost);
    socket.setSoTimeout(5000); // TODO hardcoded
    port = socket.getLocalPort();//from   ww w.  ja  va 2 s.  c om
}

From source file:com.meh.CopyProxy.java

public void init() {
    try {//from w  w  w  . j a  v a2s  .  com
        socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
        socket.setSoTimeout(5000);
        port = socket.getLocalPort();
        Log.d(tag, "port " + port + " obtained");
    } catch (UnknownHostException e) {
        Log.e(tag, "Error initializing server", e);
    } catch (IOException e) {
        Log.e(tag, "Error initializing server", e);
    }
}

From source file:edu.hawaii.soest.pacioos.text.MockDataSource.java

/**
 * Implements the Runnable interface, and creates a local data source server by
 * binding to the 127.0.0.1:5111 address.  Upon a client connection, sends all lines
 * of the mock data file to the client.//from ww  w  . j a v  a  2  s . c  om
 * @see java.lang.Runnable#run()
 */
public void run() {
    String host = "127.0.0.1";
    int portNumber = 5111;
    clientSocket = null;
    InetAddress address = null;
    try {
        address = InetAddress.getByName(host);
        serverSocket = new ServerSocket(portNumber, 1, address);
        log.info("Accepting connections on " + serverSocket.getInetAddress() + ":"
                + serverSocket.getLocalPort());
        clientSocket = serverSocket.accept();

    } catch (IOException ioe) {
        log.error("Couldn't open the server socket: " + ioe.getMessage());
    }

    File ctdData = new File(mockCTDData);
    List<String> lines = new ArrayList<String>();
    try {
        lines = FileUtils.readLines(ctdData);

    } catch (IOException e) {
        log.error("Couldn't read data file: " + e.getMessage());

    }

    try {
        if (this.clientSocket.isConnected()) {
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            // loop through the file and send each line over the wire
            for (String line : lines) {
                line = line + "\r\n";
                log.debug("Line bytes: " + new String(Hex.encodeHex(line.getBytes("US-ASCII"))));
                out.print(line);
                out.flush();
                Thread.sleep(500);
            }
            out.close();
        } else {
            log.debug("Client is not connected");
        }
    } catch (IOException e) {
        e.printStackTrace();

    } catch (InterruptedException e) {
        e.printStackTrace();

    }

}

From source file:com.seajas.search.utilities.rmi.RestrictedHostSocketFactory.java

/**
 * Create a server socket which only binds to the specified hostname.
 * /*from  w w  w .  j  a v  a 2  s.  c  o  m*/
 * @param port
 * @return ServerSocket
 * @throws IOException
 */
@Override
public ServerSocket createServerSocket(final int port) throws IOException {
    InetAddress inetAddress = null;

    if (logger.isInfoEnabled())
        logger.info(
                "Creating (server-side) socket #" + totalSocketsCreated.incrementAndGet() + " on port " + port);

    if (!StringUtils.isEmpty(hostname) && !hostname.equals("0.0.0.0"))
        inetAddress = InetAddress.getByName(hostname);

    ServerSocket result = new ServerSocket(port, DEFAULT_BACKLOG, inetAddress);

    result.setReuseAddress(true);

    return result;
}

From source file:audio.StreamProxy.java

public void init() {
    try {/*from   w  w  w  . j av  a  2 s.co  m*/
        socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
        socket.setSoTimeout(5000);
        port = socket.getLocalPort();
        Log.d(LOG_TAG, "port " + port + " obtained");
    } catch (UnknownHostException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    }
}

From source file:com.arthur.pervasivenfc.ExecuteActivity.StreamProxy.java

public void init() {
    try {/*from  w  w  w. ja  v  a  2  s.c om*/
        socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
        //socket = new ServerSocket(port);
        //socket.setSoTimeout(5000);
        port = socket.getLocalPort();
        Log.d(LOG_TAG, "port " + port + " obtained");
    } catch (UnknownHostException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    }
}

From source file:com.code.android.vibevault.StreamProxy.java

public void init() {
    try {//from  w  ww  .j a v  a  2s  .  c  om
        socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
        socket.setSoTimeout(5000);
        port = socket.getLocalPort();

    } catch (UnknownHostException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error initializing server", e);
    }
}

From source file:org.prx.prp.StreamProxy.java

public void init(MediaPlayer m, Context mContext) {
    try {/*from w w w. java  2  s. c  o  m*/
        this.m = m;
        this.mContext = mContext;
        socket = new ServerSocket(port, 0, InetAddress.getLocalHost());
        socket.setSoTimeout(5000);
        port = socket.getLocalPort();
        Log.d(getClass().getName(), "port " + port + " obtained");
    } catch (UnknownHostException e) {
        Log.e(getClass().getName(), "Error initializing server", e);
    } catch (IOException e) {
        Log.e(getClass().getName(), "Error initializing server", e);
    }
}

From source file:org.alfresco.util.remote.server.socket.HostConfigurableSocketFactory.java

public ServerSocket createServerSocket(int port) throws IOException {
    ServerSocket socket = null;//from  w  w w .ja  va  2 s . co  m
    for (int i = 0; socket == null && i < retries + 1; i++) {
        try {
            socket = new ServerSocket(port, 50, this.host);
        } catch (BindException e) {
            if (i >= retries) {
                // We're out of retries, abort
                if (logger.isErrorEnabled()) {
                    logger.error("Failed to create server socket on port " + port + ", tried " + (i + 1)
                            + " times.");
                }
                throw e;
            } else {
                // Sleep and try again
                if (logger.isWarnEnabled()) {
                    logger.warn("Port " + port + " in-use, will retry after " + retryInterval
                            + " millis (was attempt #" + (i + 1) + ")");
                }
                try {
                    Thread.sleep(retryInterval);
                } catch (InterruptedException ie) {
                }
            }
        }
    }
    return socket;
}