我的Java服务器不接受不同线程中的多个客户端请求。

huangapple go评论89阅读模式
英文:

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.

英文:

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(&quot;Looking for connections&quot;);
            
            Socket soc=ss.accept();
            System.out.println(&quot;A new Client has joined&quot;);
            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&lt;ClientHandler&gt; clients=new ArrayList&lt;&gt;();
    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 + &quot; has joined the group chat&quot;);
            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(&quot;localhost&quot;,9090);
    String username;
    Scanner sc=new Scanner(System.in);
    
    System.out.println(&quot;Please enter your username&quot;);
    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.

我的Java服务器不接受不同线程中的多个客户端请求。

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

答案1

得分: 3

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

this.username=br.readLine();以及之后的所有内容移到run方法,在循环之前。这样,所有的读取操作将在另一个线程中完成。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月9日 22:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定