尝试通过套接字传递对象时发生EOFException

时间:2019-07-11 06:42:48

标签: java sockets

我正在尝试通过套接字将对象从服务器传递到客户端,发生的情况是,在某些PC上它运行良好,而在另一些PC上却无法运行。对象被无限期地更新,因此很明显,我使用一个无限循环来无限期地发送对象。尝试执行客户端时,会发送以下错误:

java -jar PC-CheckClient.jar 192.168.1.71 -console
Successfully established connection with server PC-CHECK
Starting... It may take several seconds
jul 11, 2019 1:34:31 AM com.he.pc.view.Main <init>
GRAVE: null 
java.io.EOFException 
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at com.he.pc.view.Main.<init>(Main.java:39)
    at com.he.pc.view.Main.main(Main.java:93)

这是代码Sonde命令,来自服务器的对象

private void sendData() {
        //Declarar, instanciar e inicializar
        ObjectOutputStream oos = null;
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            //Puerto 5000
            int puerto = 5000;
            serverSocket = new ServerSocket(puerto);
            System.out.println("Establishing connection with the PC-CHECK client");
            socket = serverSocket.accept();
            System.out.println("Successfully established connection");
            System.out.println("Starting... It may take several seconds");
            oos = new ObjectOutputStream(socket.getOutputStream());
            while (true) {
                //Obtencion del hardware
                this.cpuMain = this.loadCPU();
                this.ram = this.loadRAM();
                //Envio de datos al pc cliente
                oos.writeObject(this.cpuMain);
                oos.writeObject(this.ram);
            }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if(oos != null){
                    oos.close();
                }
                if(socket != null){
                    socket.close();
                }
                if(serverSocket != null){
                    serverSocket.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

客户在这里收到它

public Main(String ip) {

        try {
            socket = new Socket(ip, 5000);
            System.out.println("Successfully established connection with server PC-CHECK");
            System.out.println("Starting... It may take several seconds");
            ois = new ObjectInputStream(socket.getInputStream());
            while(true){
                this.cpu = (CPU)ois.readObject();
                Utilities.clear();
                System.out.println("CPU: ");
                System.out.println(cpu.toString());

                this.ram = (RAM) ois.readObject();
                System.out.println("RAM: ");
                System.out.println(this.ram.toString());
            }
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception e){

        }finally {
            try {
                if (socket != null) {
                    socket.close();
                }
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

我的问题是如何解决此错误,并确保所有计算机都能正常工作?

谢谢

0 个答案:

没有答案