Java Socket with SSL

Here's 4 Java sample code.
Notice that the Connect class is the same for non-SSL and SSL server.

Socket Client

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

public class
Client
{

    public static
void main(String[] args)
    {
        try
        {
           
//
           
Socket soc = new Socket("10.1.165.111", 51004);

           
// Input/Output stream
           
ObjectOutputStream oos = new ObjectOutputStream(soc
                   
.getOutputStream());
           
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());

           
// Write to output stream
           
oos.writeObject(args[0]);

           
// Read from input stream
           
String str = (String) ois.readObject();

           
//
           
System.out.println("string to uppercase : " + str);

           
// Close
           
oos.close();
           
ois.close();
           
soc.close();
        }
        catch (
Exception e)
        {
           
e.printStackTrace();
        }
    }
}

Socket Server

import java.net.*;

public class
Server
{

    public static
void main(String[] args)
    {
        try
        {
           
ServerSocket ssoc = new ServerSocket(51004);

            while (
true)
            {
                try
                {
                   
System.out.println("waiting for connection from client");

                   
Socket soc = ssoc.accept();
                   
System.out.println("receive client!");

                    new
Connect(soc);
                }
                catch (
Exception e)
                {
                   
e.printStackTrace();
                }
            }
        }
        catch (
Exception e)
        {
           
e.printStackTrace();
        }
    }
}

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

public class
Connect extends Thread
{

    private
Socket soc = null;

    public
Connect(Socket soc)
    {
       
this.soc = soc;

       
// Start Thread
       
this.start();
    }

    public
void run()
    {
        try
        {
           
// Input/Output stream
           
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
           
ObjectOutputStream oos = new ObjectOutputStream(soc
                   
.getOutputStream());

           
// Read a string from the stream
           
String str = (String) ois.readObject();

           
// Convert to upper case
           
str = str.toUpperCase();

           
// Write to the output stream
           
oos.writeObject(str);

           
// Close
           
ois.close();
           
oos.close();
           
soc.close();
        }
        catch (
Exception e)
        {
           
e.printStackTrace();
        }
    }
}

SSL Client

import java.io.*;
import java.security.KeyStore;

import javax.net.ssl.*;

public class
SSLClient
{

    public static
void main(String[] args)
    {
        try
        {

           
// TrustStore
           
KeyStore trust_store = KeyStore.getInstance("JKS");
           
char[] trust_pass = "changeit".toCharArray();
           
trust_store.load(new FileInputStream("C:\\Temp\\clientTrust"),
                   
trust_pass);

           
TrustManagerFactory tmf = TrustManagerFactory
                   
.getInstance("SunX509");
           
tmf.init(trust_store);

           
// Create Socket
           
SSLContext context = SSLContext.getInstance("TLS");
           
context.init(null, tmf.getTrustManagers(), null);
           
SSLSocketFactory sf = context.getSocketFactory();
           
SSLSocket soc = (SSLSocket) sf.createSocket("10.10.7.60", 51004);
           
soc.startHandshake();

           
// Input/Output stream
           
ObjectOutputStream oos = new ObjectOutputStream(soc
                   
.getOutputStream());
           
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());

           
//
           
oos.writeObject(args[0]);

           
//
           
String str = (String) ois.readObject();

           
//
           
System.out.println("string to uppercase : " + str);

           
// Close
           
oos.close();
           
ois.close();
           
soc.close();
        }
        catch (
Exception e)
        {
           
e.printStackTrace();
        }
    }
}

SSL Server

import java.io.FileInputStream;
import java.net.*;
import java.security.KeyStore;

import javax.net.ssl.*;

public class
Server
{

    public static
void main(String[] args)
    {
        try
        {

           
// KeyStore
           
KeyStore key_store = KeyStore.getInstance("JKS");
           
char[] key_pass = "changeit".toCharArray();
           
key_store
                   
.load(
                            new
FileInputStream(
                                   
"C:\\Temp\\keystore.key"),
                           
key_pass);

           
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
           
kmf.init(key_store, key_pass);

           
// Create Server Socket
           
SSLContext context = SSLContext.getInstance("TLS");
           
context.init(kmf.getKeyManagers(), null, null);
           
SSLServerSocketFactory ssf = context.getServerSocketFactory();
           
SSLServerSocket ssoc = (SSLServerSocket) ssf
                   
.createServerSocket(51004);

            while (
true)
            {
                try
                {
                   
System.out.println("waiting for connection from client");
                   
// Wait for the client to connect
                   
Socket soc = ssoc.accept();
                   
System.out.println("receive client!");

                   
// Spawn Connect instance and let it handle the connection.
                   
new Connect(soc);
                }
                catch (
Exception e)
                {
                   
e.printStackTrace();
                }
            }
        }
        catch (
Exception e)
        {
           
e.printStackTrace();
        }
    }
}