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 840957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:42:47+00:00 2026-05-15T05:42:47+00:00

I have the following C struct from the source code of a server, and

  • 0

I have the following C struct from the source code of a server, and many similar:

// preprocessing magic: 1-byte alignment

typedef struct AUTH_LOGON_CHALLENGE_C
{
    // 4 byte header
    uint8   cmd;
    uint8   error;      
    uint16  size;       

    // 30 bytes
    uint8   gamename[4];
    uint8   version1;
    uint8   version2;
    uint8   version3;
    uint16  build;
    uint8   platform[4];
    uint8   os[4];
    uint8   country[4];
    uint32  timezone_bias;
    uint32  ip;
    uint8   I_len;

    // I_len bytes
    uint8   I[1];
} sAuthLogonChallenge_C;

// usage (the actual code that will read my packets): 
sAuthLogonChallenge_C *ch = (sAuthLogonChallenge_C*)&buf[0]; // where buf is a raw byte array

These are TCP packets, and I need to implement something that emits and reads them in C#. What’s the cleanest way to do this?

My current approach involves

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct foo { ... }

and a lot of fixed statements to read and write it, but it feels really clunky, and since the packet itself is not fixed length, I don’t feel comfortable using it. Also, it’s a lot of work.

However, it does describe the data structure nicely, and the protocol may change over time, so this may be ideal for maintenance.

What are my options? Would it be easier to just write it in C++ and use some .NET magic to use that?

Clarification: I also need to handle endian issues and null-padded strings.

  • 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-15T05:42:47+00:00Added an answer on May 15, 2026 at 5:42 am

    I would create a native C# class to represent the packet and its data (not one that attempts to match the wire format, necessarily), and pass it a BinaryReader in the constructor. Have it read its data in appropriate chunks from the data stream:

    public class LogonChallenge
    {
        public LogonChallenge(BinaryReader data)
        {
            // header
            this.Cmd = data.ReadByte();
            this.Error = data.ReadByte();
            this.Size = data.ReadUInt16();
    
            // etc
        }
    }
    

    If you have multiple packet types that share a common header or other leading fields, then you can use inheritance to avoid repetition. The BasePacket class might read and populate the header fields, and the LogonChallenge class would inherit from BasePacket and begin reading the challenge fields after calling the base constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

g++ 4.4.3 I have the following structure from a API guide: typedef struct NETWORK_INFO
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
I have defined the following struct in C: typedef struct point{ float x; float
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
Say I have the following code: struct date { int day; int month; int
Let's say I have the following code: struct mytype { ~mytype() { /* do
I have the following code in my user control: <asp:LinqDataSource ID=myLinqDataSource runat=server AutoSort=true ContextTypeName=MyDBContext
I have the following code: // ---- third party library code ---- struct Point
Lets say, I have the following text file: Listing 1: Endianess=little AddressModel=32 typedef struct{
I have the following C code to copy a linked list(taken from Stanford CS

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.