SSL Client Socket

In this chapter you will learn:

  1. Creating an SSL Client Socket
  2. SSL HTTP client

Creating an SSL Client Socket

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/* j  ava 2  s. c  om*/
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class Main {
  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();
    out.close();
  }
}

SSL HTTP client

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
// j  ava 2  s.com
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class Main {
  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();
    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();

  }
}

Next chapter...

What you will learn in the next chapter:

  1. Creating an SSL Server Socket
  2. How to get supported cipher suites
Home » Java Tutorial » Socket

Socket

    Socket
    Socket creation
    Socket read
    HTTP client
    SMTP Client
    Cipher Socket
    Socket connection and thread

ServerSocket

    ServerSocket
    ServerSocket connection
    File server and client
    Thread based Server
    Time server
    SocketServer based zip server
    ServerSocketChannel
    Channel selector

SocketChannel

    SocketChannel Creation
    Read and write with SocketChannel
    SocketChannel based HTTP client
    SocketChannel Asynchronous

ServerSocketChannel

    ServerSocketChannel
    Channel selector

SSL

    SSL Client Socket
    SSL Server Socket

UDP

    DatagramSocket
    DatagramChannel
    Multicast Group

Cookie

    Cookies