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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:57:05+00:00 2026-06-05T04:57:05+00:00

I have a C++ struct below: struct CUSTOM_DATA { int id; u_short port; unsigned

  • 0

I have a C++ struct below:

struct CUSTOM_DATA {
   int id;
   u_short port;
   unsigned long ip;
} custom_data;

How can i convert it to C# struct, serialize it and send via tcp socket?

Thanks!

upd

So C# code will be?

[StructLayout(LayoutKind.Sequential)]
public struct CustomData
{
 public int id;
 public ushort port;
 public uint ip;
}

public void Send()
{
 CustomData d = new CustomData();
 d.id = 12;
 d.port = 1000;
 d.ip = BitConverter.ToUInt32(IPAddress.Any.GetAddressBytes(), 0);
 IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(d));
 Marshal.StructureToPtr(d, pointer, false);
 byte[] data_to_send = new byte[Marshal.SizeOf(d)];
 Marshal.Copy(pointer, data_to_send, 0, data_to_send.Length);
 client.GetStream().Write(data_to_send, 0, data_to_send.Length);
}
  • 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-05T04:57:06+00:00Added an answer on June 5, 2026 at 4:57 am

    The C# version of this struct would be:

    [StructLayout(LayoutKind.Sequential)]
    public struct CustomData
    {
        public int id;
        public ushort port;
        public uint ip;
    }
    

    As for sending this via a socket, you can just send the binary data directly. The Marshal class has methods for getting a pointer (IntPtr) from the structure and copying into a byte array.

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

Sidebar

Related Questions

Long story short, I have a struct (see below) that contains exactly one field:
I have a struct as below typedef struct _someStruct { int V1; char V2[10];
I have the below struct: struct node { float val; int count; } I
For eg. I have an array of structs 'a' as below: struct mystruct{ int
I have the STRUCT1 Structure declared as below typedef struct struct1 { short int
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have a struct which has some vectors as members: struct my_struct { std::vector<int>
I have this struct typedef struct { int numberPipes; // | int numberAmpersands; //
I have a type defined like this: struct DynTab_s { int object_count; //other fields
I have the following struct: typedef struct { int someArray[3][2]; int someVar; } myStruct;

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.