Transfer complex objects using TCP

I have a client server application in which I need to transfer a user-defined object from client to server using a TCP connection. My object has the following structure:

class Conversation
{
    private string convName, convOwner;
    public ArrayList convUsers;

    public string getConvName()
    {
       return this.convName;
    }
    public string getConvOwner()
    {
       return this.convOwner;
    }
}

Please help me how to pass this object from the client and de-serialize it again to the corresponding object on the server side.

+3
source share
3 answers

As already mentioned, you must make your object serializable. Once you have done this with an attribute Serializable, you can use the famous BinaryFormatterone to convert your object to an array byte.

BinaryFormatter, . :

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class SerializationUtils
{
    public static byte[] SerializeToByteArray(object request)
    {
        byte[] result;
        BinaryFormatter serializer = new BinaryFormatter();
        using (MemoryStream memStream = new MemoryStream())
        {
            serializer.Serialize(memStream, request);
            result = memStream.GetBuffer();
        }
        return result;
    }

    public static T DeserializeFromByteArray<T>(byte[] buffer)
    {
        BinaryFormatter deserializer = new BinaryFormatter();
        using (MemoryStream memStream = new MemoryStream(buffer))
        {
            object newobj = deserializer.Deserialize(memStream);
            return (T)newobj;
        }
    }
}

, . , , , . , Serializable:

using System;
using System.Collections;

[Serializable]
public class Conversation
{
    public Conversation(string convName, string convOwner)
    {
        this.convName = convName;
        this.convOwner = convOwner;
    }

    public Conversation()
    {
    }

    private string convName, convOwner;
    public ArrayList convUsers;

    public string getConvName()
    {
        return this.convName;
    }
    public string getConvOwner()
    {
        return this.convOwner;
    }
}

, :

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Capishi
{
    [Serializable]
    public class Conversation
    {
        public Conversation(string convName, string convOwner)
        {
            this.convName = convName;
            this.convOwner = convOwner;
        }

        public Conversation()
        {
        }

        private string convName, convOwner;
        public ArrayList convUsers;

        public string getConvName()
        {
            return this.convName;
        }
        public string getConvOwner()
        {
            return this.convOwner;
        }
    }

    public class SerializationUtils
    {
        public static byte[] SerializeToByteArray(object request)
        {
            byte[] result;
            BinaryFormatter serializer = new BinaryFormatter();
            using (MemoryStream memStream = new MemoryStream())
            {
                serializer.Serialize(memStream, request);
                result = memStream.GetBuffer();
            }
            return result;
        }

        public static T DeserializeFromByteArray<T>(byte[] buffer)
        {
            BinaryFormatter deserializer = new BinaryFormatter();
            using (MemoryStream memStream = new MemoryStream(buffer))
            {
                object newobj = deserializer.Deserialize(memStream);
                return (T)newobj;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // create and initialize a conversation object
            var convName = "Capishi";
            var convOwner = "Ice Cream";
            Conversation myConversation = new Conversation(convName, convOwner);
            myConversation.convUsers = new ArrayList();
            myConversation.convUsers.Add("Ron Klein");
            myConversation.convUsers.Add("Rakesh K");

            // serialize to a byte array
            byte[] data = SerializationUtils.SerializeToByteArray(myConversation);

            // print the resulting byte array if you want
            // PrintArray(data);

            // deserialize the object (on the other side of the communication
            Conversation otherConversation = SerializationUtils.DeserializeFromByteArray<Conversation>(data);

            // let see if all of the members are really there
            Console.WriteLine("*** start output ***");
            Console.WriteLine("otherConversation.getConvName() = " + otherConversation.getConvName());
            Console.WriteLine("otherConversation.getConvOwner() = " + otherConversation.getConvOwner());
            Console.WriteLine("otherConversation.convUsers:");
            foreach (object item in otherConversation.convUsers)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("*** done output ***");

            // wait before close
            Console.ReadLine();

        }

        /// <summary>
        /// just a helper function to dump an array to the console output
        /// </summary>
        /// <param name="data"></param>
        private static void PrintArray(byte[] data)
        {
            for (int i = 0; i < data.Length; i++)
            {
                Console.Write("{0:000}", data[i]);
                if (i < data.Length - 1)
                    Console.Write(", ");
            }
            Console.WriteLine();
        }
    }
}

:

*** start output ***
otherConversation.getConvName() = Capishi
otherConversation.getConvOwner() = Ice Cream
otherConversation.convUsers:
Ron Klein
Rakesh K
*** done output ***

:

List ArrayList, .NET 1. *.

+4

, DataContract , ​​ WCF, , .

:

[DataContract]
class Conversation
{
    private string convName, convOwner;
    public ArrayList convUsers;

    [DataMember]
    public string ConvName
    {
       get { return this.convName; }
    }
    [DataMember]
    public string ConvOwner
    {
       get { return this.convOwner; }
    }
}
+4

You need to make your object serializable .

+2
source

Source: https://habr.com/ru/post/1740725/


All Articles