gpt4 book ai didi

java - Java中的多客户端服务器,也许线程运行不正常或者我不知道,无法找出原因

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:41 24 4
gpt4 key购买 nike

我正在用 java 编写一个服务器,每秒向客户端广播 Date() 函数。问题是它只适用于一个客户端,但当我开始对多客户端支持进行修改时,它仅广播 Date() 一次,然后停止,就好像该函数仅被调用一次一样。我找不到我做错了什么,所以我只会粘贴代码,希望有人会发现错误。我在网上搜索,但结果却比开始时更加困惑。对于客户端程序,我使用适用于 Windows 的tellnet 终端应用程序。

public class Server
{
private ServerSocket SERVER;
private int PORT;
private Socket CLIENT;

public Server()
{
PORT = 8818;
try
{
SERVER = new ServerSocket(PORT);
System.out.println("Server started on port: " + PORT);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}

public void On() throws IOException
{
while(true)
{
CLIENT = SERVER.accept();
new ClientHandler(CLIENT).start();
}
}
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class ClientHandler extends Thread
{
private Socket CLIENT;
private OutputStream out;
public ClientHandler(Socket CLIENT)
{
System.out.println("Accepted Connection from: " + CLIENT.getInetAddress());
this.CLIENT = CLIENT;
}

public void run()
{
try
{
out = CLIENT.getOutputStream();
out.write(("Time now is: " + new Date() + "\n").getBytes());
sleep(1000);
}
catch(Exception e)
{
System.out.println(CLIENT.getInetAddress() + " has left the session");
try
{
out.close();
CLIENT.close();
}
catch(IOException j)
{
System.out.println("Unexpected Error");
System.exit(-1);
}
}
}
}

最佳答案

您的修改几乎有效 - 下面是一个运行版本,仅对您的代码进行了少量修改。

您的修改的一部分无意中删除了 run 函数中的 while 循环,这意味着 Date() 函数实际上只被调用一次。要看到这一点,请删除 run() 中的 while 循环,并在打印日期后(在 telnet 窗口中)显示消息“Done with the run function”。已打印。

我为每个客户端添加了一个标识符,上面印有日期。静态 cnt 类字段确保每个客户端都有不同的 id。

我使用以下命令在单独的命令提示符终端中启动了单独的客户端telnet localhost 8818 以便它们同时运行。底部是第三个客户端的输出。

我确实将代码切换为驼峰命名法(以小写字母开头并将每个新单词大写),因为所有大写字母通常都保留用于常量,并且更改使代码更易于阅读。

public class Server
{
private ServerSocket server;
private int port;
private Socket client;

public Server()
{
port = 8818;
try
{
server = new ServerSocket(port);
System.out.println("Server started on port: " + port);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}

public void on() throws IOException
{
while(true)
{
client = server.accept();
new ClientHandler(client).start();
}
}
}
public class ClientHandler extends Thread {
private Socket client;
private OutputStream out;
private int id;
private static int cnt=0;

public ClientHandler(Socket client) {
System.out.println("Accepted Connection from: " + client.getInetAddress());
this.client = client;
id=cnt;
cnt++;
}

public void run() {
try {
out = client.getOutputStream();
while (true) {
out.write(("Client " + id + ": Time now is: " + new Date() + "\n").getBytes());
sleep(1000);
}

} catch (Exception e) {
System.out.println(client.getInetAddress() + " has left the session");
try {
out.close();
client.close();
} catch (IOException j) {
System.out.println("Unexpected Error");
System.exit(-1);
}
}
System.out.println("Done with the run function.");

}
}

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

Server s = new Server();

s.on();
}

}

enter image description here

关于java - Java中的多客户端服务器,也许线程运行不正常或者我不知道,无法找出原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56653688/

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