import java.io.*;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.*;

// java SSLClient 192.168.0.12 10002
public class SSLClient {

    public static void main(String[] args) throws Exception {
        String file = "msg";
        int msgLen = 0;
        //      byte buf[] = new byte[1000] ;
        byte buf[]=new byte[1000];
        FileOutputStream file_output=null;
        BufferedOutputStream data_out=null;
        try {
            file_output = new FileOutputStream ("/tmp/foo");
            data_out    = new BufferedOutputStream (file_output );
            // Wrap the FileInputStream with a DataInputStream
            FileInputStream file_input = new FileInputStream (file);
            BufferedInputStream data_in    = new BufferedInputStream (file_input );
            int len = data_in.read(buf);
            msgLen=len;
            Integer iLen = new Integer(len);
            System.out.println("Size read "+iLen);
        } catch (EOFException eof) {
        }
        SSLContext sc = SSLContext.getInstance ( "SSLv3" ) ; 
        sc.init (null,null,null) ; 
        sc.createSSLEngine();

        Socket socket=new Socket(args[0], Integer.parseInt(args[1]));
        SSLSocketFactory sslFact = (SSLSocketFactory)SSLSocketFactory.getDefault();
 
 			SSLSocket sslSocket = (SSLSocket) 
 				sslFact.createSocket(socket, 
 				socket.getInetAddress().getHostAddress(), 
 				socket.getPort(), true);

        sslSocket.setEnabledCipherSuites(sslSocket.getEnabledCipherSuites());

        sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols());
        sslSocket.setEnableSessionCreation(true);
        sslSocket.setSoTimeout(100000);
        // enable all the suites

        String[] supported = sslSocket.getSupportedCipherSuites();
        sslSocket.setEnabledCipherSuites(supported);
        sslSocket.setUseClientMode(true);
        sslSocket.startHandshake();


        BufferedInputStream bri = new BufferedInputStream(sslSocket.getInputStream());
        BufferedOutputStream bro = new BufferedOutputStream(sslSocket.getOutputStream());
        bro.write(buf,0,msgLen);
        bro.flush();
        int l = bri.read(buf,0,1000);
        data_out.write(buf,0,l);
        data_out.close();
        System.out.println("Read "+l+" bytes data is"+buf);

        sslSocket.close();
    }
}