Example usage for javax.net SocketFactory createSocket

List of usage examples for javax.net SocketFactory createSocket

Introduction

In this page you can find the example usage for javax.net SocketFactory createSocket.

Prototype

public abstract Socket createSocket(InetAddress host, int port) throws IOException;

Source Link

Document

Creates a socket and connects it to the specified port number at the specified address.

Usage

From source file:SSLSimpleClient.java

public static void main(String[] args) throws Exception {
    SocketFactory sf = SSLSocketFactory.getDefault();
    Socket s = sf.createSocket(args[0], Integer.parseInt(args[1]));

    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    PrintWriter pw = new PrintWriter(s.getOutputStream());
    pw.println("from java2s.");
    pw.flush();/*w w w. ja  v a  2 s .  co m*/
    System.out.println(br.readLine());
    s.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int port = 443;
    String hostname = "hostname";
    SocketFactory socketFactory = SSLSocketFactory.getDefault();
    Socket socket = socketFactory.createSocket(hostname, port);

    InputStream in = socket.getInputStream();
    OutputStream out = socket.getOutputStream();

    // Read from in and write to out...

    in.close();//from w  w w  . j av  a2 s.c o m
    out.close();
}

From source file:SecureClient.java

public static void main(String[] args) throws Exception {
    String host = "127.0.0.1";

    SocketFactory sf = SSLSocketFactory.getDefault();
    SSLSocket sock = (SSLSocket) sf.createSocket(host, PORT);
    System.out.println("Server connected");

    InputStream rawIn = sock.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(rawIn));
    System.out.println(in.readLine());
    sock.close();/*from w  w w.j  a  v  a2 s  .  co  m*/
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SocketFactory factory = SSLSocketFactory.getDefault();
    Socket socket = factory.createSocket("127.0.0.1", 8080);

    OutputStream outputStream = socket.getOutputStream();
    PrintWriter out = new PrintWriter(outputStream);
    out.print("GET / HTTP/1.0\r\n\r\n");
    out.flush();//from w w w  .  j  a va2  s .c  om
    InputStream inputStream = socket.getInputStream();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader in = new BufferedReader(inputStreamReader);

    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    out.close();
    in.close();
    socket.close();

}

From source file:com.fujitsu.dc.test.utils.Http.java

static Socket createSocket(URL url) throws IOException, KeyStoreException, NoSuchAlgorithmException,
        CertificateException, KeyManagementException {
    String host = url.getHost();//from   w  ww . ja  v a 2  s  .c om
    int port = url.getPort();
    String proto = url.getProtocol();
    if (port < 0) {
        if ("https".equals(proto)) {
            port = PORT_HTTPS;
        }
        if ("http".equals(proto)) {
            port = PORT_HTTP;
        }
    }
    log.debug("sock: " + host + ":" + port);
    log.debug("proto: " + proto);
    // HTTPS?????????????SSLSocket???
    if ("https".equals(proto)) {
        KeyManager[] km = null;
        TrustManager[] tm = { new javax.net.ssl.X509TrustManager() {
            public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {
                log.debug("Insecure SSLSocket Impl for Testing: NOP at X509TrustManager#checkClientTrusted");
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {
                log.debug("Insecure SSLSocket Impl for Testing: NOP at X509TrustManager#checkServerTrusted");
            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(km, tm, new SecureRandom());
        SocketFactory sf = sslContext.getSocketFactory();
        return (SSLSocket) sf.createSocket(host, port);
    }
    // HTTPS????????
    return new Socket(host, port);
}

From source file:SocketFetcher.java

/**
 * Create a socket with the given local address and connected to the given
 * host and port. Use the specified connection timeout. If a socket factory is
 * specified, use it. Otherwise, use the SSLSocketFactory if useSSL is true.
 *//* w w w  .  j  a v a 2 s  .c o m*/
private static Socket createSocket(InetAddress localaddr, int localport, String host, int port, int cto,
        SocketFactory sf, boolean useSSL) throws IOException {
    Socket socket;

    if (sf != null)
        socket = sf.createSocket(host, port);
    else if (useSSL)
        socket = SSLSocketFactory.getDefault().createSocket(host, port);
    else
        socket = new Socket(host, port);
    /*
     * if (localaddr != null) socket.bind(new InetAddress(localaddr,
     * localport)); if (cto >= 0) socket.connect(new InetSocketAddress(host,
     * port), cto); else socket.connect(new InetSocketAddress(host, port));
     */
    return socket;
}

From source file:pl.otros.logview.gui.actions.ConnectToSocketHubAppenderActionTest.java

@Test
public void tryToConnect() throws IOException {
    OtrosApplication otrosApplication = new OtrosApplication();
    ConnectToSocketHubAppenderAction action = new ConnectToSocketHubAppenderAction(otrosApplication);
    DataConfiguration dc = new DataConfiguration(new BaseConfiguration());
    String hostAndPort = "abc:50";
    SocketFactory socketFactory = mock(SocketFactory.class);
    Socket mockSocket = mock(Socket.class);
    when(socketFactory.createSocket("abc", 50)).thenReturn(mockSocket);

    Socket socket = action.tryToConnectToSocket(dc, hostAndPort, socketFactory);

    assertEquals(mockSocket, socket);//from   w w w .ja  v  a 2  s  .com
    assertEquals(1, dc.getList(ConfKeys.SOCKET_HUB_APPENDER_ADDRESSES).size());
    assertEquals("abc:50", dc.getList(ConfKeys.SOCKET_HUB_APPENDER_ADDRESSES).get(0));
}

From source file:pl.otros.logview.gui.actions.ConnectToSocketHubAppenderActionTest.java

@Test
public void tryToConnectFail() throws IOException {
    OtrosApplication otrosApplication = new OtrosApplication();
    ConnectToSocketHubAppenderAction action = new ConnectToSocketHubAppenderAction(otrosApplication);
    DataConfiguration dc = new DataConfiguration(new BaseConfiguration());
    String hostAndPort = "abc:50";
    SocketFactory socketFactory = mock(SocketFactory.class);
    Socket mockSocket = mock(Socket.class);
    when(socketFactory.createSocket("abc", 50)).thenThrow(new UnknownHostException());

    try {//w w w . ja  va  2  s. c om
        action.tryToConnectToSocket(dc, hostAndPort, socketFactory);
        Assert.fail();
    } catch (UnknownHostException e) {
        //success
    }

    assertEquals(0, dc.getList(ConfKeys.SOCKET_HUB_APPENDER_ADDRESSES).size());
}

From source file:com.cloupia.feature.nimble.http.MySSLSocketFactory.java

public Socket createSocket(String host, int port) throws IOException, UnknownHostException {

    TrustManager[] trustAllCerts = getTrustManager();

    try {//from  ww  w .j a v  a  2  s.  c  o m

        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        SocketFactory socketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();

        return socketFactory.createSocket(host, port);

    }

    catch (Exception ex) {

        throw new UnknownHostException("Problems to connect " + host + ex.toString());

    }

}

From source file:com.cloupia.feature.nimble.http.MySSLSocketFactory.java

public Socket createSocket(Socket socket, String host, int port, boolean flag)
        throws IOException, UnknownHostException {

    TrustManager[] trustAllCerts = getTrustManager();

    try {/*w  w  w.  ja va  2 s.  c  o  m*/

        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        SocketFactory socketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();

        return socketFactory.createSocket(host, port);

    }

    catch (Exception ex) {

        throw new UnknownHostException("Problems to connect " + host + ex.toString());

    }

}