“IOException:invalid stream header: 00010000” While getting data from C# Host to Java Client on TCP

别来无恙 提交于 2019-12-13 17:55:57

问题


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

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

and the java client:

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. Both java and C# has the same object with the implementation of serialization.

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.

Thanks in advance :)


回答1:


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.

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



来源:https://stackoverflow.com/questions/10302570/ioexceptioninvalid-stream-header-00010000-while-getting-data-from-c-sharp-ho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!