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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:13:32+00:00 2026-05-14T07:13:32+00:00

I’m writing a client for a server program written in C++. As is not

  • 0

I’m writing a client for a server program written in C++. As is not unusual, all the networking protocol is in a format where packets can be easily memcopied into/out of a C++ structure (1 byte packet code, then different arrangements per packet type).

I could do the same thing in C#, but is there an easier way, especially considering lots of the data is fixed-length char arrays that I want to play with as strings? Or should I just suck it up and convert types as needed? I’ve looked at using the ISerializable interface, but it doesnt look as low level as is required.

  • 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-14T07:13:32+00:00Added an answer on May 14, 2026 at 7:13 am

    I wrote a post on this in 2004 which covers some of the options available for converting a binary stream to a .NET memory structure. I reposted it on my new blog since the old blog site no longer exists.

    http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.html

    Basically you have three options

    1. Use C++ style memory pointers in C# which requires the /unsafe switch
    2. Use the .NET marshaling to allocate an unmanaged block of memory, copy the bytes to the unmanaged memory and then use Marshal.PtrToStructure to marshal the data back to the managed heap mapping it into your structure.
    3. Use a BinaryReader to manualy read the byte stream and pack the data into the structure. Personally, this was my preferred option.

    When you consider the options you should also take into account how the byte ordering might affect you.

    As an example I will use the IP header as an example, since at the time of the post I was working with Raw TCP packets.

    You need to define your .NET structure that the binary data will be mapped to. For example the IP Header looks like the following.

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    struct IpHeader
    {  
      public byte VerLen;  
      public byte TOS;  
      public short TotalLength;   
      public short ID;  
      public short Offset;  
      public byte TTL;  
      public byte Protocol;  
      public short Checksum;  
      public int SrcAddr;  
      public int DestAddr;
    }
    

    Note that the StructLayout attribute is only required for first two options, and of course you will need to set the packing as appropriate for the structure that is being serialized from the server.

    So in C/C++, given a pointer to a block of memory that contains the data bytes that map to the C/C++ structure you can use the following bit of code to view the block of data as a structure piece of memory, where packet is a byte* to the memory.

    IpHeader *pHeader = (IpHeader*)packet;
    

    Doing the same is C# using /unsafe option and the struct defined above you count use the following code.

    IpHeader iphdr;
    unsafe
    {  
      fixed ( byte *pData = packet)  
      {    
        iphdr = *(IpHeader*)pData;  
      }
    }
    //Use iphdr...
    

    The marshaling option would look like the following

    IntPtr pIP = Marshal.AllocHGlobal( len );
    Marshal.Copy( packet, 0, pIP, len );
    iphdr = (IpHeader)Marshal.PtrToStructure( pIP, typeof(IpHeader) );
    Marshal.FreeHGlobal( pIP );
    

    And finally you can use the BinaryReader to do this entirely in managed code.

    MemoryStream stm = new MemoryStream( packet, 0, len );
    BinaryReader rdr = new BinaryReader( stm );
    
    iphdr.VerLen = rdr.ReadByte();
    iphdr.TOS = rdr.ReadByte();
    iphdr.TotalLength = rdr.ReadInt16();
    iphdr.ID = rdr.ReadInt16();
    iphdr.Offset = rdr.ReadInt16();
    iphdr.TTL = rdr.ReadByte();
    iphdr.Protocol = rdr.ReadByte();
    iphdr.Checksum = rdr.ReadInt16();
    iphdr.SrcAddr = rdr.ReadInt32();
    iphdr.DestAddr = rdr.ReadInt32();
    

    As I mentioned earlier, you might need to consider byte ordering. For example, the above code is not quite correct because the IpHeader does not use the same byte order as is assumed by ReadInt16. ReadInt32 etc. Resolving the issue with the above solution is as simple as using IPAddress.NetworkToHostOrder.

    iphdr.VerLen = rdr.ReadByte();
    iphdr.TOS = rdr.ReadByte();
    iphdr.TotalLength = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
    iphdr.ID = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
    iphdr.Offset = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
    iphdr.TTL = rdr.ReadByte();
    iphdr.Protocol = rdr.ReadByte();
    iphdr.Checksum = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
    iphdr.SrcAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());
    iphdr.DestAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 405k
  • Answers 405k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Potentially you could use the keyboard layout suggested here but… May 15, 2026 at 5:44 am
  • Editorial Team
    Editorial Team added an answer You can approximate the derivative by looking at the gradient… May 15, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer #!/usr/bin/ruby require "rubygems" require "fastercsv" scannedIPs = Hash.new(0) count =… May 15, 2026 at 5:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.