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

查看:18
本文介绍了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+"
"+e.getMessage());
      } 
      catch (ClassNotFoundException e) 
      {
       alert(INTERNAL_ERROR+"
"+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.)

要修复它,请在接受套接字连接时创建 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天全站免登陆