[英]“IOException:invalid stream header: 00010000” While getting data from C# Host to Java Client on TCP
I am new to Sockets, Working on a project with C# host and Java/Android Client. 我是Sockets的新手,正在使用C#主机和Java / Android Client开发项目。
I need to get data from host to client continuously via TCP socket. 我需要通过TCP套接字连续从主机到客户端获取数据。 I am facing the same problem "invalid stream header" while fetching data with DataInputStream or ObjectInputStream.
使用DataInputStream或ObjectInputStream提取数据时,我遇到了同样的问题“无效的流头”。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using PacketAndroid;
namespace SampleForAndroid
{
public class TCPListener
{
public Socket SocketForClient;
public void StartListening()
{
try
{
TcpListener Listener = new TcpListener(5155);
Listener.Start();
SocketForClient = Listener.AcceptSocket();
if (SocketForClient != null && SocketForClient.Connected)
{
int i = 1;
while (SocketForClient.Connected && i<2)
{
NetworkStream NS = new NetworkStream(SocketForClient);
BinaryFormatter BF = new BinaryFormatter();
TCPPayLoad PayLoad = new TCPPayLoad();
Console.WriteLine(PayLoad.ToString());
NS.Flush();
BF.Serialize(NS, PayLoad);
i = 2;
}
}
}
catch (SocketException EX)
{
}
}
static void Main(string[] args)
{
TCPListener List = new TCPListener();
System.Threading.Thread TcpThread=new System.Threading.Thread(new System.Threading.ThreadStart(List.StartListening));
TcpThread.Start();
Console.ReadLine();
}
}
}
and the java client: 和Java客户端:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ObjectReceiver {
public static void main(String argv[]) {
fun_0();
}
@SuppressWarnings("unused")
private static void fun_0() {
ObjectInputStream ois = null;
Socket socket = null;
try {
// open a socket connection
socket = new Socket("127.0.0.1", 5155);
System.out.println("Connecting...");
ObjectInputStream din = new ObjectInputStream(socket.getInputStream());
int size = din.readInt();
System.out.println(din.read());
byte[] bytes = new byte[size];
int readBytes = din.read(bytes);
System.out.println(readBytes);
Object object = din.readObject();
TcpPayload payload = (TcpPayload) object;
System.out.println(payload.toString());
} catch (IOException e) {
System.out.println("IOException:" + e.getMessage());
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
}
}
The object TcpPayload is an instance of a class which has only preemptive data types and a string. 对象TcpPayload是仅具有抢先数据类型和字符串的类的实例。 Both java and C# has the same object with the implementation of serialization.
Java和C#在序列化的实现中具有相同的对象。
Please tell me about the solution, I have the only option to tune the code over client side . 请告诉我有关解决方案的信息,我唯一的选择是通过客户端调整代码。 The host is already working fine with iOS client.
主机已经可以与iOS客户端正常工作。
Thanks in advance :) 提前致谢 :)
Unfortunately binary serialization is not compatible between C# (.NET) and Java. 不幸的是,二进制序列化在C#(.NET)和Java之间不兼容。 I actually remember trying the same thing as you a while back and failing.
我实际上记得一段时间后尝试过与您相同的尝试,但失败了。
I'm afraid you will need to find an alternative encoding method, such as perhaps a custom XML or JSON serialization (which in fact enables web services to work across platforms). 恐怕您将需要找到一种替代的编码方法,例如自定义XML或JSON序列化(实际上,这使Web服务可以跨平台工作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.