java.io.StreamCorruptedException:无效的流标头:7371007E
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2939073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.StreamCorruptedException: invalid stream header: 7371007E
提问by Alex
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
}
采纳答案by mdma
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.
如果您要发送多个对象,通常最简单的方法是将它们放入某种类型的持有者/集合中,例如Object[]
或List
。它使您不必明确检查流的结尾,并负责明确传输流中的对象数量。
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 sameobject 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 传递给您的服务器读取方法并从中读取对象。
回答by Stephen C
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
.
当我只从客户端向服务器发送一个对象时,一切正常。
当我尝试在同一个流上一个接一个地发送多个对象时,我得到
StreamCorruptedException
.
Actually, your client code is writing one object to the server and readingmultiple objects from the server. And there is nothing on the server side that is writingthe objects that the client is trying to read.
实际上,您的客户端代码正在向服务器写入一个对象并从服务器读取多个对象。服务器端没有任何东西写入客户端试图读取的对象。
回答by James Raitsev
This exception may also occur if you are using Socket
s on one side and SSLSocket
s on the other. Consistency is important.
如果您Socket
在一侧使用s 而在另一侧使用s,也可能会发生此异常SSLSocket
。一致性很重要。