英文:
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("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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论