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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:42:45+00:00 2026-05-24T22:42:45+00:00

I am working on a TCP-Communication with another person’s program. For proper Communication he

  • 0

I am working on a TCP-Communication with another person’s program. For proper Communication he defined a Header-Struct for the Messages sent via TCP. It is defined something as following:

typedef struct
{
    uint16_t  number1;                 
    uint16_t  number2;
    char      name1[64];
    char      name2[64];
    uint32_t  size;
} headerStruct;

The struct is somehow filled, most importantly headerStruct.size sets the number of bytes following the header and is the size of the messages to be sent.

I now tried to serialize the header like this:

headerStruct sourceHeader;
// ... filling Header-Struct here ...

array<Byte>^ result = gcnew array<Byte>(520);  // I tried sizeof(headerStruct) instead of '520', but then I get errors
double allreadyCopied = 0;

// serialize uint16_t 'number1'
array<Byte>^ array1 = BitConverter::GetBytes( sourceHeader.number1 );
Array::Copy(array1 , 0, result, allreadyCopied, array1 ->Length);
allreadyCopied += array1 ->Length;

// serialize uint16_t 'number2'
array<Byte>^ array2 = BitConverter::GetBytes( sourceHeader.number2 );
Array::Copy(array2 , 0, result, allreadyCopied, array2->Length);
allreadyCopied += array2->Length;

// serialize char-Array 'name1'    
for (int i = 0; i < sizeof(sourceHeader.name1); i++) 
{
    array<Byte>^ arrayName1 = BitConverter::GetBytes( sourceHeader.name1[i] );
    Array::Copy(arrayName1 , 0, result, allreadyCopied, arrayName1->Length);
    allreadyCopied += arrayName1->Length;
}

// serialize char-Array 'name2'       
for (int i = 0; i < sizeof(sourceHeader.name2); i++) 
{
    array<Byte>^ arrayName2 = BitConverter::GetBytes( sourceHeader.name2[i] );
    Array::Copy(arrayName2 , 0, result, allreadyCopied, arrayName2->Length);
    allreadyCopied += arrayName2->Length;
}

// serialize uint32_t 'size'        
array<Byte>^ arraySize = BitConverter::GetBytes( sourceHeader.size);
Array::Copy(arraySize, 0, result, allreadyCopied, arraySize->Length);
allreadyCopied += arraySize->Length;

Now this seems to work for me, BUT:

  1. I tried using sizeof(headerStruct) instead of the hardcoded value 520 for the size if the result-array. I just “calculated” it with the variable allreadyCopied. But why are they different? Where’s my error here?

  2. This method is surely not very elegant. Is there a better way to do this? I tried the Marshal-Class but did not manage to get it working!

  3. Somehow it feels like this approach is wrong. Is this the right way or am I completely wrong here?

Any help is appreciated a lot! Thanks!

Edit:

OK just checked:

sizeof(headerStruct);       // = 136;
sizeof(char);               // = 1;
sizeof(sourceHeader.name1); // = 64;

BUT

arrayName1->Length;         // = 4;

Why? Very confusing: MSDN (link here) says that the returned Array should be of length 2, not 4.

  • 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-24T22:42:46+00:00Added an answer on May 24, 2026 at 10:42 pm

    OK, I googled a lot and read some articles on MSDN. I am now doing it like this:

    headerStruct sourceHeader;
    // ... filling Header-Struct here ..
    
    // First Create all arrays, for every element of the struct one Array
    array< Byte >^ array1 = gcnew array< Byte >(sizeof(sourceHeader.number1));
    array< Byte >^ array2 = gcnew array< Byte >(sizeof(sourceHeader.number2));
    array< Byte >^ arrayName1 = gcnew array< Byte >(sizeof(sourceHeader.name1));
    array< Byte >^ arrayName2 = gcnew array< Byte >(sizeof(sourceHeader.name2));
    array< Byte >^ arraySize = gcnew array< Byte >(sizeof(sourceHeader.size));
    
    // Let the Marshall Class copy those native Types (by casting there Pointers to a IntPtr)
    Marshal::Copy( (IntPtr)(&sourceHeader.number1), array1, 0, sizeof(sourceHeader.number1) );
    Marshal::Copy( (IntPtr)(&sourceHeader.number2), array2, 0, sizeof(sourceHeader.number2) );
    Marshal::Copy( (IntPtr)(&sourceHeader.name1), arrayName1, 0, sizeof(sourceHeader.name1) );
    Marshal::Copy( (IntPtr)(&sourceHeader.name2), arrayName2, 0, sizeof(sourceHeader.name2) );
    Marshal::Copy( (IntPtr)(&sourceHeader.size), arraySize, 0, sizeof(sourceHeader.size) );
    
    // Next we create the Array in Which all Data shall be copied alltogether
    array<Byte>^ allInOne = gcnew array<Byte>(sizeof(headerStruct));
    int allreadyCopied = 0;
    
    // Copy those arrays into the "Big" Array (watch out for the right order!)
    Array::Copy(array1, 0, allInOne, allreadyCopied, array1->Length);
    allreadyCopied += array1->Length;
    Array::Copy(array2, 0, allInOne, allreadyCopied, array2->Length);
    allreadyCopied += array2->Length;
    Array::Copy(arrayName1, 0, allInOne, allreadyCopied, arrayName1->Length);
    allreadyCopied += arrayName1->Length;
    Array::Copy(arrayName2, 0, allInOne, allreadyCopied, arrayName2->Length);
    allreadyCopied += arrayName2->Length;
    Array::Copy(arraySize, 0, allInOne, allreadyCopied, arraySize->Length);
    allreadyCopied += arraySize->Length;
    
    // Now let's Test if it worked correctly by converting back the ByteArray to a Struct
    pin_ptr<unsigned char> p1 = &allInOne[0];
    unsigned char* p2 = p1;
    headerStruct myHeader = *reinterpret_cast<headerStruct*>(p2);
    

    Works for me 🙂 Hope this approach is OK the way I do it.
    I used the Infos and Examples on following sites for this solution:

    1. MSDN Article 1
    2. MSDN Article 2

    This should work for most Custom-Structs using native types, right? Thanks @ Hans Passant for pointing me into the right direction 🙂

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

Sidebar

Related Questions

I am currently working on a program which sniffs TCP packets being sent and
I'm working on a TCP based application that processes bitpacked messages, meaning: The messages
I was working with a TCP server and something ultra weird came up. When
I am working on an application that communicates via TCP/IP with an external device
I'm working on a WCF service that will be communicating over net.tcp to n
I'm wanting to get a WCF-over-TCP service working. I was having some problems with
I am working on an embedded TCP/IP4 stack and HTTP/SNMP/SMTP stuff. It functionally works
I'm working in a project for the iPad, it is a small program and
I have win32 application in which winsock is used for TCP/IP communication. I am
I am using IO::Socket::INET to create inter-process communication in my program. I need to

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.