gpt4 book ai didi

java - 与单独的线程读/写操作聊天。写入线程立即关闭

转载 作者:行者123 更新时间:2023-12-02 01:39:51 24 4
gpt4 key购买 nike

我有一个聊天任务。我有 4 个类服务器、客户端、写入器和读取器。客户端与服务器连接。当进程之间的连接正常时。 Client和Server启动Reader和Write线程,Client和Server可以进行通信。当他们都写“退出”时,连接就关闭了。客户端进程终止,服务器正在等待连接。问题是当我尝试再次连接服务器时。服务器中的写入器线程立即关闭。我观察到当程序处于

line = reader.readLine()

立即跳转至

    try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}

我真的很感激任何想法:)

以下是类(class):

客户端

public class Client {

public static void main(String[] args) {
Socket kSocket = null;
ThreadGroup group = new ThreadGroup("Client");

try {
kSocket = new Socket("localhost", 2222);
System.out
.println("Connected with: " + kSocket.getInetAddress() + " at port: " + kSocket.getPort());
new Pisarz(group, kSocket).start();
new Czytelnik(group, kSocket).start();
while(group.activeCount()!=0){}
kSocket.close();
} catch (UnknownHostException e) {
} catch (IOException e) {
System.err.println("Uuu IOException");
}
}

}

服务器

public class Server {
public static void main(String[] args) {
ServerSocket server = null;
ThreadGroup group = new ThreadGroup("Server");
try {
server = new ServerSocket(2222);
} catch (IOException e) {
System.out.println("Unavaible to listen on port 2222");
}
try {
while (true) {
Socket sSocket = server.accept();
System.out.println(
"Connected with: " + sSocket.getInetAddress() + " at port: " + sSocket.getPort());

new Pisarz(group, sSocket).start();
new Czytelnik(group, sSocket).start();
while (group.activeCount() != 0) {
}
}
} catch (SocketException e) {
System.err.println("Socket exception :/");
} catch (UnknownHostException e) {
System.err.println("UnknownHost :/");
} catch (IOException e) {
System.err.println("IO Exception :/");

} finally {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

作者主题

public class Writer extends Thread {
Socket sOut;
ThreadGroup group;

public Pisarz(ThreadGroup group, Socket sOut) {
super(group, "Writer");
this.sOut = sOut;
this.group = group;
}

@Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter printer = null;
try {
printer = new PrintWriter(sOut.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
String line = null;
try {
while ((line = reader.readLine()) != null) {
printer.println(line);
printer.flush();
if (line.equals("exit")) {
break;
}
}
} catch (IOException e) {}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Writer is closed");

}
}

读者话题

public class Reader extends Thread {

Socket sInput;
ThreadGroup group;
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

public Czytelnik(ThreadGroup group,Socket sInput) {
super(group, "Reader");
this.sInput = sInput;
this.group = group;

}

@Override
public void run() {
String line = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(sInput.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(time.format(new Date()) + " " + line);
if (line.equals("exit")) {
System.out.println("Reader is closed");
break;
}
}
} catch (IOException e) {
System.out.println("IOException");
}
}
}

最佳答案

你的“writer”正在从System.in读取,然后关闭它。只有一个System.in,所以如果你关闭它,你将无法再次读取它。 – heenenee

好吧,我只需删除该行即可,一切正常。 – 卡 boolean

关于java - 与单独的线程读/写操作聊天。写入线程立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485130/

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