我正在开发一个简单的客户端 - 服务器时钟,它将当前时间传输到由时钟服务器应用程序托管的所有客户端。有一个定时事件(TimerTask)定期向所有客户端发送数据。并且在服务器部分它工作正常,但是当客户端运行时,它只读取一次,即使它在while循环中 - 它跳转到catch块并引发EOFException。最常见的是while循环中的readUTF()方法。我该如何抵消呢?
public class Client {
public static void main(String[] arg) {
Socket socketConnection = null;
ObjectOutputStream clientOutputStream = null;
ObjectInputStream clientInputStream = null;
try {
socketConnection = new Socket("127.0.0.1", 11111);
clientOutputStream = new ObjectOutputStream(
socketConnection.getOutputStream());
clientInputStream = new ObjectInputStream(
socketConnection.getInputStream());
while(true){
String date = clientInputStream.readUTF();
clientOutputStream.flush();
System.out.println(date);
}
} catch (Exception e) {
System.out.println("The following exception has occured and was caught:");
System.out.println(e);
}
finally{
try {
clientOutputStream.close();
clientInputStream.close();
socketConnection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
例如,我可以将整个try / catch / finally块放在while循环中,然后它可以正常工作。但是这样我的客户端每隔一秒断开连接并重新连接到服务器,这是不可接受的。有什么想法吗?
编辑 - stacktrace:
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at task2.Client.main(Client.java:28)
编辑 - 服务器代码:
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listeningSocket = true;
try {
serverSocket = new ServerSocket(11111);
} catch (IOException e) {
System.err.println("Could not listen on port: 11111");
}
while(listeningSocket){
System.out.println("Waiting for a client to connect...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");
ConnectThread ct = new ConnectThread(clientSocket);
ct.start();
}
System.out.println("closed");
serverSocket.close();
}
}
服务器一直在监听传入的客户端,每次接受连接时,它都会在一个单独的线程上开始对话,该线程由以下类定义:
public class ConnectThread extends Thread{
private Socket socket = null;
public ConnectThread(Socket socket) {
super("ConnectThread");
this.socket = socket;
}
@Override
public void run(){
ObjectOutputStream serverOutputStream = null;
ObjectInputStream serverInputStream = null;
try {
serverOutputStream = new ObjectOutputStream(socket.getOutputStream());
serverInputStream = new ObjectInputStream(socket.getInputStream());
ClockTask ctask = new ClockTask();
Timer timer = new Timer();
timer.schedule(ctask, 0, 1000);
Thread.sleep(1000);
serverOutputStream.writeUTF(ctask.date);
serverOutputStream.flush();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
serverOutputStream.close();
serverInputStream.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
计划定期更新日期:
public class ClockTask extends TimerTask {
public Calendar c;
public String date;
@Override
public void run() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
this.c = Calendar.getInstance();
this.date = dateFormat.format(c.getTime());
}
}
答案 0 :(得分:2)
在ConnectThread运行中需要一个循环 - 实际上,它会停止并且流已关闭。
编辑代码:
ClockTask ctask = new ClockTask();
Timer timer = new Timer();
timer.schedule(ctask, 0, 1000);
while( true ){
Thread.sleep(1000);
serverOutputStream.writeUTF(ctask.date);
serverOutputStream.flush();
}
答案 1 :(得分:2)
您的服务器将日期一次写入流,然后关闭。因此,客户端在第一次读取后获取和EOFException。