This question already has answers here:
Continuously read objects from an ObjectInputStream in Java
                                
                                    (4个答案)
                                
                        
                                4年前关闭。
            
                    
几周前,我发布了以下问题,因为在使用readObject从ObjectInputStream读取对象时遇到了问题:

Continuously read objects from an ObjectInputStream in Java

通过我得到的响应,我认为我能够理解出了什么问题->即使没有数据发送也正在循环中调用readObject,因此我收到了EOFException。

但是,因为我真的想要一种不断从输入流中读取内容的机制,所以我正在寻找解决此问题的方法。

我尝试使用以下内容创建一种机制,仅在有可用数据时才调用readObject:

if(mObjectIn.available() > 0)
    mObjectIn.readObject()


但不幸的是,mObjectIn.available()始终返回0。

谁能帮助我朝好的方向发展。是否有可能实现我想要的?

最佳答案

您可以通过int发送ObjectOutputStream,让对方知道何时停止发送对象。

例如:

public static void main(String[] args) {
    //SERVER
    new Thread(new Runnable() {
        @Override
        public void run() {
            try (ServerSocket ss = new ServerSocket(1234)) {
                try (Socket s = ss.accept()) {
                    try (ObjectInputStream ois = new ObjectInputStream(
                            s.getInputStream())) {
                        while (ois.readInt() != -1) {//Read objects until the other side sends -1.
                            System.out.println(ois.readObject());
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

    //CLIENT
    try (Socket s = new Socket(InetAddress.getByName("localhost"), 1234)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(
                s.getOutputStream())) {
            for (int i = 0; i < 10; i++) {
                oos.writeInt(1);//Specify that you are still sending objects.
                oos.writeObject("Object" + i);
                oos.flush();
            }
            oos.writeInt(-1);//Let the other side know that you've stopped sending object.
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}


或者,您可以在最后写一个null对象,让对方知道您将不再发送任何对象。仅在确定您要发送的所有对象都不是null时,此方法才起作用。

new Thread(new Runnable() {
        @Override
        public void run() {
            try (ServerSocket ss = new ServerSocket(1234)) {
                try (Socket s = ss.accept()) {
                    try (ObjectInputStream ois = new ObjectInputStream(
                            s.getInputStream())) {
                        String obj;
                        while ((obj = (String) ois.readObject()) != null) {
                            System.out.println(obj);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

    try (Socket s = new Socket(InetAddress.getByName("localhost"), 1234)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(
                s.getOutputStream())) {
            for (int i = 0; i < 10; i++) {
                oos.writeObject("Object" + i);
                oos.flush();
            }
            oos.writeObject(null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

关于java - 检查ObjectInputStream上是否有数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30510344/

10-10 11:21