Skip to content

Program for Multi Client Chat Server

Program for Multi Client Chat Server

Code:

Server.java

import java.io.*;
import java.net.*;
import java.util.*;

public class Server{

    public static void main(String args[]) {
List<clientThread> t= new ArrayList<clientThread>();          
Socket clientSocket = null;
ServerSocket serverSocket = null;
int port_number=1111;

        try {
        serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
        {System.out.println(e);}
   
    while(true){
        try {
        clientSocket = serverSocket.accept();
clientThread th=new clientThread(clientSocket,t);
t.add(th);
th.start();

        }
        catch (IOException e) {
        System.out.println(e);}
    }
    }
}

class clientThread extends Thread{
   
    DataInputStream is = null;
    String line;
String destClient=””;
String name;
PrintStream os = null;
Socket clientSocket = null;      
List<clientThread> t;
String clientIdentity;
   
    public clientThread(Socket clientSocket, List<clientThread> t){
    this.clientSocket=clientSocket;
        this.t=t;
    }
   
public void selectDestClient() throws IOException
{
int h=0;
os.println(“nTotal Members online in Chat room : “+t.size());
        os.println(“nWith whom you want to chat ?”);
    if(t.size()>1)
    {
        for(int i=0; i<t.size(); i++)
        {
    if(t.get(i)!=this)
                                    {
            this.os.println((++h)+” ) – “+(t.get(i)).clientIdentity);
                                     }
if(t.indexOf(t.get(i))==(t.size()-1))
 {
os.println(“nPlease Enter the name from above list to chat : “);
wh:while(true)
   {
    destClient=is.readLine();
for(clientThread ct:t)
{
if(destClient.equalsIgnoreCase(ct.clientIdentity) && ct!=t)//!destClient.equalsIgnoreCase(clientIdentity))
{
os.println(“nYou are connected with “+destClient+”….Start enoying this chat service.nn_______________________________________________________________________n”);
return;
}
}
os.println(“nInvalid destination. — There is NO user with name “+destClient+”. nnPlease Re-enter : “);
continue;
}//wh:while

}
    }
    }else
    {
            this.os.println(“n- Currently no other client has logged in for Chatting.n- You are the  first one.Wait for some clients….”);
    }

}
    public void run()
    {
    try{
        is = new DataInputStream(clientSocket.getInputStream());
        os = new PrintStream(clientSocket.getOutputStream());
        os.println(“Enter your name.”);
        name = is.readLine();
                    clientIdentity=name;
        os.println(“nnWelcome “+name+” to this chatting Application.nnInstructions :n- To Sign-Out enter /quit in a new linen- To view List of Chat Members enter /view amd follow the instructions.nn”);
             for(int i=0; i<t.size(); i++)
        {
    if(t.get(i)!=this)
                                    {
  (t.get(i)).os.println(“*** A new user “+name+” entered the chat room ***nn”);
                                     }
}
selectDestClient();
        while (true) {
        line = is.readLine();

                if(line.startsWith(“/view”))
{
selectDestClient();
continue;
}

                if(line.startsWith(“/quit”)) break;

    if(t.size()>1)
    {
        for(int i=0; i<t.size(); i++)
           {
if((t.get(i))!=this)
{
        if((t.get(i)).clientIdentity.equalsIgnoreCase(destClient))
                                   {
                                       (t.get(i)).os.println(“nn<From “+this.clientIdentity+”> “+line);
                break;
            }

}
            }

    }

        }

    if(t.size()>1)
    {
        for(int i=0; i<t.size(); i++)
           {
        if(t.get(i)!=this)
                           (t.get(i)).os.println(“*** The user “+name+” left the chat room***” );

    }
}
        os.println(“*** Bye “+name+” ***”);

        t.remove(this);
        is.close();
        os.close();
        clientSocket.close();
    }
    catch(IOException e){};
    }
}

Client.java

import java.io.*;
import java.net.*;

public class Client implements Runnable{
    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;
   
    public static void main(String[] args) {
    int port_number=1111;
        String host=”localhost”;

    try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (Exception e) {
            System.err.println(“Exception occurred : “+e.getMessage());
        }

        if (clientSocket != null && os != null && is != null) {
            try {
                new Thread(new Client()).start();
       
        while (!closed) {
                    os.println(inputLine.readLine());
                }

        os.close();
        is.close();
        clientSocket.close();
            } catch (IOException e) {
                System.err.println(“IOException:  ” + e);
            }
        }
    }          
   
    public void run() {       
    String responseLine;
    try{
        while ((responseLine = is.readLine()) != null) {
    System.out.println(responseLine);
        if (responseLine.indexOf(“*** Bye”) != -1) break;
        }
            closed=true;
    } catch (IOException e) {
        System.err.println(“IOException:  ” + e);
    }
    }
}


Output:

1 thought on “Program for Multi Client Chat Server”

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!