gpt4 book ai didi

java - Java 客户端-服务器应用程序中的 EOFException

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

我正在开发一个简单的客户端服务器时钟,它将当前时间传输到由时钟服务器应用程序托管的所有客户端。有一个定时事件(TimerTask)定期向所有客户端发送数据。在服务器部分,它工作正常,但是当客户端运行时,它只读取一次时间,即使它处于 while 循环中 - 它会跳转到 catch block 并引发 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 block 放入 while 循环中,然后它就可以正常工作了。但这样我的客户端每秒都会断开连接并重新连接到服务器,这是 Not Acceptable 。有什么想法吗?

编辑 - 堆栈跟踪:

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());
}

}

最佳答案

在 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();
}

关于java - Java 客户端-服务器应用程序中的 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756202/

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