简体   繁体   English

OptionalDataException Java

[英]OptionalDataException Java

I am making a voice chat program and I got the OptionalDataException error and I never had this problem with the code before I added voice. 我正在制作语音聊天程序,但出现OptionalDataException错误,在添加语音之前,代码从未遇到此问题。 The voice communication is handles by a different socket so I don't see the problem. 语音通信是由不同的套接字处理的,所以我看不到问题。

Code: 码:

    public class Client implements Runnable {                                                   // CLIENT
    private String msg;
    public void run() {
        try {
            s1 = new Socket(ipAddress, port);
            s2 = new Socket(ipAddress, 1210);
            o1 = new ObjectOutputStream(s1.getOutputStream());
            o1.writeObject(name);
            serverListModel.addElement(name);
            i1 = new ObjectInputStream(s1.getInputStream());
            Thread voice = new Thread(new ClientAudio());
            voice.start();
            while(true) {
                msg = (String) i1.readObject();
                    String[] namePart = msg.split("-");
                    if(namePart[0].equals("AddName") && !namePart[1].equals(name) && !serverListModel.contains(namePart[1])) {
                        serverListModel.addElement(namePart[1]);
                    }
                    if(namePart[0].equals("RemoveName") && !namePart[1].equals(name)) {
                        serverListModel.removeElement(namePart[1]);
                    }
                    if(!msg.equals(null) && !namePart[0].equals("AddName") && !namePart[0].equals("RemoveName")) {
                        chatWindow.append(msg+"\n");
                    }
                }
        } catch (IOException | ClassNotFoundException e) {
            chatWindow.append("Server Closed");
            e.printStackTrace();
            try {
                s1.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            mainWindow(true);
        }
    }
}


flag

it was thrown at msg = (String) i1.readObject(); 它被扔在msg = (String) i1.readObject(); and it says 它说

java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1361)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at client.chat$Client.run(chat.java:319)
at java.lang.Thread.run(Thread.java:745)

From Oracle : Oracle

Exception indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream. 异常,指示由于未读的原始数据或流中属于序列化对象的数据的结尾而导致对象读取操作失败。 This exception may be thrown in two cases: 在两种情况下可能会引发此异常:

  • An attempt was made to read an object when the next element in the stream is primitive data. 当流中的下一个元素是原始数据时,尝试读取对象。 In this case, the OptionalDataException's length field is set to the number of bytes of primitive data immediately readable from the stream, and the eof field is set to false. 在这种情况下,OptionalDataException的length字段设置为立即可从该流读取的原始数据的字节数,而eof字段设置为false。

  • An attempt was made to read past the end of data consumable by a class-defined readObject or readExternal method. 试图通过类定义的readObject或readExternal方法读取超出消耗性数据结尾的数据。 In this case, the OptionalDataException's eof field is set to true, and the length field is set to 0. 在这种情况下,OptionalDataException的eof字段设置为true,而length字段设置为0。

It would appear that the next object in the Stream is not a String . 看来Stream中的下一个对象不是String

Is the server code under your control? 服务器代码受您控制吗? Or do you at least have the source? 还是至少有来源? If so, verify that String objects are the only ones being transmitted, or adjust your code to handle the actual objects/primitives being sent. 如果是这样,请验证String对象是唯一要发送的对象,或者调整代码以处理实际发送的对象/基元。

Edit 编辑

From your other question Voice Server not working : 从另一个问题来看, 语音服务器无法正常工作

byte[] soundData = 
//...
o.write(soundData, 0, bytesRead);

... It looks like you are not writing String objects to the ObjectOutputStream . ...好像您没有将String对象写入ObjectOutputStream Actually, not even writing an Object, but raw bytes. 实际上,甚至不写对象,而是原始字节。 You have to read data the same way you write it; 您必须以与写入数据相同的方式读取数据。 anything else just won't work. 其他任何东西都行不通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java中的OptionalDataException - OptionalDataException in java run方法中的java.io.OptionalDataException? - java.io.OptionalDataException in run method? objectInputStream.readObject()引发异常java.io.OptionalDataException - objectInputStream.readObject() throws exception java.io.OptionalDataException 仅在序列化对象时Java OptionalDataException(任何地方都没有原始数据类型) - Java OptionalDataException when only serializing objects (no primitive data types anywhere) 自定义(de-)序列化方法抛出java.io.OptionalDataException - Custom (de-) serialize method throws java.io.OptionalDataException 在我的代码中找不到java.io.OptionalDataException的原因 - Cannot find reason for java.io.OptionalDataException in my code 由于原始int值而获取OptionalDataException,但如何在JAVA中避免它 - Getting OptionalDataException because of primitive int value , but how to avoid it in JAVA javax.ejb.EJBException:发送请求时出错[原因:java.io.OptionalDataException] - javax.ejb.EJBException: Error while sending a request[Caused by: java.io.OptionalDataException] ReadObject返回OptionalDataException - ReadObject returns OptionalDataException ObjectInputStream readObject不起作用OptionalDataException - ObjectInputStream readobject doesnt work OptionalDataException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM