Use SSLSocketFactory and SSLSocket to create an echo client - Java Network

Java examples for Network:SSL

Description

Use SSLSocketFactory and SSLSocket to create an echo client

Demo Code

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class Main {
    public static void main(String[] args) {
        try {//from ww  w .j a v  a  2 s  . com
            SSLSocketFactory sslSocketFactory = 
                    (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslSocket = (SSLSocket) 
                    sslSocketFactory.createSocket("localhost", 9999);

            InputStreamReader inputStreamReader = 
                    new InputStreamReader(System.in);
            BufferedReader bufferedReader = 
                    new BufferedReader(inputStreamReader);

            OutputStream outputStream = sslSocket.getOutputStream();
            OutputStreamWriter outputStreamWriter = 
                    new OutputStreamWriter(outputStream);
            BufferedWriter bufferedwriter = 
                    new BufferedWriter(outputStreamWriter);

            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                bufferedwriter.write(line + '\n');
                bufferedwriter.flush();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

Related Tutorials