Answer to Question #231892 in Java | JSP | JSF for saad

Question #231892

1) Design and implement a Java program to make a chat platform using synchronization between 3 threads.

2) Design a flowchart of your design

3) Implement your your design

4) Put your code and screenshot output in your pdf file.


1
Expert's answer
2021-09-01T23:57:47-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
    {
        @SuppressWarnings("resource")
        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++;
 
        }
    }
}
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.StringTokenizer;

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.arr)
                {
                    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