Example usage for java.net Socket isClosed

List of usage examples for java.net Socket isClosed

Introduction

In this page you can find the example usage for java.net Socket isClosed.

Prototype

public boolean isClosed() 

Source Link

Document

Returns the closed state of the socket.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket client = new Socket("google.com", 80);

    System.out.println(client.isClosed());

    client.close();//from  w w w  .  ja v  a  2s. co  m
}

From source file:com.gomoob.embedded.EmbeddedMongo.java

/**
 * Main entry of the program./*from  w w  w. j a  va2  s  .c  o m*/
 * 
 * @param args arguments used to customize starting.
 * 
 * @throws Exception If an error occured while executing the server.
 */
public static void main(String[] args) throws Exception {

    // Creates a default execution context, then the configuration of this context can be changed depending on the 
    // command line parameters received.
    context = new Context();

    // Parse the command line
    parseCommandLine(args);

    boolean terminated = false;

    System.out.println("MONGOD_HOST=" + context.getMongoContext().getNet().getServerAddress().getHostName());
    System.out.println("MONGOD_PORT=" + context.getMongoContext().getNet().getPort());
    System.out.println("SERVER_SOCKET_PORT=" + context.getSocketContext().getServerSocket().getLocalPort());

    Socket socket = null;
    BufferedReader reader = null;
    Writer writer = null;

    while (!terminated) {

        socket = context.getSocketContext().getServerSocket().accept();

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

        System.out.println("Waiting for command...");
        String commandString = reader.readLine();
        System.out.println(commandString);

        ICommand command = parseCommandString(commandString);

        IResponse response = command.run(context);

        writer.write(response.toJSON());

        writer.close();

        // If the current socket is opened close it
        if (!socket.isClosed()) {
            socket.close();
        }

        // If stop is required
        terminated = response.isTerminationRequired();

    }

    context.getSocketContext().getServerSocket().close();
}

From source file:com.sharneng.net.NetUtils.java

/**
 * Quietly close a {@linkplain Socket} object. It tries to shutdown the input and output of the socket before close.
 * Exceptions are ignored./*from w  ww .  ja va2 s  .co  m*/
 * 
 * @param socket
 *            the Socket object to close
 * @see #shutdown(Socket)
 */
public static void close(Socket socket) {
    if (socket == null)
        return;

    if (!socket.isClosed()) {
        shutdown(socket);
        try {
            socket.close();
        } catch (Throwable e) {
            log.debug(e.getMessage(), e);
        }
    }
}

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

public static void close(Socket socket) {
    try {//from  w w w. j  a  va  2s.c  o  m
        if (socket != null && !socket.isClosed())
            socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.linkedin.databus2.test.TestUtil.java

/**
 * Checks if a server is running on a given host and port
 * @param host        the server host/*from   www .j  av a  2s  .c  om*/
 * @param port        the server port
 * @param log         logger for diagnostic messages (can be null)
 * @return true if successful
 * @throws IOException
 */
public static boolean checkServerRunning(String host, int port, Logger log, boolean logError) {
    boolean success = false;

    try {
        Socket socket = new Socket(host, port);
        log.info("host=" + host + " port=" + port);
        log.info("Socket Info:" + socket.toString());
        log.info("IsConnected=" + socket.isConnected() + " isClosed=" + socket.isClosed() + " isBound="
                + socket.isBound());
        success = socket.isConnected();
        socket.close();
    } catch (ConnectException ce) {
        if (null != log)
            log.error("Fail to connect to port:" + port);
        if (logError && null != log)
            log.error("Connect error", ce);
        success = false;
    } catch (IOException e) {
        if (logError && null != log)
            log.error("connect error", e);
    } catch (RuntimeException e) {
        if (logError && null != log)
            log.error("runtime error", e);
    }

    return success;
}

From source file:pt.minha.calibration.Calibrator.java

private static void runServer() throws Exception {
    // Reality server
    ServerSocket ss = new ServerSocket(12345);

    logger.info("server: started at {}", ss.getLocalSocketAddress());

    while (true) {
        Socket s = ss.accept();

        logger.info("server: accepted {}", s.getRemoteSocketAddress());

        try {/*  w  ww .j av  a  2  s. c o  m*/
            ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
            oos.flush();
            ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(s.getInputStream()));

            while (!s.isClosed()) {
                Map<String, Object> p = (Map<String, Object>) ois.readObject();
                Benchmark next = (Benchmark) Class.forName((String) p.get("bench")).newInstance();
                next.setParameters(p);

                logger.info("server: running {}", p);

                Object result = next.server();

                logger.info("server: running {} done", p);

                oos.writeObject(result);
                oos.flush();
            }

            logger.info("server: disconnected {}", s.getRemoteSocketAddress());

        } catch (IOException ioe) {
            logger.info("server: disconnected {} on {}", s.getRemoteSocketAddress(), ioe);
        }
    }
}

From source file:org.gcaldaemon.core.ldap.LDAPListener.java

private static final void closeSocket(Socket socket) {
    if (socket != null) {
        try {/*from w  w w .j  ava  2 s  . c  o  m*/
            if (!socket.isClosed()) {
                socket.close();
            }
        } catch (Exception ignored) {
        }
    }
}

From source file:Main.java

/**
 * Performs execution of the shell command on remote host.
 * @param host ip address or name of the host.
 * @param port telnet port/* w w  w .  java 2  s  .c o  m*/
 * @param command shell command to be executed.
 * @return true if success, false on error.
 */
public static final boolean executeRemotely(String host, int port, String command) {
    Socket socket = null;
    OutputStream os = null;

    try {
        socket = new Socket(host, port);

        os = socket.getOutputStream();

        os.write(command.getBytes());
        os.flush();

        return true;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (socket != null && !socket.isClosed()) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.sikuli.scriptrunner.ScriptRunner.java

public static void startRemoteRunner(String[] args) {
    int port = getPort(args.length > 0 ? args[0] : null);
    try {/*from w ww  .j  av  a2 s.c o  m*/
        try {
            if (port > 0) {
                server = new ServerSocket(port);
            }
        } catch (Exception ex) {
            log(-1, "Remote: at start: " + ex.getMessage());
        }
        if (server == null) {
            log(-1, "Remote: could not be started on port: " + (args.length > 0 ? args[0] : null));
            System.exit(1);
        }
        while (true) {
            log(lvl, "Remote: now waiting on port: %d at %s", port,
                    InetAddress.getLocalHost().getHostAddress());
            Socket socket = server.accept();
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new Scanner(socket.getInputStream());
            HandleClient client = new HandleClient(socket);
            isHandling = true;
            while (true) {
                if (socket.isClosed()) {
                    shouldStop = client.getShouldStop();
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
            }
            if (shouldStop) {
                break;
            }
        }
    } catch (Exception e) {
    }
    if (!isHandling) {
        log(-1, "Remote: start handling not possible: " + port);
    }
    log(lvl, "Remote: now stopped on port: " + port);
}

From source file:neohope.mule.hl7v2.socket.AbstractTcpSocketFactory.java

public void destroyObject(Object key, Object object) throws Exception {
    Socket socket = (Socket) object;
    if (!socket.isClosed()) {
        socket.close();//  w  w  w. ja  v  a  2 s. co m
    }
}