Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Build secure network applications with SSL and the JSSE API

Get started with SSL and JSSE using these two simple apps

  • Print
  • Feedback

Page 5 of 6

  java -Djavax.net.ssl.keyStore=foobar -Djavax.net.ssl.keyStorePassword=foobar EchoServer


The client, shown below, uses JSSE to securely connect to the server. When running the client, you must specify the truststore to use, which contains the list of trusted certificates. I have created a simple truststore that contains a single certificate. (See Resources to download the certificate.)

import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public
class EchoClient
{
  public
  static
  void
  main(String [] arstring)
  {
    try
    {
      SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
      SSLSocket sslsocket = (SSLSocket)sslsocketfactory.createSocket("localhost", 9999);
      InputStream inputstream = System.in;
      InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
      BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
      OutputStream outputstream = sslsocket.getOutputStream();
      OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
      BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
      String string = null;
      while ((string = bufferedreader.readLine()) != null)
      {
        bufferedwriter.write(string + '\n');
        bufferedwriter.flush();
      }
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }
  }
}


Use the following command to start the client (foobar is both the name of the truststore file and its password):

  • Print
  • Feedback

Resources