我有一个任务要聊天。我有四个类服务器,客户端,作家和读者。客户端与服务器连接。当这些过程之间的连接正常时。客户端和服务器启动读写线程,客户端和服务器可以通信。当它们都写"退出"时,连接将关闭。客户端进程已终止,服务器正在等待连接。问题是当我再次尝试连接服务器时。服务器中的编写器线程正在立即关闭。我观察到当程序处于时
line = reader.readLine()
立即跳转到
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
我真的很感激任何想法:)
以下是类别:
客户端
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");
}
}
}
您的"作家;正在读取System.in
,然后将其关闭;s只有一个System.in,所以如果您关闭它,您将';我再也读不下去了。–nbsp;heenenee
好吧,我只是删除了那几行,一切都很完美Kapool