0

I'm trying to develop client server application which can transfer Files and Strings from Client to Server. I'm new to TCP, sockets and server transactions.

Client side is written in Java and Server Side is Written in C#

what I'm trying to do in Server(C#) , get all the bytes and cast it to an Object, because I'm sending all the Objects from client side in Object type. then i wanted to cast to File or String

but in this line of code I'm getting an error

Object myObject = (Object)binForm.Deserialize(memStream);

Server side:-

private void ClientHandler(object client)
    {
        int bytesRead = 0;
        byte[] buffer = new byte[32];

        tcpClient = (TcpClient)client;
        clientStream = tcpClient.GetStream();

        Console.WriteLine("Client Handler Started!");

        while (true)
        {
            bytesRead = 0;

            try
            {
                Console.WriteLine("Server waiting for commands\n");

                MemoryStream memStream = new MemoryStream();
                BinaryFormatter binForm = new BinaryFormatter();


                while ((bytesRead = tcpClient.Client.Receive(buffer)) > 0)
                {

                    Console.WriteLine("bytes received :- " + bytesRead);

                    memStream.Write(buffer, 0, bytesRead);                        

                }
                try
                {

                    memStream.Position = 0;

                    Object myObject = (Object)binForm.Deserialize(memStream);
                    // After this cast it to String or File 

                }
                catch(Exception exp)
                {                       
                    Console.WriteLine(exp.ToString());
                }

            }
            catch
            {
                //a socket error has occured
                Console.WriteLine("a socket error has occured!!!");
                break;
            }
        }
6
  • What is the error you're seeing? Commented Aug 8, 2014 at 5:07
  • show the inner exception of catch ? Commented Aug 8, 2014 at 5:07
  • Exception is "End of Stream encountered before parsing was completed" Commented Aug 8, 2014 at 5:36
  • I've never actually tried this before, but I'm pretty sure that you can't use .NET BinaryFormatter to deserialize an object that was serialized using Java. See stackoverflow.com/questions/17801967/… Commented Aug 8, 2014 at 5:36
  • Please add to question your serializable object. Commented Aug 8, 2014 at 5:58

1 Answer 1

2

Binary formatter places a header on the binary stream which represents a .NET type. You can not use that to send 'object' meaning to a non .net client.

Instead you will have to serialise to XML or Json and provide a local model and deserialisation in your client.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.