- SSL Server Socket Example

This section provides a tutorial example on how to write a sample program to create a SSL server socket to listen for incoming SSL socket connection requests from client machines.

The following sample program,, shows you how to create a SSL server socket with a self-signed pair of keys:

 - Copyright (c) 2014,, All Rights Reserved.
public class SslReverseEchoer {
   public static void main(String[] args) {
      String ksName = "herong.jks";
      char ksPass[] = "HerongJKS".toCharArray();
      char ctPass[] = "My1stKey".toCharArray();
      try {
         KeyStore ks = KeyStore.getInstance("JKS");
         ks.load(new FileInputStream(ksName), ksPass);
         KeyManagerFactory kmf = 
         kmf.init(ks, ctPass);
         SSLContext sc = SSLContext.getInstance("TLS");
         sc.init(kmf.getKeyManagers(), null, null);
         SSLServerSocketFactory ssf = sc.getServerSocketFactory();
         SSLServerSocket s 
            = (SSLServerSocket) ssf.createServerSocket(8888);
         SSLSocket c = (SSLSocket) s.accept();
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
         BufferedReader r = new BufferedReader(new InputStreamReader(
         String m = "Welcome to SSL Reverse Echo Server."+
            " Please type in some words.";
         while ((m=r.readLine())!= null) {
            if (m.equals(".")) break;
            char[] a = m.toCharArray();
            int n = a.length;
            for (int i=0; i<n/2; i++) {
               char t = a[i];
               a[i] = a[n-1-i];
               a[n-i-1] = t;
      } catch (Exception e) {
   private static void printSocketInfo(SSLSocket s) {
      System.out.println("Socket class: "+s.getClass());
      System.out.println("   Remote address = "
      System.out.println("   Remote port = "+s.getPort());
      System.out.println("   Local socket address = "
      System.out.println("   Local address = "
      System.out.println("   Local port = "+s.getLocalPort());
      System.out.println("   Need client authentication = "
      SSLSession ss = s.getSession();
      System.out.println("   Cipher suite = "+ss.getCipherSuite());
      System.out.println("   Protocol = "+ss.getProtocol());
   private static void printServerSocketInfo(SSLServerSocket s) {
      System.out.println("Server socket class: "+s.getClass());
      System.out.println("   Socket address = "
      System.out.println("   Socket port = "
      System.out.println("   Need client authentication = "
      System.out.println("   Want client authentication = "
      System.out.println("   Use client mode = "

Of course, to run this program, you need to have the key store file, herong.jks, ready. It contains a self-signed pair of private and public keys. Read other chapters on Java key store files.

If you run this program with JDK 1.8, you will get:

Server socket class: class
   Socket address =
   Socket port = 8888
   Need client authentication = false
   Want client authentication = false
   Use client mode = false

Now my SSL socket server is ready and listening for new SSL socket connection requests.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

SSL Socket Communication Testing Programs

 SSL Socket Communication Test - SSL Server Socket Example - SSL Client Socket Example

 Making Self-Signed Certificates Trusted - Debugging SSL Socket Communication

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version