Answer to Question #231491 in Java | JSP | JSF for Sozib

Question #231491
Design and implement a Java program to make a chat platform using synchronization between 3 threads.
1
Expert's answer
2021-08-31T01:51:33-0400
import java.io.*;
import java.util.*;
import java.net.*;

public class Server 
{
    static Vector<ClientHandler> arr = new Vector<>();
    static int i = 0;
  
    public static void main(String[] args) throws IOException 
    {
        ServerSocket server_socket = new ServerSocket(3456);
          
        Socket socket;
        while (true) 
        {
            socket = server_socket.accept();
  
            System.out.println("New client request received : " + socket);
              
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
              
            System.out.println("Creating a new handler for this client...");
  
            ClientHandler mtch = new ClientHandler(socket,"client " + i, dis, dos);
  
            Thread t = new Thread(mtch);
              
            System.out.println("Adding this client to active client list");
  
            arr.add(mtch);
  
            t.start();
            i++;
  
        }
    }
}
  
class ClientHandler implements Runnable 
{
    Scanner scn = new Scanner(System.in);
    private String name;
    final DataInputStream dis;
    final DataOutputStream dos;
    Socket socket;
    boolean isloggedin;
      
    public ClientHandler(Socket socket, String name,
                            DataInputStream dis, DataOutputStream dos) {
        this.dis = dis;
        this.dos = dos;
        this.name = name;
        this.socket = socket;
        this.isloggedin=true;
    }
  
    @Override
    public void run() {
  
        String received;
        while (true) 
        {
            try
            {
                received = dis.readUTF();
                  
                System.out.println(received);
                  
                if(received.equals("logout")){
                    this.isloggedin=false;
                    this.socket.close();
                    break;
                }
                
                StringTokenizer st = new StringTokenizer(received, "#");
                String MsgToSend = st.nextToken();
                String recipient = st.nextToken();
                for (ClientHandler mc : Server.ar) 
                {
                    if (mc.name.equals(recipient) && mc.isloggedin==true) 
                    {
                        mc.dos.writeUTF(this.name+" : "+MsgToSend);
                        break;
                    }
                }
            } catch (IOException e) {
                  
                e.printStackTrace();
            }
              
        }
        try
        {
            this.dis.close();
            this.dos.close();
              
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS