SimpleTLSClient.java (Código fuente)

import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;

public class SimpleTLSClient {
    public static void main(String[] args) {
	
        if (args.length < 2) {
            System.out.println("Usage: java SimpleTLSClient <ip_address> <port>");
            return;
        }

        String ipAddress = args[0];
        int port = Integer.parseInt(args[1]);

        try {

            // Create the SSL context
            SSLContext sslContext = SSLContext.getInstance("TLS");

            // Load the trust store
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(new FileInputStream("/home/z/test_java_tls/keystore.p12"), "tmztmz".toCharArray());

            // Create the trust manager factory
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(trustStore);

            // Initialize the SSL context with the trust managers
            sslContext.init(null, trustManagerFactory.getTrustManagers(), null);

            // Create the client socket
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
	    // Estos dos funcionan

            //SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket("localhost", 32345);
            //SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket("2a01:b5c0:1:2::200", 32345);

	    // este contra servidor smartos no funciona
            //SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket("2a01:b5c0:1:1:10:c60b:a495:aa58", 32345);
            SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(ipAddress, port);

            // Read the response
            BufferedReader in = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
            System.out.println("Server response: " + in.readLine());

            // Close the connection
            sslSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}