Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:26:38+00:00 2026-06-02T16:26:38+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:26:39+00:00Added an answer on June 2, 2026 at 4:26 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a client/server application in C#, and I need to get Asynchronous
I am working on a client-server project and need to implement a logic where
I am working on a project and have a question about Java sockets. The
When creating a Socket in Java: new Socket(host, port); The Socket constructor will try
I'm using this kind of code for my TCP/IP connection: sock = new Socket(host,
I am trying to create a new TCP socket in Android emulator. Below is
Okay, so I'm working on a project where I use a Java program to
Me and a group of friends are working on a project in Java, and
i'm working in a project where i need to read some values and send
I am working with sockets in C (linux) and I need to send a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.