java.io.EOFException(关闭流时遇到问题)

时间:2013-08-04 00:40:34

标签: java client outputstream eofexception

只有实现登录/注销才能获得简单的客户端/服务器应用程序。当我按连接它运行完美,但问题来自尝试断开连接(在客户端获得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(); }
     }
}

1 个答案:

答案 0 :(得分:2)

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