相关疑难解决方法(0)

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

所以基本上我正在写一个客户端 - 服务器多人游戏.我有一个SeverCommunicationThread,如果他收到RequestForGame就会创建一个gameThread来创建gameThread.当我发送一个RequestForGame异常时抛出java.io.StreamCorruptedException:无效的类型代码: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 …
Run Code Online (Sandbox Code Playgroud)

java sockets multithreading objectoutputstream

10
推荐指数
2
解决办法
3万
查看次数

Java中的序列化,无效的类型代码00

在读取序列化对象时,我收到错误(java.io.StreamCorruptedException:无效的类型代码:00).这是实现serializable的类:

package guts;

import cc.mallet.classify.*;
import java.io.*;


public class NaiveBayesWithID implements Serializable  {

private NaiveBayes nb;
private static final long serialVersionUID = 1;
    private static final int CURRENT_SERIAL_VERSION = 1;

public NaiveBayesWithID(NaiveBayes nb) {
this.nb = nb;
}
public NaiveBayesWithID(){
this.nb = null;
}

    public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    int version = in.readInt();
    if (version != CURRENT_SERIAL_VERSION)
        throw new ClassNotFoundException("Mismatched NaiveBayesTrainer versions: wanted " +
                CURRENT_SERIAL_VERSION + ", got " +
                version);

    //default selections for the kind …
Run Code Online (Sandbox Code Playgroud)

java serialization

5
推荐指数
1
解决办法
2万
查看次数