简体   繁体   English

“ IOException:无效的流头:00010000”在C上将数据从C#主机获取到TCP上的Java客户端时

[英]“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.

相关问题 读取从C#客户端发送的ObjectInputStream时出现“StreamCorruptedException:invalid stream header” - “StreamCorruptedException: invalid stream header” when reading ObjectInputStream sent from C# client 在C#中检测客户端在TCP流上尚未接收到哪些数据 - Detect what data is (not yet) received by client on TCP stream in C# 在 .net c# 中使用 ssl Stream 从 Tcp.Client.Socket 发送数据 - Send data from Tcp.Client.Socket with ssl Stream in .net c# Java(客户端)和 C#(服务器)TCP 套接字。 和服务器从客户端读取无限的最后数据 - Java(client) and C#(Server) TCP Socket. and Server read infinite last data from client 从C#中的原始TCP流获取unicode字符串 - Getting a unicode string from a raw TCP stream in C# 如何通过Tcp将Java二进制流发送到C#? - How to send a binary stream from Java to C# via Tcp? C#ZipOutputStream从输出流中获取无效文件 - C# ZipOutputStream getting invalid files from output stream 如何在tcp / ip C#客户端中以十六进制打印传入的流数据? - How to print incoming stream data in Hex in tcp/ip C# client? 我能否确定在传入的TCP客户端流(在C#中)中排队了多少数据? - Can I determine how much data is queued up on an incoming TCP client stream (in C#)? C#服务器和Java客户端TCP - C# server and Java client TCP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM