Java program to create Instant Messenger application for communication between multiple clients

INSTANT MESSENGER
 
AIM
          To write a java program for creating Instant Messenger application for communication between multiple clients.


ALGORITHM

Step 1: Start the process.

Step 2: Create the client module as  class Client by extending thread
   class.

Step 3: Create the Server module as class Server for accepting
   connection from clients.

Step 4: Create ClientThread class in Server module to make 10 clients
   to communuicate.

Step 5: Start the Server.

Step 6: After Server is ready, then run the clients to communicate with
  each other.

Step 6: Terminate the clients and Server respectively.

Step 7: Stop the process.


Source Code

Client.java

import java.io.*;
import java.net.*;
public class Client implements Runnable
{
            static Socket socket=null;
            static PrintStream output;
            static BufferedReader input=null;
            static BufferedReader userip=null;
            static boolean flag=false;
            public static void main(String[] args)
            {
                        int port=1234;
                               String host="localhost";
                        try
                        {
                                    socket=new Socket(host,port);
                                    userip=new BufferedReader(new InputStreamReader(System.in));
                                    output=new PrintStream(socket.getOutputStream());
                                    input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        }
                        catch(Exception e)
                        {
                                    System.err.println("Unknown host"+host);
                        }
                        if(socket!=null)
                        {
                                    try
                                    {
                                                new Thread(new Client()).start();
                                                while(!flag)
                                                {
                                                output.println(userip.readLine());
                                                }
                                    output.close();
                                    input.close();
                                    socket.close();
                        }
                        catch(Exception e1)
                        {
                                    System.err.println("IOException"+e1);
                        }
            }
}
public void run()
{
String msg;
try
{
while((msg=input.readLine())!=null)
System.out.println(msg);
flag=true;
}
catch(IOException e)
{
System.err.println("IOException" + e);
}
}
}


Server.java

import java.io.*;
import java.net.*;
public class Server
{
      static ServerSocket server=null;
      static Socket socket=null;
      static ClientThread th[]=new ClientThread[10];
      public static void main(String args[])
      {
            int port=1234;
            System.out.println("Server started...");
            System.out.println("  [Press Ctrl C to terminate ]");
            try
            {
                  server=new ServerSocket (port);
            }
            catch(IOException e)
            {
                  System.out.println("Exception for Input/Output");
            }
            while(true)
            {
                  try
                  {
                        socket=server.accept();
                        for(int i=0;i<=9;i++)
                        {
                              if(th[i]==null)
                              {
                                    (th[i]=new ClientThread(socket,th)).start();
                                    break;
                              }
                        }
                        }
                        catch(IOException e)
                        {
                              System.out.println("Exception for Input/Output");
                        }
                  }
            }
      }

      class ClientThread extends Thread
      {
            BufferedReader input=null;
            PrintStream output=null;
            Socket socket=null;
            ClientThread th[];
            public ClientThread(Socket socket,ClientThread[] th)
            {
                  this.socket=socket;
                  this.th=th;
            }
            public void run()
            {
                  String msg;
                  String username;
                  try
                  {
                        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        output = new PrintStream(socket.getOutputStream());
                        output.println("What is your Name?Enter it-");
                        username = input.readLine();
                        output.println(username + ":Welcome to chat room.");
                        output.println("To leave chat room type $$");
                        for (int i = 0; i <= 9; i++)
                              if (th[i] != null && th[i] != this)
                                    th[i].output.println("------------A new user arrived in chat Room:" + username);
                              while (true)
                              {
                                    msg = input.readLine();
                                    if (msg.startsWith("$$"))
                                          break;
                                    for (int i = 0; i <= 9; i++)
                                          if (th[i] != null)
                                                th[i].output.println("<" + username + ">" + msg);
                              }
                              for (int k = 0; k <= 9; k++)
                                    if (th[k] != null && th[k] != this)
                                          th[k].output.println("------A user Leaving chat Room:" + username + "--------");
                              output.println("Press Ctrl C to return to prompt---");
                              for (int j = 0; j <= 9; j++)
                                    if (th[j] == this)
                                          th[j] = null;
                              input.close();
                              output.close();
                              socket.close();
                  }
                  catch (IOException e)
                  {
                        System.out.println("Exception for Input/Output");
                  }
                  }
            }

3 comments:

Schumy said...

Hi. I have tested out the code and it only works with local IPs. It seems that it doesn't recognize/doesn't want to connect with normal IPs. How can I make it work with normal IPs(not locally)?

Anonymous said...

Initialise host with the IP address (in this code change the "localhost" to the IP address) to connect it to normal IP in Client.java

Anonymous said...

Nice. Helped me a lot.

Post a Comment