. io .streamtedexception:无效流标头:7371007E。

时间:2022-08-17 20:35:43

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?

有人能告诉我这个错误的原因吗?

client write method

客户端写方法

   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;
 }

server read method

服务器读取方法

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

and the SecMessage Class is

SecMessage类是。

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
}

3 个解决方案

#1


7  

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.

如果您要发送多个对象,那么通常最简单的方法是将它们作为一个对象或列表(如对象[]或列表)。它省去了您必须显式地检查流的末端,并负责显式地传输流中有多少对象。

EDIT: 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.

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

(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.)

(您得到的错误是因为objectOutputStream写了一个标题,这是被objectIutputStream所期望的。由于您不是在编写多个流,而只是多个对象,因此在套接字输入上创建的下一个objectInputStream不能找到第二个报头,并抛出一个异常。

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.

要修复它,当您接受套接字连接时,创建objectInputStream。将这个objectInputStream传递到您的服务器读取方法并从中读取对象。

#2


1  

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.

当我尝试在同一条流上一个接一个地发送多个对象时,我得到了streamtedexception。

Actually, your client code is writing one object to the server and reading multiple objects from the server. And there is nothing on the server side that is writing the objects that the client is trying to read.

实际上,您的客户端代码正在向服务器写入一个对象,并从服务器读取多个对象。在服务器端,没有任何东西是在写入客户机试图读取的对象。

#3


-2  

This exception may also occur if you are using Sockets on one side and SSLSockets on the other. Consistency is important.

如果您在一边使用套接字,另一边使用SSLSockets,则可能会出现此异常。一致性是很重要的。

#1


7  

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.

如果您要发送多个对象,那么通常最简单的方法是将它们作为一个对象或列表(如对象[]或列表)。它省去了您必须显式地检查流的末端,并负责显式地传输流中有多少对象。

EDIT: 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.

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

(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.)

(您得到的错误是因为objectOutputStream写了一个标题,这是被objectIutputStream所期望的。由于您不是在编写多个流,而只是多个对象,因此在套接字输入上创建的下一个objectInputStream不能找到第二个报头,并抛出一个异常。

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.

要修复它,当您接受套接字连接时,创建objectInputStream。将这个objectInputStream传递到您的服务器读取方法并从中读取对象。

#2


1  

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.

当我尝试在同一条流上一个接一个地发送多个对象时,我得到了streamtedexception。

Actually, your client code is writing one object to the server and reading multiple objects from the server. And there is nothing on the server side that is writing the objects that the client is trying to read.

实际上,您的客户端代码正在向服务器写入一个对象,并从服务器读取多个对象。在服务器端,没有任何东西是在写入客户机试图读取的对象。

#3


-2  

This exception may also occur if you are using Sockets on one side and SSLSockets on the other. Consistency is important.

如果您在一边使用套接字,另一边使用SSLSockets,则可能会出现此异常。一致性是很重要的。