- A Multi-Connection Socket Server

This section provides a tutorial example on how to write a network application,, that creates a server socket and listens for multiple remote socket connection requests. It creates a new execution thread each time, for each new connection.

ReverseEchoer runs perfectly if there is only one client program talking to it. If we run another copy of SocketClient, while a copy of SocketClient is running and connected to ReverseEchoer, the second copy of SocketClient will not be able establish a communication link with ReverseEchoer, because it is busy with the first copy of SocketClient.

To create a server application that can handle multiple connections, a new execution thread needs to be created for each connection. The following program, called ReverseEchoServer, offers the same function to client program. But it can handle multiple connections.

 - Copyright (c) 2014,, All Rights Reserved.
public class ReverseEchoServer implements Runnable {
   private Socket con = null;
   public static void main(String[] args) {
      try {
         ServerSocket s = new ServerSocket(8888);
         while (true) {
            Socket c = s.accept();
            ReverseEchoServer v = new ReverseEchoServer(c);
            Thread t = new Thread(v);
      } catch (IOException e) {
   public ReverseEchoServer(Socket c){
      con = c;
   public void run() { 
      try {
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
         BufferedReader r = new BufferedReader(new InputStreamReader(
         String m = "Welcome to 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 (IOException e) {
   private static void printSocketInfo(Socket s) {
      System.out.println("Remote address = "
      System.out.println("Remote port = "
      System.out.println("Local socket address = "
      System.out.println("Local address = "
      System.out.println("Local port = "
   private static void printServerSocketInfo(ServerSocket s) {
      System.out.println("Server socket address = "
      System.out.println("Server socket port = "

Here is the output displayed on the ReverseEchoServer console window, with two copies of SocketClient running:

Server socket address =
Server socket port = 8888
Remote address = /
Remote port = 1084
Local socket address = /
Local address = /
Local port = 8888
Remote address = /
Remote port = 1085
Local socket address = /
Local address = /
Local port = 8888

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

 What Is a Socket?

 Establishing a Socket Communication Link - A Simple Server Socket Application - A Simple Client Socket Application - A Multi-Connection Socket Server

 Binding Sockets to Specific Ports

 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 Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version