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:org.apache.axis2.transport.http.server.DefaultConnectionListener.java

public void run() {
    try {/*from ww w  . j a  v a 2 s .  c o m*/
        while (!Thread.interrupted()) {
            try {
                if (serversocket == null || serversocket.isClosed()) {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Listening on port " + port);
                    }
                    serversocket = new ServerSocket(port);
                    serversocket.setReuseAddress(true);
                }
                LOG.debug("Waiting for incoming HTTP connection");
                Socket socket = this.serversocket.accept();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Incoming HTTP connection from " + socket.getRemoteSocketAddress());
                }
                AxisHttpConnection conn = new AxisHttpConnectionImpl(socket, this.params);
                try {
                    this.connmanager.process(conn);
                } catch (RejectedExecutionException e) {
                    conn.sendResponse(new DefaultHttpResponseFactory().newHttpResponse(HttpVersion.HTTP_1_0,
                            HttpStatus.SC_SERVICE_UNAVAILABLE, new BasicHttpContext(null)));
                }
            } catch (java.io.InterruptedIOException ie) {
                break;
            } catch (Throwable ex) {
                if (Thread.interrupted()) {
                    break;
                }
                if (!failureHandler.failed(this, ex)) {
                    break;
                }
            }
        }
    } finally {
        destroy();
    }
}

From source file:och.util.socket.server.SocketServer.java

private void init() throws IOException {

    log.info("Start " + name + " [port:" + port + ", threadsCount:" + maxThreads + "]");

    serverSocket = new ServerSocket(port);
    serverSocket.setSoTimeout(waitConnectTimeout);

    //connect threads
    connectProcessingPool = newFixedThreadPool("SocketServer-process-connect", maxThreads, this);

    //wait connect thread
    waitConnectThread = newSingleThreadExecutor("SocketServer-wait-connect");
    endFuture = waitConnectThread.submit(new Runnable() {
        @Override/*from   w w w  .j  a v  a 2s .  c om*/
        public void run() {
            try {
                while (!shutdownFlag) {
                    Socket socket = null;
                    try {
                        socket = serverSocket.accept();
                        connectProcessingPool.submit(new ProcessSocketTask(socket));
                    }
                    //queue is full
                    catch (RejectedExecutionException e) {
                        try {
                            socket.close();
                        } catch (Exception ignore) {
                        }
                    }
                    //stoped waiting
                    catch (SocketTimeoutException e) {
                        /*ok*/}
                }
            } catch (Throwable t) {
                log.error("error in wait connect loop", t);
            } finally {
                shutdownAll();
            }
        }
    });
}

From source file:com.kyne.webby.rtk.web.WebServer.java

public WebServer(final WebbyRTKModule rtkModule, final int port) throws IOException {
    LogHelper.initLogger("WebbyRTKModule:WebServer", "Minecraft");
    this.connectedAdmins = new HashMap<String, Date>();
    this.lastUrlByIp = new HashMap<InetAddress, String>();
    this.htmlCache = new HashMap<String, byte[]>();
    this.serverSocket = new ServerSocket(port);
    this.rtkModule = rtkModule;
    this.allowedRessources = this.getAllowedRessources();
}

From source file:Networking.Client.java

public Client(int port, Writable toWrite, Server ser, SecureNode d) {
    this.port = port;
    this.writer = toWrite;
    this.sender = ser;
    this.node = d;
    this.client = new Thread(this, "AKE");

    try {/*from  ww w  .j  a v  a2  s  .c om*/
        server = new ServerSocket(port);
    } catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.wentam.defcol.connect_to_computer.WebServer.java

public void runServer() {
    try {// w w  w. j  ava  2 s.  com
        serverSocket = new ServerSocket(serverPort);

        serverSocket.setReuseAddress(true);

        while (running) {
            try {
                final Socket socket = serverSocket.accept();

                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();

                serverConnection.bind(socket, new BasicHttpParams());

                httpService.handleRequest(serverConnection, httpContext);

                serverConnection.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (HttpException e) {
                e.printStackTrace();
            }
        }

        serverSocket.close();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    running = false;
}

From source file:net.usikkert.kouchat.testclient.TestClientTelnetServer.java

private void startServer() {
    processMessages = true;/*from w w w  . java 2s  .  c  o m*/

    try {
        final ServerSocket serverSocket = new ServerSocket(20000);
        socket = serverSocket.accept();
        IOUtils.closeQuietly(serverSocket);

        writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        ui.setWriter(writer);

        processMessages();
    }

    catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.amazonaws.auth.EC2MetadataServiceMock.java

private ServerSocket startServerSocket() {
    try {/*w  ww .ja  v a2  s. c om*/
        ServerSocket serverSocket = new ServerSocket(0);

        System.setProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY,
                "http://localhost:" + serverSocket.getLocalPort());
        System.out.println("Started mock metadata service at: "
                + System.getProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY));

        return serverSocket;
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to start mock EC2 metadata server", ioe);
    }
}

From source file:com.brienwheeler.lib.io.TestListener.java

private void openServerSocket(int port) throws IOException {
    listenSocket = new ServerSocket(port);
    listenSocket.setReuseAddress(true);// w ww .j  a  va2  s .c o  m
}

From source file:dk.dma.ais.virtualnet.transponder.Transponder.java

public Transponder(TransponderConfiguration conf) throws IOException {
    this.conf = conf;
    status = new TransponderStatus();
    serverConnection = new ServerConnection(this, conf);
    serverSocket = new ServerSocket(conf.getPort());
    ownMessage = new TransponderOwnMessage(this, conf.getOwnPosInterval());
    vdoTransformer = new VdmVdoTransformer(conf.getOwnMmsi(), "AI");
    cropTransformer = new CropVdmTransformer();
    if (conf.isSendPsttSentence()) {
        psttSender = new StreamTime();
    } else {/*ww  w  .  j a v a  2 s . c  o  m*/
        psttSender = null;
    }
}

From source file:SimpleDaemon.java

/**
 * init and destroy were added in jakarta-tomcat-daemon.
 *//*from w  w w  .  j ava2  s . c  om*/
public void init(DaemonContext context) throws Exception {
    System.err.println("SimpleDaemon: instance " + this.hashCode() + " init");

    int port = 1200;

    String[] a = context.getArguments();
    try {
        if (a.length > 0)
            port = Integer.parseInt(a[0]);
    } catch (NumberFormatException ex) {
        throw new DaemonInitException("parsing port", ex);
    }
    if (a.length > 1)
        this.directory = a[1];
    else
        this.directory = "/tmp";

    /* Dump a message */
    System.err.println("SimpleDaemon: loading on port " + port);

    /* Set up this simple daemon */
    this.controller = context.getController();
    this.server = new ServerSocket(port);
    this.thread = new Thread(this);
}