服务器端的ObjectInputStream和客户端的ObjectOutputStream已经用相应的Socket创建好,但是当客户端写入对象时服务器却无法收到,有时还会抛出StreamCorruptedException异常,以下是异常的详细信息:
     java.io.StreamCorruptedException: invalid handle value: 71007E00
at java.io.ObjectInputStream.readHandle(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Server$paintReceiveHandler.run(Server.java:381)
at java.lang.Thread.run(Unknown Source)请问这是什么原因?谢谢

解决方案 »

  1.   

    建议你使用 Stream  进行 byte级别的操作,最安全!
      

  2.   

    这是用String类型,用其他类型的对象时服务器端会抛出OptionalDataException异常
       java.io.OptionalDataException
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Server$paintReceiveHandler.run(Server.java:381)
    at java.lang.Thread.run(Unknown Source)
    下面是源代码:服务器端:
       public void run() {
    while (true) {
    try {
      if (ObjectIn != null) { test rcv=(test)ObjectIn.readObject();
    }
    }
                      catch (IOException e) {
    e.printStackTrace();
    }   catch (ClassNotFoundException e) {
    e.printStackTrace();
         }

      try {
    Thread.sleep(400);

                      catch (InterruptedException e) {
    e.printStackTrace();
    } }
    }客户端:
        public void run() {
              
    try {
    objectOut=new ObjectOutputStream(s.getOutputStream());
                } 
            catch (IOException e1) {
    e1.printStackTrace();
    }
    while (true) {
    try {
    objectOut.writeObject(new test(1,6)); } 
                                catch (IOException e) { e.printStackTrace();
    }  

    }
     try {
    Thread.sleep(400);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }    
    } }test类:
       public class test implements Serializable
        {
         int a;
         int b;
         public test()
         {
         a=1;
         b=1;
         }
         public test(int a,int b)
         {
         this.a=a;
         this.b=b;
         }
        }谢谢大家帮助!
      

  3.   

    传输字符串对象服务器端会抛出StreamCorruptedException,其他对象会抛出OptionalDataException
      

  4.   

    答:在objectOut.writeObject(new test(1,6)); 之后加上objectOut.flush();试试。
      

  5.   

    源代码我放到邮箱里了:[email protected] 密码jz370682,谢谢您
      

  6.   

    答:你的程序我已收到并看过了。写得挺好的。一运行就知道问题的根源了:
    1)问题不在对象序列化上,其实你的几何图形、颜色等对象序列化后在网络上传输都是成功的,没有问题。
    2)问题根源是:当客户连接进来后,你用同一个socket的流,既用Scanner方式传输命令、又用Object流方式传输对象,这才是产生这个java.io.StreamCorruptedException异常的根本原因。这一点已单独验证过了。
    3)因此:建议:命令用命令端口传输、几何图形等对象用数据端口传输就成功了。这一点也已单独验证过了。
    当时在设计基于网络的协同式矢量图形编辑器时(与老美合作的子项目之一),有:命令端口、图形对象数据端口(因为:图形画面要即时刷新)、文本消息端口等。以上仅供你参考