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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:57:29+00:00 2026-06-19T02:57:29+00:00

I want to be able to receive some binary data over TCP/IP which consists

  • 0

I want to be able to receive some binary data over TCP/IP which consists of a known structure.
I don’t want to inter-operate with C or C++, so solutions that work for this case didn’t help me.
Unfortunately the other side cannot change the protocol.
The problem should also arise when I would try to read a binary file with a given format.

I also checked BinaryFormatter and similar but they use their own format which is not acceptable for me.

Here is a sample set of structs. I’d like to be able to reconstruct nested arrays (of known length) of structs. With the current code I get an exception:

Could not load type ‘NestedStruct’ from assembly ‘…’ because it
contains an object field at offset 2 that is incorrectly aligned or
overlapped by a non-object field.

I want to be able to send/receive (or read/write) instances of struct MainStruct.

    [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 244, CharSet = CharSet.Ansi)]
    public struct NestedStruct
    {

        [FieldOffset(0)]
        public Int16 someInt;

        [FieldOffset(2), MarshalAs(UnmanagedType.ByValArray, SizeConst = 242)]
        public Byte[] characterArray; // an array of fixed length 242

    }

    [StructLayout(LayoutKind.Explicit)]
    public struct OtherNestedStruct
    {
        [FieldOffset(0)]
        public Int16 someInt;
        [FieldOffset(2)]
        public Int16 someOtherInt;

    }


    [StructLayout(LayoutKind.Explicit)]
    public struct MainStruct
    {
        [FieldOffset(0)]
        public double someDouble;
        [FieldOffset(8)]
        public NestedStruct nestedContent;
        [FieldOffset(8 + 244)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 13 * 4)]
        public OtherNestedStruct[] arrayOfStruct; // fixed array length 13

    }

UPDATE:

Here is my latest version:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct NestedStruct
    {

        public Int16 someInt;

        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 242)]
        public Byte[] characterArray; // an array of fixed length 242

    }

    [StructLayout(LayoutKind.Sequential , Pack=1)]
    public struct OtherNestedStruct
    {
        public Int16 someInt;
        public Int16 someOtherInt;

    }


    [StructLayout(LayoutKind.Sequential, Pack=1)]
    public struct MainStruct
    {

        public double someDouble;

        public NestedStruct nestedContent;

        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct,  SizeConst = 13)]
        public OtherNestedStruct[] arrayOfStruct; // fixed array length 13

    }
  • 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-06-19T02:57:30+00:00Added an answer on June 19, 2026 at 2:57 am

    you must specify the ArraySubType

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct NestedStruct
        {
            public Int16 someInt;
            [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 242)]
            public Byte[] characterArray; // an array of fixed length 242
        }
    
         [StructLayout(LayoutKind.Sequential, Pack = 1)]
         public struct OtherNestedStruct
         {
             public Int16 someInt;
             public Int16 someOtherInt;
    
         }
    
    
         [StructLayout(LayoutKind.Sequential, Pack = 1)]
         public struct MainStruct
         {
             public double someDouble;
             public NestedStruct nestedContent;
             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 13 * 4)]
             public OtherNestedStruct[] arrayOfStruct; // fixed array length 13
    
        }  
    
        static void Main(string[] args)
        {
            var x = Marshal.SizeOf(typeof(MainStruct));
            //x == 460
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have service that will receive some data in 'json', i don't know which
I have this TCP server that I want to be able to receive numbers
I want to attempt to calculate how much data (bytes) I send/receive over the
I have a form.Panel in which I show some data. I receive the data
I want the wrapper my_function to be able to receive either a class or
I want to be able to replicate only the folder structure (not the contents)
I want to be able to hide a selector if it contains any data.
I have 5 processes p1,p2,...,p5 where I want to write some data to stdin
I want to be able to get a list or some sort of set
I have some XML data that I want to transmit to a web server

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.