Blog Archive

Java Multithreaded Socket server example code

In this example we have created a server that listens on port 11111 and accepts any number of incoming request from a client. For each client connection it starts a child thread to process the request independent of any other incoming requests.
package com.as400samplecode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class MultiThreadedSocketServer {

    ServerSocket myServerSocket;
    boolean ServerOn = true;


    public MultiThreadedSocketServer() 
    { 
        try 
        { 
            myServerSocket = new ServerSocket(11111); 
        } 
        catch(IOException ioe) 
        { 
            System.out.println("Could not create server socket on port 11111. Quitting."); 
            System.exit(-1); 
        } 




        Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        System.out.println("It is now : " + formatter.format(now.getTime()));




        // Successfully created Server Socket. Now wait for connections. 
        while(ServerOn) 
        {                        
            try 
            { 
                // Accept incoming connections. 
                Socket clientSocket = myServerSocket.accept(); 

                // accept() will block until a client connects to the server. 
                // If execution reaches this point, then it means that a client 
                // socket has been accepted. 

                // For each client, we will start a service thread to 
                // service the client requests. This is to demonstrate a 
                // Multi-Threaded server. Starting a thread also lets our 
                // MultiThreadedSocketServer accept multiple connections simultaneously. 

                // Start a Service thread 

                ClientServiceThread cliThread = new ClientServiceThread(clientSocket);
                cliThread.start(); 

            } 
            catch(IOException ioe) 
            { 
                System.out.println("Exception encountered on accept. Ignoring. Stack Trace :"); 
                ioe.printStackTrace(); 
            } 

        }

        try 
        { 
            myServerSocket.close(); 
            System.out.println("Server Stopped"); 
        } 
        catch(Exception ioe) 
        { 
            System.out.println("Problem stopping server socket"); 
            System.exit(-1); 
        } 



    } 

    public static void main (String[] args) 
    { 
        new MultiThreadedSocketServer();        
    } 


    class ClientServiceThread extends Thread 
    { 
        Socket myClientSocket;
        boolean m_bRunThread = true; 

        public ClientServiceThread() 
        { 
            super(); 
        } 

        ClientServiceThread(Socket s) 
        { 
            myClientSocket = s; 

        } 

        public void run() 
        {            
            // Obtain the input stream and the output stream for the socket 
            // A good practice is to encapsulate them with a BufferedReader 
            // and a PrintWriter as shown below. 
            BufferedReader in = null; 
            PrintWriter out = null; 

            // Print out details of this connection 
            System.out.println("Accepted Client Address - " + myClientSocket.getInetAddress().getHostName()); 

            try 
            {                                
                in = new BufferedReader(new InputStreamReader(myClientSocket.getInputStream())); 
                out = new PrintWriter(new OutputStreamWriter(myClientSocket.getOutputStream())); 

                // At this point, we can read for input and reply with appropriate output. 

                // Run in a loop until m_bRunThread is set to false 
                while(m_bRunThread) 
                {                    
                    // read incoming stream 
                    String clientCommand = in.readLine(); 
                    System.out.println("Client Says :" + clientCommand);

                    if(!ServerOn) 
                    { 
                        // Special command. Quit this thread 
                        System.out.print("Server has already stopped"); 
                        out.println("Server has already stopped"); 
                        out.flush(); 
                        m_bRunThread = false;   

                    } 

                    if(clientCommand.equalsIgnoreCase("quit")) { 
                        // Special command. Quit this thread 
                        m_bRunThread = false;   
                        System.out.print("Stopping client thread for client : "); 
                    } else if(clientCommand.equalsIgnoreCase("end")) { 
                        // Special command. Quit this thread and Stop the Server
                        m_bRunThread = false;   
                        System.out.print("Stopping client thread for client : "); 
                        ServerOn = false;
                    } else {
                            // Process it 
                            out.println("Server Says : " + clientCommand); 
                            out.flush(); 
                    }
                } 
            } 
            catch(Exception e) 
            { 
                e.printStackTrace(); 
            } 
            finally 
            { 
                // Clean up 
                try 
                {                    
                    in.close(); 
                    out.close(); 
                    myClientSocket.close(); 
                    System.out.println("...Stopped"); 
                } 
                catch(IOException ioe) 
                { 
                    ioe.printStackTrace(); 
                } 
            } 
        } 


    } 
}

13 comments :

  1. This article has been the best one I've found. You're just great :)

    ReplyDelete
  2. Can you post your client code as well?

    ReplyDelete
    Replies
    1. http://www.mysamplecode.com/2011/12/java-socket-client-example-connect-to.html

      Delete
  3. You really shouldn't extend Thread, you should be extending Runnable.

    ReplyDelete
    Replies
    1. Ooops, meant implementing Runnable

      Delete
    2. You know it won't make a difference right? Thread implments Runnable, so implemmenting Runnable or extending Thread is the exact same thing

      Delete
    3. Implementing Runnable and extending Thread are not the exact same thing. If you extend Thread in a class A, then while creating another class B which needs to extend Thread, you can just extend A which is logically wrong.

      Delete
  4. What happens when the server got disconnected? Do you have any idea how you could make one client connected to be the new server?

    ReplyDelete
  5. Do you know how we can easly limit the number of thread ?

    ReplyDelete
  6. Thanks for your useful post.
    I also refer very helpful and useful blogs about Android Multithreading Example.
    http://www.mindstick.com/blog/10966/Android%20Multithreading%20Example
    http://developer.android.com/training/multiple-threads/index.html

    ReplyDelete
  7. Thank you alot, its a nice base server example and its without mistakes and working as it is. Which is not so common. Ill pray for you!

    ReplyDelete