Example usage for java.net Socket connect

List of usage examples for java.net Socket connect

Introduction

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

Prototype

public void connect(SocketAddress endpoint, int timeout) throws IOException 

Source Link

Document

Connects this socket to the server with a specified timeout value.

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();

    int timeoutMs = 2000;
    sock.connect(sockaddr, timeoutMs);
}

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 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   ww  w  . j a va  2  s .c om
    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:org.jboss.test.kerberos.KerberosSetup.java

/**
 * //  w  w  w .java2s . c  o  m
 * @param args
 */
public static void main(String[] args) {
    try {
        if (args.length == 1 && STOP_CMD.equals(args[0])) {
            System.out.println("Sending STOP command to Kerberos controll process.");
            SocketAddress sockaddr = new InetSocketAddress(InetAddress.getLocalHost(), SERVER_PORT);
            // Create an unbound socket
            Socket sock = new Socket();
            sock.connect(sockaddr, SOCKET_TIMEOUT);
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
            wr.write(STOP_CMD);
            wr.close();
            sock.close();
        } else {
            System.out.println("Starting Kerberos controll process.");
            KerberosSetup ns = new KerberosSetup();
            ns.startKDC(args);
            ns.waitForStop();
            ns.stopKDC();
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

}

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;// w w w .j a  v  a 2 s .c  o  m
}

From source file:Main.java

public static boolean isPortOpen(final String ip, final int port, final int timeout) {
    try {//from   ww  w.jav  a  2  s  .c om
        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 w w .  java  2s  .  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:org.huahinframework.emanager.util.StopUtils.java

/**
 * @param port/*w w  w .j a va 2 s.  com*/
 * @param msg
 */
public static void stop(int port, String msg) {
    try {
        InetSocketAddress socketAddress = new InetSocketAddress("localhost", port);
        Socket socket = new Socket();
        socket.connect(socketAddress, 10000);

        InetAddress inadr = socket.getInetAddress();
        if (inadr == null) {
            log.error("connect error: " + "localhost:" + port);
            return;
        }

        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.println(msg);

        writer.close();
        socket.close();
    } catch (Exception e) {
        log.error(e);
        e.printStackTrace();
    }
}

From source file:com.mirth.connect.connectors.tcp.SocketUtil.java

public static void connectSocket(Socket socket, String host, int port, int timeout)
        throws UnknownHostException, IOException {
    socket.connect(new InetSocketAddress(InetAddress.getByName(TcpUtil.getFixedHost(host)), port), timeout);
}

From source file:guiTool.Helper.java

public static final boolean checkInternet() {

    try {//from   w  w w .  j  av a2 s. c  o  m
        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;
    }

}