java.io.StreamCorruptedException:无效的流标题:7371007E [英] java.io.StreamCorruptedException: invalid stream header: 7371007E

查看:324
本文介绍了java.io.StreamCorruptedException:无效的流标题:7371007E的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用对象进行通信的客户端服务器应用程序。

当我只从客户端向服务器发送一个对象时,一切正常。

当我尝试发送多个对象时我在同一个流上接连一个

I have a client Server application which communicate using objects.
when I send only one object from the client to server all works well.
when I attempt to send several objects one after another on the same stream I get

StreamCorruptedException.  

有人可以指导我查看此错误的原因吗?

Can some one direct me to the cause of this error?

客户端写入方法

   private SecMessage[] send(SecMessage[] msgs) 
   {
     SecMessage result[]=new SecMessage[msgs.length];
      Socket s=null;
      ObjectOutputStream objOut =null;
      ObjectInputStream objIn=null;
      try
      {
       s=new Socket("localhost",12345);
       objOut=new ObjectOutputStream( s.getOutputStream());
       for (SecMessage msg : msgs) 
       {
            objOut.writeObject(msg);
       }
       objOut.flush();
       objIn=new ObjectInputStream(s.getInputStream());
       for (int i=0;i<result.length;i++)
            result[i]=(SecMessage)objIn.readObject();
      }
      catch(java.io.IOException e)
      {
       alert(IO_ERROR_MSG+"\n"+e.getMessage());
      } 
      catch (ClassNotFoundException e) 
      {
       alert(INTERNAL_ERROR+"\n"+e.getMessage());
      }
      finally
      {
       try {objIn.close();} catch (IOException e) {}
       try {objOut.close();} catch (IOException e) {}
      }
      return result;
 }

服务器读取方法

//in is an inputStream Defined in the server
SecMessage rcvdMsgObj;
rcvdMsgObj=(SecMessage)new ObjectInputStream(in).readObject();
return rcvdMsgObj;

和SecMessage类是

and the SecMessage Class is

public class SecMessage implements java.io.Serializable
{
 private static final long serialVersionUID = 3940341617988134707L;
 private String cmd;
    //... nothing interesting here , just a bunch of fields , getter and setters
}


推荐答案

如果要发送多个对象,通常最简单的方法是将它们放在某种类型的持有者/集合中,如 Object [] 列表。它可以节省你必须明确检查流的结束并负责明确传输流中有多少对象。

If you are sending multiple objects, it's often simplest to put them some kind of holder/collection like an Object[] or List. It saves you having to explicitly check for end of stream and takes care of transmitting explicitly how many objects are in the stream.

编辑:现在我格式化了代码,我看到你已经在数组中有消息。只需将数组写入对象流,然后在服务器端读取数组。

Now that I formatted the code, I see you already have the messages in an array. Simply write the array to the object stream, and read the array on the server side.

您的服务器读取方法只读取一个对象。如果多次调用它,则会出现错误,因为它尝试从同一输入流中打开多个对象流。这不起作用,因为所有对象都写入客户端的相同对象流,因此您必须在服务器端镜像这种安排。也就是说,使用一个对象输入流并从中读取多个对象。

Your "server read method" is only reading one object. If it is called multiple times, you will get an error since it is trying to open several object streams from the same input stream. This will not work, since all objects were written to the same object stream on the client side, so you have to mirror this arrangement on the server side. That is, use one object input stream and read multiple objects from that.

(你得到的错误是因为objectOutputStream写了一个头,这是objectIutputStream所期望的。你不是在写多个流,而只是多个对象,那么在套接字输入上创建的下一个objectInputStream无法找到第二个头,并抛出异常。)

(The error you get is because the objectOutputStream writes a header, which is expected by objectIutputStream. As you are not writing multiple streams, but simply multiple objects, then the next objectInputStream created on the socket input fails to find a second header, and throws an exception.)

To修复它,在接受套接字连接时创建objectInputStream。将此objectInputStream传递给您的服务器读取方法并从中读取Object。

To fix it, create the objectInputStream when you accept the socket connection. Pass this objectInputStream to your server read method and read Object from that.

这篇关于java.io.StreamCorruptedException:无效的流标题:7371007E的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆