P
P
Peter2018-08-28 15:55:27
C++ / C#
Peter, 2018-08-28 15:55:27

Why such a big difference in data volume?

Good afternoon.
It took me in a training project to send data from the client to the server and back. I wrote a simple semblance of a "package manager".

Class for writing data to a stream
public class DataWriter : BinaryWriter 
    {
        private MemoryStream _ms;
        private BinaryFormatter _bf;

        public DataWriter()
            :base()
        {
            _ms = new MemoryStream();
            _bf = new BinaryFormatter();
            OutStream = _ms;
        }

        /// <summary>
        /// Method for Objects.
        /// </summary>
        /// <param name="o"></param>
        public void WriteObject(object o)
        {
            _bf.Serialize(_ms, o);
        }

        public byte[] GetBytes()
        {
            Close();

            byte[] data = _ms.ToArray();

            return data;
        }
    }

Class for reading data from a stream
public class DataReader :BinaryReader
    {
        private BinaryFormatter _bf;

        public DataReader(byte [] data)
            :base(new MemoryStream(data))
        {
            _bf = new BinaryFormatter();
        }

        public T ReadObject<T>()
        {
            return (T)_bf.Deserialize(BaseStream);
        }
    }


I decided to test in a console application. Created a simple class.
Artist class
[Serializable]
    public class Artist
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string SongTitle { get; set; }
    }

Code to test work
static void Main(string[] args)
        {

        
            Console.Title = "Data Into Packets Lib. Tester";

            Artist artist = new Artist();
            artist.FirstName = "Michael";
            artist.LastName = "Jackson";
            artist.SongTitle = "Give In To Me";

            DataWriter dw = new DataWriter();
            dw.WriteObject(artist);

            byte[] data = dw.GetBytes();
            Console.WriteLine($"Values:\nName: {artist.FirstName}.\nLastName: {artist.LastName}.\nSong Title: {artist.SongTitle}");
            Console.WriteLine($"Class object size: {data.Length} bytes");

            dw = new DataWriter();
            dw.Write(artist.FirstName);
            dw.Write(artist.LastName);
            dw.Write(artist.SongTitle);

            data = dw.GetBytes();

            Console.WriteLine($"Simple variable size: {data.Length} bytes");
            Console.ReadKey();
        }
    }


And I was surprised by such a huge difference between sending an object and sending data in parts.
If you send the entire object, then the size is 240 bytes, when sending data in parts, the size is only 30 bytes.
For what reason is this happening? I understand that during serialization, there is a complete pass through the object graph, but not at the same price. The class is extremely simple. Or did I write something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Radjah, 2018-08-28
@Morpheus_God

Look at the packets using tcpdump or a similar sniffer, everything will immediately fall into place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question