java.io.StreamCorruptedException:无效类型代码:00

问题描述 投票:0回答:8

所以基本上我正在编写一个客户端-服务器多人游戏。 我有一个 SeverCommunicationThread,如果他收到 RequestForGame 创建一个游戏线程,它就会创建一个游戏线程。 当我发送 RequestForGame 异常时抛出 java.io.StreamCorruptedException: invalid type code: 00 我认为这是因为两个线程都尝试读取相同的 ObjectInputStream,我对它的工作原理不太了解,我只知道如何使用它。您能帮我了解问题所在以及如何解决吗? 谢谢:)

public class ServerCommunicationThread extends Thread{
private Socket connectionSocket;
private ObjectInputStream inFromClient;
private ObjectOutputStream outToClient;
private String nickname;
private ServerModelManager model;


public ServerCommunicationThread(Socket connectionSocket,
        ServerModelManager model) throws IOException {
    this.connectionSocket = connectionSocket;
    inFromClient = new ObjectInputStream(connectionSocket.getInputStream());
    outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());
    this.model = model;
    start();

}

public void run() {
    try {
        String nickname = (String) inFromClient.readObject();
        if (model.exists(nickname)){
            System.out.println(nickname + " already exists");
            outToClient.writeObject(new MessageForClient("Please choose another nickname"));
        }
        else
        {
            System.out.println(nickname + " connected, adding to list");
            model.addClient(nickname, connectionSocket,outToClient,inFromClient);
            this.nickname=nickname;
        }
        while(true){
            Object o= inFromClient.readObject();//StreamCorruptedexception
            if(o instanceof RequestForGame)
            {
                RequestForGame r=(RequestForGame)o;
                String userToPlayWith=r.getUserToPlayWith();
                if(userToPlayWith.equals(nickname))
                {
                    String message="Playing with yourself makes your palms hairy, choose another opponent";
                    outToClient.writeObject(message);
                }
                else
                {
                System.out.println("received request to play with "+userToPlayWith+". starting game");
                ClientRepresentative client1=model.getClient(nickname);
                ClientRepresentative client2=model.getClient(userToPlayWith);
                ServerGameThread s=new ServerGameThread(client2,client1,client2.getInStream(),client1.getInStream(),client1.getOutStream(),client2.getOutStream());
                }
            }
            else if(o instanceof String)
            {
                String s=(String) o;
                if(s.equals("i want to quit"))
                {
                    model.deleteClient(nickname);
                    inFromClient.close();
                    String q="quit";
                    outToClient.writeObject(q);
                    connectionSocket.close();
                    System.out.println(nickname+"has quit without exc");
                }
            }
        }
    } catch (EOFException e) {
        System.out.println(nickname+" has quit");
    }
    catch (SocketException e)
    {
        System.out.println(nickname+" has quit");
    }

    catch (Exception e) {

        e.printStackTrace();
    }
}

}
 public class ServerGameThread extends Thread {

private ClientRepresentative client1,client2;
private ObjectInputStream inFromClient1,inFromClient2;
private ObjectOutputStream outToClient1,outToClient2;
private Field gameField; 
public ServerGameThread(ClientRepresentative client1, ClientRepresentative client2,ObjectInputStream inFromClient1,ObjectInputStream inFromClient2,ObjectOutputStream outToClient1,ObjectOutputStream outToClient2)
{
    System.out.println("startin game thred");
    this.client1=client1;//client 1 goes first
    this.client2=client2;//client 2 started game


        this.inFromClient1=inFromClient1;
        this.inFromClient2=inFromClient2;
        this.outToClient1=outToClient1;
        this.outToClient2=outToClient2;


        gameField=new Field();
        System.out.println("check");
        start();
}
public void run()
{
    System.out.println("Starting game. players: "+client1.getNickname()+";"+client2.getNickname());
    try {
        outToClient1.writeObject(gameField);
        outToClient2.writeObject(gameField);
        while(true)
        {
            try {
                System.out.println("listening to "+client1.getNickname());
                Object o1=inFromClient1.readObject();//read move from client 1.**//StreamCorruptedexception**

                while(!(o1 instanceof PlayerMove))
                {
                    o1=inFromClient1.readObject();//read move from client 1.
                }
                PlayerMove move1=(PlayerMove)o1;
                System.out.println("received move "+move1+" sending to "+client2.getNickname());
                outToClient2.writeObject(move1);
                System.out.println("listening to "+client2.getNickname());
                Object o2=inFromClient2.readObject();//read move from client 1.
                while(!(o2 instanceof PlayerMove))
                {   
                    o2=inFromClient2.readObject();//read move from client 1.
                }
                PlayerMove move2=(PlayerMove)o2;
                System.out.println("received move "+move2+" sending to "+client1.getNickname());
                outToClient1.writeObject(move2);
            }
                catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}    

model.addClient 方法虽然我不认为问题出在这里

  public void addClient(String nickname, Socket       clientSocket,ObjectOutputStream stream,ObjectInputStream inStream)
{
    clients.addClient(nickname, clientSocket,stream,inStream);//add to arraylist
//send client list to all clients
    String[] users=this.getAvailableClients();
    ObjectOutputStream[] streams=clients.getOutStreams();
    for(int i=0;i<streams.length;i++)
    {
        try {
            streams[i].writeObject(users);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

将对象发送到服务器的客户端代理,方法由 GUI 中的用户操作触发

  public class Proxy {
final int PORT = 1337;
String host;
String nickname;
private Socket clientSocket;
private ObjectOutputStream outToServer;
private ObjectInputStream inFromServer;
private ClientModelManager manager;
public Proxy(String nickname,String host,ClientModelManager manager)
{
    this.nickname=nickname;
    this.host=host;
    this.manager=manager;
    this.connect(nickname);
}
public void connect(String nick)
{
    Socket clientSocket;
    try {
        clientSocket = new Socket(host, PORT);
        System.out.println("client socket created");
        outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
        inFromServer=new ObjectInputStream(clientSocket.getInputStream());
        outToServer.flush();
        outToServer.writeObject(nick);
        ClientReceiverThread t=new ClientReceiverThread(inFromServer,manager);
        t.start();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}
public void makeRequest(String user)
{
    try
    {
    outToServer.writeObject(new RequestForGame(user));
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}
public void quit()
{
    try {
        outToServer.writeObject(new String("i want to quit"));
        //clientSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void sendMove(PlayerMove move)
{
    try {
        outToServer.writeObject(move);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

java multithreading sockets objectoutputstream
8个回答
10
投票

如果您在同一个套接字上构建新的

ObjectInputStream
ObjectOutputStream
,而不是在套接字的生命周期内使用相同的,则可能会发生此问题;如果您还在同一套接字上使用另一种流;或者,如果您使用对象流来读取或写入非对象的内容,那么您就会失去同步。


7
投票

在intellij中,这就是我解决Java问题的方法。 转到构建> gradle

然后将java版本更改为较低版本。


4
投票

如果读取序列化对象的 JVM 没有该对象的正确类/jar 文件,也可能会发生这种情况。这通常会产生

ClassNotFoundException
,但如果您有不同的 jar/类版本,并且
serialVersionUID
在版本之间没有更改,则会生成
StreamCorruptedException
。 (如果存在类名冲突,也可能出现此异常。例如:包含具有相同完整类名的不同类的 jar,尽管它们可能也需要相同的
serilVersionUID
)。

检查客户端是否有正确版本的 jar 和类文件。


4
投票

如果通过添加此方法为类实现自定义反序列化例程,我遇到了另一种可能性:

private void readObject( ObjectInputStream objectInputStream ) throws IOException

然后必须调用 objectInputStream.defaultReadObject() ,并在进一步读取输入流之前调用以正确初始化对象。

我错过了这一点,尽管对象返回时没有抛出异常,但下一次读取对象流却令人困惑地引发了无效类型代码异常。

此链接提供有关该过程的更多信息:http://osdir.com/ml/java.sun.jini/2003-10/msg00204.html.


3
投票

我也有这个例外。发生这种情况是因为我对服务器类和客户端类使用了两个线程。我使用一个线程来发送和接收对象。然后就没事了。如果您不熟悉

synchronized
,这是解决问题的简单方法。


2
投票

如果

ObjectInputStream
仅构造一次,然后将其引用传递给另一个线程,则只需将此对象的访问包含在
synchronized
块中,以确保一次只有一个线程可以访问该对象。

每当您从

ObjectInputStream
读取数据时,如果它在多个线程之间共享,只需在
synchronized
块内访问它。


示例代码:(对所有出现的

readObject()
执行此操作)

...
String nickname = null;
synchronized (inFromClient) {
    nickname = (String) inFromClient.readObject();
}

1
投票

java.io.StreamCorruptedException:无效类型代码:00

我最近遇到了这个问题,但没有做OP所做的事情。进行了快速的谷歌搜索,没有找到任何太有帮助的东西,因为我认为我解决了它,所以我正在对我的解决方案发表评论。

TLDR:不要让多个线程同时写入同一个输出流(而是轮流)。当客户端尝试读取数据时会导致问题。解决方案是锁定输出写入。

我正在做一些与OP非常相似的事情,制作多人(客户端-服务器模型)游戏。我有一个像 OP 这样的线程正在监听流量。在我的服务器端发生的情况是,服务器有多个线程同时写入客户端的流(认为这是不可能的,游戏是半回合制的)。读取传入流量的客户端线程抛出此异常。为了解决这个问题,我基本上在写入客户端流(在服务器端)的部分上加了一个锁,因此服务器端的每个线程都必须在写入流之前获得锁。


1
投票

我的问题是 gradle JVM 配置不正确。请检查设置中是否使用了兼容的JDK

1. File | Setting | Build, Execution, Deployment | Build Tools | Gradle | Gradle JVM (Here is my problem)

2. Please select the correct your own JDK directory installation in the local computer (default dir in windows is located at the path "C:\Program Files\Java\jdk-17")

希望对大家有帮助

© www.soinside.com 2019 - 2024. All rights reserved.