gpt4 book ai didi

java - 多线程似乎不起作用

转载 作者:行者123 更新时间:2023-12-01 13:12:19 25 4
gpt4 key购买 nike

我有 2 个类,一个服务器和一个客户端。服务器使用多个线程来接受许多客户端。所以 x 个客户端可以加入同一台服务器。然而,在尝试从客户端方法识别线程时,我似乎发现它没有创建多个线程,因为所有客户端的 ID 都相同。我的代码如下:

服务器:

public class Server
{
ServerSocket serverSocket;
int portNumber;
public static volatile String userInput;
public volatile int noOfClients = 0;

public static void main(String[] args)
{
Server s = new Server();
s.startup();
}


/**
* Start the server on the user picked port
*/
public void startup()
{
try
{
System.out.println("Enter a port");
Scanner dif = new Scanner(System.in);
portNumber = Integer.parseInt(dif.nextLine());
dif.close();

serverSocket = new ServerSocket(portNumber);
newThread();
}
catch (IOException e) {
System.out.println("Error");
System.exit(0);
}
}


public void newThread()
{
Thread thread =new Thread()
{

public void run()
{
while(true) {
try {

accept();
} catch (Exception e) {
System.out.println("Error");
}
}
}
};
thread.start();
}

public void accept()
{
try
{
Socket clientSocket = serverSocket.accept();
new Thread(new ClientSocket(clientSocket)).start();
System.out.println("A new client has just connected.");
noOfClients++;

} catch(IOException e)
{
System.out.println("Error");
System.exit(0);
}
}


class ClientSocket implements Runnable {
Socket clientSocket;

public ClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
}

public void run() {
{
try
{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

while (true)
{
userInput = in.readLine();
}

} catch (IOException e)
{
System.out.println("Error");
}
}
}
}
}

客户:

public class Client
{
Socket clientSocket;
public static int threadName;


public static void main(String[] args) throws IOException {
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);


try {
Socket serverSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

Thread thread = Thread.currentThread();
System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");
String userInput;

while ((userInput = stdIn.readLine()) != null)
{

out.println(userInput);
System.out.println("Server: " + userInput);
}

} catch(UnknownHostException e) {
System.out.println("error in host");
} catch(IOException e) {
System.out.println("error in IO");
}


}
}

当运行两个单独的客户端时,

System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");

一行代码打印出相同的结果。我该如何修复它,以便每个新的客户端连接都在自己的唯一线程内启动。那么 2 个客户端总共有 2 个线程?谢谢:)

最佳答案

首先,您要检查客户端的线程 ID,它们彼此独立,因此这是行不通的。

但是,使用线程 ID 并不是识别客户端的好方法。相反,为什么不记录客户端数量,然后当新客户端加入时,增加该数字并将该数字作为 id 提供给客户端对象。

关于java - 多线程似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com