Example usage for java.net Socket Socket

List of usage examples for java.net Socket Socket

Introduction

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

Prototype

public Socket() 

Source Link

Document

Creates an unconnected Socket.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    sock.connect(sockaddr);/*from   w w w  .j  ava2 s .  c  om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000;
    sock.connect(sockaddr, timeoutMs);// www.  j  a  v  a 2  s. co  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000; // 2 seconds
    sock.connect(sockaddr, timeoutMs);/*from  ww w .j  a v a 2 s .  c o m*/
}

From source file:com.sm.test.TestConnect.java

public static void main(String[] args) throws Exception {
    String[] opts = new String[] { "-host", "-port", "-times" };
    String[] defaults = new String[] { "ssusd003.ir.dc", "6666", "500" };
    String[] paras = getOpts(args, opts, defaults);
    String host = paras[0];//from   w  w  w .ja v a2 s  . c o m
    int port = Integer.valueOf(paras[1]);
    int timeout = Integer.valueOf(paras[2]);

    //        logger.info("pos="+"/test/".indexOf("/"));
    //        logger.info("/test/".substring(0, "/test/".indexOf("/")));
    //        BuildClusterNodes bc = new BuildClusterNodes("./clusterStore/src/test/resources/config.xml");
    //        logger.info(bc.build().toString());
    //        BuildStoreConfig bs = new BuildStoreConfig("./clusterStore/src/test/resources/config.xml");
    //        logger.info(bs.build().toString());
    long begin = System.currentTimeMillis();
    SocketAddress inet = new InetSocketAddress(host, port);
    try {

        Socket socket = new Socket();
        socket.connect(inet, timeout);
        socket.close();
    } catch (Exception ex) {
        logger.error("ex " + ex.toString());
    }
    logger.info("time in Socket ms " + (System.currentTimeMillis() - begin));
    //        begin = System.currentTimeMillis();
    //        try {
    //            TCPClient client = TCPClient.start(host, port);
    //            client.close();
    //        } catch (Exception ex) {
    //            logger.error( ex.getMessage());
    //        }
    //        logger.info("time in TCPClient ms "+(System.currentTimeMillis() - begin));

}

From source file:Main.java

public static boolean isPortOpen(final String ip, final int port, final int timeout) {
    try {/*from   w w  w. ja  v a 2  s  .c  o  m*/
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port), timeout);
        socket.close();
        return true;
    }

    catch (ConnectException ce) {
        ce.printStackTrace();
        return false;
    }

    catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static boolean isPortOpen(final String ip, final int port, final int timeout) {
    try {// w  ww.  j  av  a2  s.  c o  m
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port), timeout);
        socket.close();
        return true;
    }

    catch (ConnectException ce) {
        //ce.printStackTrace();
        return false;
    }

    catch (Exception ex) {
        //ex.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static Socket getSocket(Context context, String proxyHost, int proxyPort) throws IOException {
    Socket sock = new Socket();

    sock.connect(new InetSocketAddress(proxyHost, proxyPort), 10000);

    return sock;/*from  w w w.ja  v a 2 s .com*/
}

From source file:guiTool.Helper.java

public static final boolean checkInternet() {

    try {//from   w  w  w  .ja  va  2s. com
        Socket socket = new Socket();
        InetSocketAddress add = new InetSocketAddress("www.google.com", 80);
        socket.connect(add, 3000);
        return true;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "The server or your internet connection could be down.",
                "connection test", JOptionPane.ERROR_MESSAGE);
        return false;
    }

}

From source file:example.springdata.cassandra.util.CassandraSocket.java

/**
 * @param host must not be {@literal null} or empty.
 * @param port/*from   w  w w  . j  ava 2s .c om*/
 * @return {@literal true} if the TCP port accepts a connection.
 */
public static boolean isConnectable(String host, int port) {

    Assert.hasText(host, "Host must not be null or empty!");

    try (Socket socket = new Socket()) {

        socket.setSoLinger(true, 0);
        socket.connect(new InetSocketAddress(host, port),
                (int) TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS));

        return true;

    } catch (Exception e) {
        return false;
    }
}

From source file:org.openbaton.nfvo.vnfm_reg.impl.register.RestRegister.java

private static boolean pingHost(String host, int port, int timeout) {
    try (Socket socket = new Socket()) {
        socket.connect(new InetSocketAddress(host, port), timeout);
        return true;
    } catch (IOException ignored) {
        return false; // Either timeout or unreachable or failed DNS lookup.
    }/*  w w w .  ja va  2s  . c om*/
}