“IOException:无效的流标头:00010000";在 TCP 上从 C# 主机到 Java 客户端获取数据时 [英] "IOException:invalid stream header: 00010000" While getting data from C# Host to Java Client on TCP

查看:28
本文介绍了“IOException:无效的流标头:00010000";在 TCP 上从 C# 主机到 Java 客户端获取数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Sockets 的新手,使用 C# 主机和 Java/Android 客户端处理项目.

I am new to Sockets, Working on a project with C# host and Java/Android Client.

我需要通过 TCP 套接字从主机到客户端连续获取数据.我在使用 DataInputStream 或 ObjectInputStream 获取数据时遇到了同样的问题无效的流标头".

I need to get data from host to client continuously via TCP socket. I am facing the same problem "invalid stream header" while fetching data with DataInputStream or 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();
       }
     }
}

和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());
        }
    }
}

对象TcpPayload 是一个只有抢占数据类型和字符串的类的实例.java和C#都是同一个对象,实现了序列化.

The object TcpPayload is an instance of a class which has only preemptive data types and a string. Both java and C# has the same object with the implementation of serialization.

请告诉我解决方案,我只有通过客户端调整代码的选择.主机已经可以正常使用 iOS 客户端了.

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.

提前致谢:)

推荐答案

不幸的是,二进制序列化在 C# (.NET) 和 Java 之间不兼容.我实际上记得前一段时间和你一样尝试过,但失败了.

Unfortunately binary serialization is not compatible between C# (.NET) and Java. I actually remember trying the same thing as you a while back and failing.

恐怕您需要找到替代编码方法,例如自定义 XML 或 JSON 序列化(这实际上使 Web 服务能够跨平台工作).

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

这篇关于“IOException:无效的流标头:00010000";在 TCP 上从 C# 主机到 Java 客户端获取数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆