gpt4 book ai didi

c# - 读取从 C# 客户端发送的 ObjectInputStream 时为 "StreamCorruptedException: invalid stream header"

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:30:03 25 4
gpt4 key购买 nike

问题

我正在尝试从 C# 客户端向此 Java 服务器发送一条 protobuf 消息,但出现此异常:

java.io.StreamCorruptedException: invalid stream header: 0A290A08 
java.io.StreamCorruptedException: invalid stream header: 0A290A08
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)

老实说,我有点不知所措。任何帮助表示赞赏。谢谢!

  • Java 服务器
    public ControllerThread(Socket s){
this.s = s; try {

this.objectInputStream = new ObjectInputStream(s.getInputStream());
byte size = objectInputStream.readByte();System.out.println("Server: BYTES SIZE:" + size);
byte[] bytes = new byte[size];
objectInputStream.readFully(bytes);
AddressBook adb = AddressBook.parseFrom(bytes);
System.out.println("Server: Addressbook:" + adb.getPersonCount());

} catch (IOException e) {
System.out.println("Server: BufferedReader oder PrintWriter von ThermoClient konnte nicht erstellt werden");
e.printStackTrace(); }
} }

C#代码

public AddressBook InitializeAdressBook()
{
Person newContact = new Person();

AddressBook addressBookBuilder = new AddressBook();
Person john = new Person();
//john.id=1234;
john.name="John Doe";
john.email="jdoe@example.com";
Person.PhoneNumber nr = new Person.PhoneNumber();
nr.number="5554321";
john.phone.Add(nr);
addressBookBuilder.person.Add(john);
TextBox.Text += ("Client: Initialisiert? " + addressBookBuilder.ToString()) + "\t" + "\n";
TextBox.Text += " Erster Person " + addressBookBuilder.person.First().name + "\t" + "\n";

return addressBookBuilder;
}

c# 输出流

    public void SendMessage(Stream ns, byte[] msg)
{
byte size = (byte)msg.Length;

try
{
ns.WriteByte(size);
ns.Write(msg, 0, msg.Length);
ns.Flush();
ns.Close();
}
catch (ArgumentNullException ane)
{
TextBox.Text += "ArgumentNullException : {0}" + ane.ToString();
}
catch (Exception e)
{
TextBox.Text += ("Unexpected exception : {0}" + e.ToString());
}


}

最佳答案

tldr; 问题是使用 ObjectInputStream (Java) 仅适用于 ObjectOutputStream (Java) 生成的数据.在这种情况下 StreamCorruptedException正在生成,因为流被赋予了 无效 数据,而这些数据不是由 ObjectOutputStream (Java) 生成的 .

相反,使用 DataInputStream (Java)读取 BinaryWriter (C#) 生成的数据.这两个都只支持“原始”类型。只要正确endianess used sign stuffing 根据需要执行:整数、 float 、 double (但不是小数)和字节数组可以通过这种方式安全地发送。

ObjectInputStream (Java) :

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream [in Java].

DataInputSteam (Java) :

A data input stream lets an application read primitive [..] types from an underlying input stream ..

BinaryWriter (C#) :

Writes primitive types in binary to a stream and supports writing strings in a specific encoding.


注意事项:

  • DataInputSteam (Java) is big-endian , 但是 BinaryWriter (C#) must be converted to big-endian .
  • 传输 char/character 时没有任何问题(除了字节序) , short , int , long , float , 和 double数据类型,因为它们在 C# 和 Java 中具有相同的签名性质和按位表示。
  • byte (Java, signed) 可能会出现签名问题对比byte (C#, unsigned) .值得庆幸的是,ProtocolBuffer 将自动处理这个 如果给定适当的 byte[] (Java or C#) .
  • 由于轻微的编码差异,字符串可以提供额外的乐趣。

关于c# - 读取从 C# 客户端发送的 ObjectInputStream 时为 "StreamCorruptedException: invalid stream header",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13186848/

25 4 0