使用套接字时出现“ java.lang.InternalError:未初始化时意外调用”

时间:2018-11-29 03:01:35

标签: java sockets

我正在做分布式系统任务。我正在运行4个通过套接字相互连接的进程,它们全部都有一个用于接收数据的服务器线程和可以随时向其发送数据的连接列表。他们第一次共享消息很好,但是当节点3收到第二条消息时,出现以下错误:

Exception in thread "Thread-3" java.lang.InternalError: Unexpected call when not initialized
at java.base/java.io.ObjectStreamClass.requireInitialized(ObjectStreamClass.java:853)
at java.base/java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:862)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2061)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1594)
at java.base/java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:2310)
at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1898)
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1772)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2060)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1594)
at java.base/java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:2310)
at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1898)
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1772)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2060)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1594)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
at com.election.Channel.extractMessage(Canal.java:27)
at com.election.Channel.run(Canal.java:38)

以前我遇到另一个错误,StreamCorruptedException: invalid type code: AC,但是设法通过保存ObjectOutputStreams列表而不是从以前拥有的套接字列表中不断创建新的错误来解决它。

我的代码:

我有一个运行此服务器线程:

public void listen() {
    Socket socketClient;
    Channel channel;
    while (true){
        try {
            socketClient = socketServer.accept(); 
            channel = new Channel(socketClient, this.process);
            channel.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

通道线程具有构造函数,并在启动时运行该构造函数:

public void extractMessage(){
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(socket.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        while (true) {
            // Message is an object with some basic data
            Message message = (Message)in.readObject();

            // process is another object running in another thread
            process.handleMessage(mensage);
        }
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}

我这样发送数据

private void sendMessage(Node node, Mensagem m){
    try {
        // Node is an object that contains an ObjectOutputStream
        node.getOos().writeObject(m);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

编辑: 再次运行并...

java.io.StreamCorruptedException: invalid type code: 00
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1622)
at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2355)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2249)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2087)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1594)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
at com.election.Channel.extractMessage(Canal.java:27)
at com.election.Channel.run(Canal.java:38)

0 个答案:

没有答案