gpt4 book ai didi

java.io.EOFException(关闭流时出现问题)

转载 作者:行者123 更新时间:2023-11-30 04:17:22 26 4
gpt4 key购买 nike

有一个简单的客户端/服务器应用程序,仅实现了登录/注销。当我按下连接时,它运行完美,但在尝试断开连接时出现问题(在客户端收到 EOFException)。我几乎可以肯定这是由于流的关闭不良造成的。有什么提示吗?

java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:98)
at java.io.ObjectInputStream.nextTC(ObjectInputStream.java:506)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:778)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
at com.mtm.ClientConnection.disconnect(ClientConnection.java:54)**

客户端类:

public class ClientConnection implements Admin{

public ObjectInputStream in;
public static ObjectOutputStream out;
public Socket socket;

public void connect(){

String host = IP;
int port = PORTO;

try {
socket = new Socket (host,port);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());

MSG_Login login = new MSG_Login();
login.setID(DeviceId.getId().toString());
send(login);

Object c1 = in.readObject();
if(c1 instanceof MSG_Login){
Thread thread = new ClientThread(this);
thread.start();
}

}
catch (UnknownHostException e) { e.printStackTrace(); }
catch (SocketException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
catch (ClassNotFoundException e) { e.printStackTrace(); }
}

public void disconnect(){
try {
MSG_Logout logout = new MSG_Logout();
logout.setID(DeviceId.getId().toString());
send(logout);

Object c1 = in.readObject();
if(c1 instanceof MSG_Logout){
in.close();
out.close();
socket.close();
}
}
catch (IOException e) { e.printStackTrace(); }
catch (ClassNotFoundException e) { e.printStackTrace(); }
}

public static void send(Object obj) {
try {
out.writeObject(obj);
out.flush();
out.reset();
}
catch (IOException e) { e.printStackTrace(); }
}
}

服务器(线程)类:

public class Servidor_Thread extends Thread{

public Socket canal;
Servidor serv;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
private boolean logOff;

public Servidor_Thread(Servidor serv) {
this.serv = serv;
canal = serv.socket;
logOff = false;
}

public void run(){
try {
ois=new ObjectInputStream(canal.getInputStream());
oos=new ObjectOutputStream(canal.getOutputStream());
while(logOff==false){
Object obj=ois.readObject();/** Objecto recebido - Reconstrução **/
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
if(obj instanceof MSG_Login){
serv.id_database.add(((MSG_Login) obj).getID());
serv.getLog().appendConsole("["+dateFormat.format(date)+"]..........User "+((MSG_Login) obj).getID()+" connected.");
enviar(obj);
}
if(obj instanceof MSG_Logout){
serv.id_database.remove(((MSG_Logout) obj).getID());
serv.getLog().appendConsole("["+dateFormat.format(date)+"]..........User "+((MSG_Logout) obj).getID()+" disconnected.");
enviar(obj);
stopThread();
}
}
}
catch (IOException e) { stopThread(); }
catch (ClassNotFoundException e) { e.printStackTrace(); }
}

public void stopThread(){
logOff = true;
try {
ois.close();
oos.close();
canal.close();
} catch (IOException e) { e.printStackTrace(); }
}

public void enviar(Object obj) {
try {
oos.writeObject(obj);
oos.flush();
oos.reset();
}
catch (IOException e) { e.printStackTrace(); }
}
}

最佳答案

这里没有问题可以回答。 readObject() 方法在流末尾抛出 EOFException。这是它的正常行为。您必须捕获它,关闭正在读取的流,然后退出读取循环。

关于java.io.EOFException(关闭流时出现问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038699/

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