weixin_44555034 2021-04-22 21:27 采纳率: 100%
浏览 345
已采纳

ObjectInputStream的invalid stream header的问题

public class Server {
  static ExecutorService executor = new ThreadPoolExecutor(30, 60, 3L, TimeUnit.HOURS,
      new ArrayBlockingQueue<Runnable>(15), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

  public static void main(String[] args) {
    ThreadPoolExecutor executor1 = (ThreadPoolExecutor) executor;
    try {
      ServerSocket serverSocket = new ServerSocket(6688);
      Socket socket = null;
      while (true) {
        System.out.println("服务器:开始监听6688");
        socket = serverSocket.accept();
        myruns command = new myruns(socket);
        new Thread(command).start();
        executor.execute(command);
        System.out.println("第" + executor1.getActiveCount() + "个客户端连接服务器");
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}

class myruns implements Runnable {
  Socket socket = null;
  ObjectInputStream ois = null;

  public myruns(Socket socket) {
    this.socket = socket;
  }

  @Override
  public void run() {
    // TODO Auto-generated method stub
    try {
      ois = new ObjectInputStream(socket.getInputStream());
      while (true) {
        while (ois.available() != -1) {
          Thread.sleep(1000);
          try {
            User user = (User) ois.readObject();
            System.out.println(user.toString());
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } finally {
      Thread.interrupted();
    }
  }

}

这个是服务端的代码

public class Client {
  static Data data = Data.getInstance();
  static App app = new App();

  public static void main(String[] args) throws UnknownHostException, IOException {

    Socket socket = new Socket("127.0.0.1", 6688);
    try {
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
      while (true) {
        if (data.getS1().equals("退出系统")) {
          socket.close();
          System.exit(0);
        }
        objectOutputStream.writeObject(new User());
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
}

这个是客户端的

究极萌新想问问各位大牛 这个到底咋回事 在教室看了一天没看出来咋解决 哭了哭了。

public class User implements Serializable {<!-- -->

对象也是序列化的了。。

java.io.StreamCorruptedException: invalid stream header: ACED7372
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:936)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:394)
        at Common.myruns.run(Server.java:59)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
java.io.StreamCorruptedException: invalid stream header: 00050009
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:936)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:394)
        at Common.myruns.run(Server.java:59)
        at java.lang.Thread.run(Thread.java:748)

QAQ这个异常搞了我几天了

  • 写回答

1条回答 默认 最新

  • weixin_44555034 2021-04-25 15:42
    关注

    不使用线程池就一点问题都没有了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码