Search code examples
javasocketsserversocket

My java server not accepting multiple client requests in different Threads


Why is my Server program in Java not accepting anymore clients when I am running the client program more than once?

Here's my code for server.java:-

import java.net.*;
public class Server {
    public static void main(String []args)throws Exception
    {
        ServerSocket ss=new ServerSocket(9090);
        
        while (true)
        {
            System.out.println("Looking for connections");
            
            Socket soc=ss.accept();
            System.out.println("A new Client has joined");
            ClientHandler ch=new ClientHandler(soc);
            Thread t1=new Thread(ch);
            t1.start();
        }
    }
}

Here's my code for ClientHandler.java:-

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

public class ClientHandler implements Runnable {
    Socket soc;
    String username;
    BufferedReader br;
    PrintWriter out;
    static ArrayList<ClientHandler> clients=new ArrayList<>();
    ClientHandler(Socket soc)
    {
        this.soc=soc;
        try
        {
            this.br=new BufferedReader(new InputStreamReader(soc.getInputStream()));
            this.out=new PrintWriter(soc.getOutputStream(),true);
            this.username=br.readLine();
            broadcast(username + " has joined the group chat");
            clients.add(this);
        }
        catch(Exception e ){e.printStackTrace();}


    }
    public void run()
    {
        while (true)
        {
            try{
                String message=br.readLine();
                broadcast(message);
                }catch(Exception e ){e.printStackTrace();break;}

        }
        

    }
    void broadcast(String message)
    {
        for (ClientHandler c : clients)
        c.out.println(message);

    }
    
}

Here is my client.java

import java.net.*;
import java.util.*;
public class Client {
public static void main(String []args)throws Exception
{
    Socket soc=new Socket("localhost",9090);
    String username;
    Scanner sc=new Scanner(System.in);
    
    System.out.println("Please enter your username");
    username=sc.nextLine();
    ClientRead cr=new ClientRead(soc,username);
    ClientWrite cw=new ClientWrite(soc,username);
    Thread t1=new Thread(cr);
    Thread t2=new Thread(cw);
    t1.start();
    t2.start();
    sc.close();
   }

}

even when I am running more than 1 client it is just printing "A new client has joined" ONLY ONCE.

enter image description here

I was expecting it to accept multiple client requests and handle them in different threads and print in server's terminal, "A new Client has joined" same number of times as the number of times i run client.java


Solution

  • Your ClientHandler constructor already tries to read from the socket. If the client doesn't immediately sends a line (including line break!), the socket will keep waiting.

    Move this.username=br.readLine(); and everything after it to the run method, before the loop. That way all reading will be done in another thread.