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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:40:18+00:00 2026-06-09T19:40:18+00:00

I need to stream data to a TCP-server and havnt done it before. The

  • 0

I need to stream data to a TCP-server and havnt done it before. The data should be in binary frame format. I googled and found this tutorial which I think describes what I need to do:

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=vs.71).aspx

But I dont know how to create the data in the format needed so I can send it in the correct way. It should be in this format:

Field|Offset|    Type  | size(octets)
id   |  0   |unsign int|      1
name |  1   |Byte Array|      40
grade|  41  |sign float|      8 

Example:

Field| Data | InBytes  |
id   |  133 | 133      |
name |  247 | 247 0    |
grade|  0   | 0        |

Whats the data type I should store the int, byte array, float in and where do I specify offsets and size and finally how should it be sent to the server (in the example they only send a byte array).

A C#-example of how to send data like this using the code in the link provided above would be appreciated.

  • 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-09T19:40:20+00:00Added an answer on June 9, 2026 at 7:40 pm

    To represent the data to be serialized (and deserialized) you can use a struct and set the proper metadata, so the CLR do the rest for you. Like others said here you will need to deal with the packet reception on the end point. Also you’ll have to consider the charset expected by the receiver, since you have a string field in your data. The following code is an example on how you can implement the struct to convert your managed data to a binary format, with comments.

    // setting the layout to sequential will prevent the compiler/JIT
    // to reorder the struct fields
    // NOTE: Observe here that the Charset used is Ansi. You may need to
    // change this depending on the format expected by the receiver.
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    struct DataPacket
    {
    
        [MarshalAs(UnmanagedType.U4)]
        public uint Id;
    
        // As I understood from your question, the Name field
        // has a prefixed size of 40 bytes. Attention here:
        // the SizeConst actually means '40 characters', not bytes
        // If you choose to use the Unicode charset, set SizeConst = 20
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
        public String Name;
    
        // This will be serialized in little endian format
        // in your question this field is 8 bytes long, which 
        // in c# corresponds to the double type. If you really mean
        // the float type (4 bytes), change here.
        public double Grade;
    
        // Calling this method will return a byte array with the contents
        // of the struct ready to be sent via the tcp socket.
        public byte[] Serialize()
        {
            // allocate a byte array for the struct data
            var buffer = new byte[Marshal.SizeOf(typeof(DataPacket))];
    
            // Allocate a GCHandle and get the array pointer
            var gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            var pBuffer = gch.AddrOfPinnedObject();
    
            // copy data from struct to array and unpin the gc pointer
            Marshal.StructureToPtr(this, pBuffer, false);
            gch.Free();
    
            return buffer;
        }
    
        // this method will deserialize a byte array into the struct.
        public void Deserialize(ref byte[] data)
        {
            var gch = GCHandle.Alloc(data, GCHandleType.Pinned);
            this = (DataPacket)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(DataPacket));
            gch.Free();
        }
    }
    

    Usage:

    DataPacket packet;
    packet.Id = 1234;
    packet.Name = "Marvin the paranoid robot";
    packet.Grade = 9.2;
    
    // serialize
    var bytes = packet.Serialize();
    
    // send via tcp
    var tcp = new TcpClient(...); 
    tcp.GetStream().Write(bytes, 0, bytes.Length);
    
    
    // deserializing;
    DataPacket receivedPacket;
    receivedPacket.Deserialize(bytes);
    

    You already have the packet, now you’ll need to deal with the packet reception on the receiver. That part you don’t need to do all by hand, you can use some tools, like @jgauffin said.

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

Sidebar

Related Questions

I need to stream webcam/microphone data from a browser to my server: Can I
I need to stream an audio file which is saved on my server. Is
I need to pass a memory stream to the WCF server , how do
I have TCP server , what recieve big data without a break. And I
I am using Csharp tcp sockets to send data between a client and server.
I have the following data structure or record, which I need to stream to
I need to stream PCM data generated at runtime. So I have a thread
For my current project, I need to request XML data over a tcp/ip socket
I have a server that streams data out on a TCP port once you
I'm reading a stream of data through TCP/IP socket. The stream load is very

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.