Example usage for java.net Socket setKeepAlive

List of usage examples for java.net Socket setKeepAlive

Introduction

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

Prototype

public void setKeepAlive(boolean on) throws SocketException 

Source Link

Document

Enable/disable SocketOptions#SO_KEEPALIVE SO_KEEPALIVE .

Usage

From source file:Main.java

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

    client.close();/*w  ww  . jav a  2 s  .  co  m*/
}

From source file:gomokuserver.GomokuServer.java

/**
 * @param args the command line arguments
 * @throws java.lang.InterruptedException
 *///from   w w  w . j av a  2 s  . c o m
public static void main(String[] args) throws InterruptedException {
    if (args.length != 1) {
        System.out.println("Run with listen portno as argument");
        return;
    }

    int portno = Integer.parseInt(args[0]);
    ServerSocket sc;
    try {
        System.out.print("Creating socket bound to " + portno + " ... ");
        sc = new ServerSocket(portno);

        System.out.println("DONE");
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        return;
    }
    FrontOffice fo = new FrontOffice();
    Lobby lb = new Lobby();
    fo.setReferalLobby(lb);
    fo.start();
    lb.start();
    while (!interrupted()) {
        try {
            Socket playersocket = sc.accept();
            playersocket.setKeepAlive(true);
            System.out.println("Received client from " + playersocket.getInetAddress());
            fo.handle(playersocket);
        } catch (IOException ex) {
            Logger.getLogger(GomokuServer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    fo.interrupt();
    lb.interrupt();
    fo.join();
    lb.join();
}

From source file:org.java.plugin.boot.ControlThread.java

static boolean isApplicationRunning(final InetAddress host, final int port) {
    try {//from   ww w  .  j  a v  a  2 s  .  c om
        Socket socket = new Socket(host, port);
        try {
            socket.setKeepAlive(true);
            String test = "" + System.currentTimeMillis(); //$NON-NLS-1$
            OutputStream out = socket.getOutputStream();
            InputStream in = null;
            try {
                System.out.println("found running control service on " //$NON-NLS-1$
                        + host + ":" + port); //$NON-NLS-1$
                out.write(("PING " + test).getBytes()); //$NON-NLS-1$
                out.flush();
                socket.shutdownOutput();
                in = socket.getInputStream();
                StringBuilder commandResult = new StringBuilder();
                byte[] buf = new byte[16];
                int len;
                while ((len = in.read(buf)) != -1) {
                    commandResult.append(new String(buf, 0, len));
                }
                socket.shutdownInput();
                if (commandResult.toString().startsWith("OK") //$NON-NLS-1$
                        && (commandResult.toString().indexOf(test) != -1)) {
                    System.out.println("PING command succeed"); //$NON-NLS-1$
                    return true;
                }
                System.out.println("PING command failed"); //$NON-NLS-1$
            } finally {
                try {
                    out.close();
                } catch (IOException ioe) {
                    // ignore
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ioe) {
                        // ignore
                    }
                }
            }
        } finally {
            socket.close();
        }
    } catch (IOException ioe) {
        System.out.println("seems that there is no control service running on " //$NON-NLS-1$
                + host + ":" + port); //$NON-NLS-1$
        //ioe.printStackTrace();
    }
    return false;
}

From source file:org.java.plugin.boot.ControlThread.java

static boolean stopRunningApplication(final InetAddress host, final int port) {
    boolean result = false;
    try {/*from www  . ja  v  a  2  s  .  c  om*/
        Socket socket = new Socket(host, port);
        try {
            socket.setKeepAlive(true);
            OutputStream out = socket.getOutputStream();
            InputStream in = null;
            try {
                System.out.println("found running control service on " //$NON-NLS-1$
                        + host + ":" + port); //$NON-NLS-1$
                out.write("STOP".getBytes()); //$NON-NLS-1$
                out.flush();
                socket.shutdownOutput();
                in = socket.getInputStream();
                StringBuilder commandResult = new StringBuilder();
                byte[] buf = new byte[16];
                int len;
                while ((len = in.read(buf)) != -1) {
                    commandResult.append(new String(buf, 0, len));
                }
                socket.shutdownInput();
                if (commandResult.toString().startsWith("OK")) { //$NON-NLS-1$
                    System.out.println("STOP command succeed"); //$NON-NLS-1$
                    result = true;
                } else {
                    System.out.println("STOP command failed"); //$NON-NLS-1$
                }
            } finally {
                try {
                    out.close();
                } catch (IOException ioe) {
                    // ignore
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ioe) {
                        // ignore
                    }
                }
            }
        } finally {
            socket.close();
        }
    } catch (IOException ioe) {
        System.out.println("seems that there is no control service running on " //$NON-NLS-1$
                + host + ":" + port); //$NON-NLS-1$
        //ioe.printStackTrace();
    }
    if (result) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            // ignore
        }
    }
    return result;
}

From source file:com.huawei.streaming.cql.LocalTaskCommons.java

private static void waitingUtilClose() {
    while (true) {
        Socket socket = null;
        try {/*from  w  ww  .  jav a  2 s  .co  m*/
            socket = new Socket("127.0.0.1", 7999);
            socket.setKeepAlive(true);
            LOG.info("port is open now, waiting one second to retry");
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            break;
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.machinepublishers.jbrowserdriver.StreamConnectionClient.java

private static Socket newSocket(final HttpContext context) throws IOException {
    InetSocketAddress proxySocks = (InetSocketAddress) context.getAttribute("proxy.socks.address");
    Socket socket;
    if (proxySocks != null) {
        socket = new Socket(new Proxy(Proxy.Type.SOCKS, proxySocks));
    } else {//from ww w  .  j av  a  2  s  .  c  om
        socket = new Socket();
    }
    socket.setTcpNoDelay(true);
    socket.setKeepAlive(true);
    return socket;
}

From source file:org.jmxtrans.embedded.util.pool.SocketOutputStreamPoolFactory.java

@Override
public SocketOutputStream create(HostAndPort hostAndPort) throws Exception {
    Socket socket = new Socket();
    socket.setKeepAlive(true);
    socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()),
            socketConnectTimeoutInMillis);
    return new SocketOutputStream(socket);
}

From source file:org.apache.jmeter.visualizers.backend.graphite.SocketOutputStreamPoolFactory.java

@Override
public SocketOutputStream create(SocketConnectionInfos connectionInfos) throws Exception {
    Socket socket = new Socket();
    socket.setKeepAlive(true);
    socket.setSoTimeout(socketTimeoutInMillis);
    socket.connect(new InetSocketAddress(connectionInfos.getHost(), connectionInfos.getPort()),
            socketConnectTimeoutInMillis);

    return new SocketOutputStream(socket);
}

From source file:org.jmxtrans.embedded.util.pool.SocketWriterPoolFactory.java

@Override
public SocketWriter create(HostAndPort hostAndPort) throws Exception {
    Socket socket = new Socket();
    socket.setKeepAlive(true);
    socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()),
            socketConnectTimeoutInMillis);

    return new SocketWriter(socket, charset);
}

From source file:net.sheehantech.cherry.pool.PooledPushSocketFactory.java

@Override
public PooledPushSocket create() throws Exception {
    Socket socket = socketFactory.createSocket(gateway, port);
    socket.setKeepAlive(true);
    PooledPushSocket pushSocket = new PooledPushSocket(socket);
    pushSocket.prepare();//from ww w .java  2s.com
    logger.info("Initialised new socket {}", pushSocket);
    return pushSocket;
}