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 4 of 6

  1. boolean getUseClientMode()
  2. void setUseClientMode(boolean flag)


A simple example

To make this toolkit tutorial clearer, I've included the source code for a simple server and a compatible client below. It's a secure variation on the typical echo application that many introductory networking texts provide.

The server, shown below, uses JSSE to create a secure server socket. It listens on the server socket for connections from secure clients. When running the server, you must specify the keystore to use. The keystore contains the server's certificate. I have created a simple keystore that contains a single certificate. (See Resources to download the certificate.)

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
public
class EchoServer
{
  public
  static
  void
  main(String [] arstring)
  {
    try
    {
      SSLServerSocketFactory sslserversocketfactory =
        (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
      SSLServerSocket sslserversocket =
        (SSLServerSocket)sslserversocketfactory.createServerSocket(9999);
      SSLSocket sslsocket = (SSLSocket)sslserversocket.accept();
      InputStream inputstream = sslsocket.getInputStream();
      InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
      BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
      String string = null;
      while ((string = bufferedreader.readLine()) != null)
      {
        System.out.println(string);
        System.out.flush();
      }
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }
  }
}


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

  • Print
  • Feedback

Resources