Other [SOLVED]Question .Net Framework implementation of B4XSerializator.

jahswant

Well-Known Member
Licensed User
Longtime User
Hey I'm a little blocked with .Net Framework implementation of B4XSerializator. I have a B4J Server communicates with my B4A apps all is good. But I want to send and recieve some data from the C#.NET client application that cannot be implemented to B4J for the moment. I send an object very well from the .Net App with this code.


C#:
            Dictionary<object, object> Dict = new Dictionary<object, object>();
            Dict.Add("id", "HELLO1");
            Dict.Add("msg", "MESSAGE1");
            Dict.Add("senderid", "HELLO SENDER");
            Dict.Add("receiverid", "ROOM1");

            B4XType B4XMessage = new B4XType("Message", Dict);

            B4XSerializator B4XSer = new B4XSerializator();

            //---create a TCPClient object at the IP and port no.---
            TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
            NetworkStream nwStream = client.GetStream();
            byte[] bytesToSend = B4XSer.ConvertObjectToBytes(B4XMessage);


            int BufferLen = bytesToSend.Length;
            byte[] Prefix = BitConverter.GetBytes(BufferLen);

            // ---send the text---
            if (nwStream.CanWrite)
            {
                nwStream.Write(Prefix, 0, Prefix.Length);
                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
                nwStream.Flush();
            }

And I recieve wit this code.

B4X:
    Type Message (id As String , senderid As String , receiverid As String , msg As String )
    Dim receivedObject As Object = Serializator.ConvertBytesToObject(Buffer)
    If receivedObject Is Message Then
        Log(receivedObject)
End if

Now when I try sending the same Object to the C# application I can not get the object back. This is the code.
B4X:
Dim Message1 As Message
        Message1.Initialize
        Message1.id = 1
        Message1.receiverid = "Hello World"
        Message1.senderid = "Hello Tense"
        Message1.msg = "Message"
        Dim RCreateRemoteMessage() As Byte =  Serializator.ConvertObjectToBytes(Message1)
        SendCommand(RCreateRemoteMessage,Ast)

I have been struggling for nearly a week with this issue. I want to get the dictionary I sent back to me. I've tried all many code and I'm confused now.
This is the latest code.
C#:
                byte[] m_Bytes = ReadToEnd(nwStream);

                object m = B4XSer.ConvertBytesToObject(m_Bytes);

                Dictionary<Object, Object> d = (Dictionary<Object, Object>)m;

                Console.WriteLine("MESSAGE");

                foreach (KeyValuePair<Object, Object> author in d)
                {
                    Console.WriteLine("Key: {0}, Value: {1}",
                        d.Keys.ToString(), d.Values.ToString());
                }


        public static byte[] ReadToEnd(System.IO.Stream stream)
        {
            long originalPosition = 0;

            if (stream.CanSeek)
            {
                originalPosition = stream.Position;
                stream.Position = 0;
            }

            try
            {
                byte[] readBuffer = new byte[4096];

                int totalBytesRead = 0;
                int bytesRead;

                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;

                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }

                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                if (stream.CanSeek)
                {
                    stream.Position = originalPosition;
                }
            }
        }

What is wrong with these code ? Please help me solve. I saw this post with same problem but don't understand the solution at this level.

C#:
StreamExtensions.ToByteArray(result)
Is StreamExtensions a custom class or .Net Class ?
 

jahswant

Well-Known Member
Licensed User
Longtime User
Why is this complicated code needed? You can use BinaryReader to read the whole stream (with HttpWebResponse.ContentLength).
It's a Win Forms Application. I not really familiar with networking in .Net apps.
 
Upvote 0

jahswant

Well-Known Member
Licensed User
Longtime User
Please @Erel provide me with a simple example on how to receive a b4x object in c# .net win forms application. I will be very grateful to you.
 
Upvote 0
Top