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

  • SEARCH
  • Home
  • 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 7446595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:21:31+00:00 2026-05-29T12:21:31+00:00

Well I want to send commands and data between client and server. I have

  • 0

Well I want to send commands and data between client and server.

I have three projects:

  • Client
  • Server
  • Common – here I put common classes and network abstraction layer

I am using following data structures for communications between client and server

public class Packet<T>
{
        public string Name { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public PacketType PacketType { get; set; }
        public T Container { get; set; }

        public Packet()
        {
        }

        public Packet(string name, PacketType packetType, T container)
        {
            Name = name;
            PacketType = packetType;
            Container = container;
        }
    }

    public enum PacketType
    {
        Command,
        Data
    }

If I need to send information about files I just create a packet with the necessary structure CreatePacket<FilesInfo>(filesInfo) and then serialize it and send it to client\server.

But how I can deserialize data on receiving side? I don’t know what is object type the packet is. Is there any other way or library or something to solve my problem? Also I don’t want to use WCF because my app should work on machines with .NET 2.0 installed.

  • 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-05-29T12:21:32+00:00Added an answer on May 29, 2026 at 12:21 pm

    Both chrfin and haiyyu have the same and a good idea. Below is the minor twist of using a Packet Base class to hold your type data on construction. You serialize a Packet< T > and deserialize to a Packet. Use is pretty simple then. Still the trick as already mentioned is to make the Type easily accessible.

    class Program
    {
        static void Main(string[] args)
        {
            var pack = new Packet<int>() { Payload = 13 };
            var serializedData = pack.Serialize();
            var deserializedData = Packet.Deserialize(serializedData);
            Console.WriteLine("The payload type is:" + deserializedData.PayloadType.Name);
            Console.WriteLine("the payload is: " + deserializedData.Payload);
            Console.ReadLine();
        }
    }
    
    
    
    [Serializable]
    public class Packet
    {
        public Type PayloadType { get; protected set; }
        public object Payload { get; protected set; }
        public static Packet Deserialize(byte[] bytes)
        {
            return (Packet)(new BinaryFormatter()).Deserialize(new MemoryStream(bytes));
        }
    }
    
    [Serializable]
    class Packet<T> : Packet
    {
        public Packet()
        {
            PayloadType = typeof(T);
        }
        public new T Payload 
        {
            get { return (T)base.Payload; } 
            set { base.Payload = value; } 
        }
    
        public override string ToString()
        {
            return "[Packet]" + Payload.ToString();
        }
    
        public byte[] Serialize()
        {
            MemoryStream m = new MemoryStream();
            (new BinaryFormatter()).Serialize(m, this);
            return m.ToArray();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well i want to transfer different chunks of data to client machine , by
I have two C# programs and I want to send some data back and
I have a well formed XHTML page. I want to find the destination URL
Well, the title say it all. I have a ruby script I want running
Starting a new rails project and we have a well-thought-out color palette, and want
I want to send some information from PHP to Java. Why? Because I have
I have a server and a client app, my server listens on port 10015
My code is work well but i want to send Item and Mode in
I want to send files as well as some other information through sockets. I
I'm writing a TCP client using boost::asio . I want to send an array

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.