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:csparql_shim.SocketStream.java

/**
 * start listening on a socket and forwarding to csparql
 * stream in defined windows to allow comparison with cqels
 *///from  w ww. j  a  va  2 s  .  com
@Override
public void run() {
    ServerSocket ssock = null;
    Socket sock = null;
    try {
        ssock = new ServerSocket(this.port);
        sock = ssock.accept();

        DataInputStream is = new DataInputStream(sock.getInputStream());
        JSONParser parser = new JSONParser();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;

        int windowcount = 1;

        //loop for streaming in data
        while ((line = reader.readLine()) != null && !stop) {

            try {
                Object obj = parser.parse(line);
                JSONArray array = (JSONArray) obj;

                //stream the triple
                final RdfQuadruple q = new RdfQuadruple((String) array.get(0), (String) array.get(1),
                        (String) array.get(2), System.currentTimeMillis());
                this.put(q);
                System.out.println("triple sent at: " + System.currentTimeMillis());
            } catch (ParseException pe) {
                System.err.println("Error when parsing input, incorrect JSON.");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.graphaware.test.util.TestUtils.java

/**
 * Get some available port./*from   w w  w.jav  a 2  s .  c  o  m*/
 *
 * @return port number.
 */
public static int getAvailablePort() {
    try {
        ServerSocket socket = new ServerSocket(0);
        try {
            return socket.getLocalPort();
        } finally {
            socket.close();
        }
    } catch (IOException e) {
        throw new IllegalStateException("Cannot find available port: " + e.getMessage(), e);
    }
}

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

public void runServer() {
    try {//from w w  w . ja va2  s.c o m
        synchronized (this) {
            running = true;
            ss = new ServerSocket(port);
            log.info("SMTP Server started on port " + port);
        }
    } catch (IOException ex) {
        log.info(ex.getMessage());
    }

    while (running) {
        try {

            // wait for a client
            Socket socket = ss.accept();
            SMTPWorker thread = null;

            if (actAsMailet) {
                thread = new SMTPWorker(socket, st, configurationContext);
            } else {
                thread = new SMTPWorker(socket, st);
            }

            thread.start();
        } catch (IOException ex) {
            if (running) {
                log.info(ex.getMessage());
            }
        }
    }
}

From source file:fr.norad.jmxzabbix.core.utils.ZabbixStub.java

public void run() {
    try (ServerSocket serverSocket = new ServerSocket(port)) {
        try (Socket clientSocket = serverSocket.accept();
                OutputStream out = clientSocket.getOutputStream();
                InputStream in = clientSocket.getInputStream()) {
            int i = 0;
            Thread.sleep(waitBeforeReadSecond * 1000);
            while (i++ < requestsToRead) {
                requests.add(asServerReadRequest(in));
                System.out.println("received : " + mapper.writerWithDefaultPrettyPrinter()
                        .writeValueAsString(requests.get(requests.size() - 1)));
                asServerWriteResponse(out, new ZabbixResponse("ok", "cool"));
            }/*from  w  ww.j a  v a 2  s .  c om*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        throw new IllegalStateException("Cannot create socket on port " + port);
    }
}

From source file:com.kyne.webby.bukkit.RTKModuleSocket.java

public RTKModuleSocket(final int port, final BukkitWebbyPlugin plugin) throws IOException {
    this.serverSocket = new ServerSocket(port);
    this.plugin = plugin;
    LogHelper.initLogger("BukkitWebby", "Minecraft");
}

From source file:com.liferay.nativity.control.win.WindowsNativityControlImpl.java

@Override
public boolean connect() {
    if (_connected) {
        return true;
    }//from w w  w .j  a  v a2s .c om

    boolean loaded = WindowsNativityUtil.load();

    if (!loaded) {
        _logger.debug("WindowsNativityUtil failed to load");

        return false;
    }

    if (_serverSocket == null) {
        try {
            _serverSocket = new ServerSocket(_port);

            _connected = true;
        } catch (IOException ioe) {
            _logger.error(ioe.getMessage(), ioe);

            return false;
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            fireSocketOpenListeners();

            while (_connected) {
                handleConnection();
            }
        }
    };

    _executor.execute(runnable);

    return true;
}

From source file:ro.bmocanu.trafficproxy.peers.PeerCommunicationServer.java

/**
 * {@inheritDoc}//w  w w .j  a  v  a2s.  co m
 */
@Override
public void init() throws Exception {
    LOG.info("Preparing peer communication server");
    serverSocket = new ServerSocket(Configuration.corePeerPort);
    serverSocket.setSoTimeout(Constants.PEER_SOCKET_TIMEOUT);
}

From source file:com.myapplication.ServerService.java

@Override
protected void onHandleIntent(Intent intent) {
    ServerSocket serverSocket;// ww  w.  j  ava 2  s.c o  m
    Socket socket;
    try {
        serverSocket = new ServerSocket(Constant.SERVER_PORT);
        while (true) {
            //Listen for incoming connections on specified port
            //Block thread until someone connects
            socket = serverSocket.accept();
            clientAddress = socket.getInetAddress().getHostAddress();

            InputStream is = socket.getInputStream();

            String receivedMessage = CommonUtils.getStringFromInputStream(is);
            CommonUtils.showToast(receivedMessage);
            Intent messageIntent = new Intent(Constant.ACTION_MESSAGE_FROM_PEER);
            messageIntent.putExtra(Constant.KEY_MESSAGE_FROM_PEER, receivedMessage);
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(messageIntent);

            socket.close();
        }
    } catch (Exception e) {
        CommonUtils.showToast(e.getMessage());
    }
}

From source file:com.web.server.node.NodeServer.java

/**
 * This method implements the node server request
 */// w  w  w.  j  a v  a  2 s .  c o m
public void run() {
    ServerSocket serversock = null;
    Socket sock = null;
    try {
        serversock = new ServerSocket(port);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    while (true) {
        try {
            sock = serversock.accept();
            new NodeServerRequestProcessor(sock).start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.clustercontrol.winsyslog.SyslogReceiver.java

public synchronized void start() throws SocketException, UnknownHostException, IOException {

    if (tcpEnable) {
        tcpSocket = new ServerSocket(listenPort);
        tcpExecutor = createExecutorService("TcpReceiver");
        tcpExecutor.submit(new TcpReceiverTask(tcpSocket));
    }/*from   ww w.  j  av  a  2  s  .c o  m*/

    if (udpEnable) {
        udpSocket = new DatagramSocket(listenPort);
        udpExecutor = createExecutorService("UdpReceiver");
        udpExecutor.submit(new UdpReceiverTask(udpSocket));
    }

    if (!tcpEnable && !udpEnable) {
        log.warn("Both TCP and UDP are disabled.");
    }
}