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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:53:15+00:00 2026-05-26T16:53:15+00:00

I am curious as to the proper method for sending multiple pieces of data

  • 0

I am curious as to the proper method for sending multiple pieces of data of arbitrary length over a socket in C. For example, if one were to send a “username” of arbitrary length, a “subject” of arbitrary length and a “message” of arbitrary length what would the correct process be for sending these. Also, the data I am attempting to send may not necessarily be null-terminated so I don’t believe I could reassemble it correctly based solely on null-bytes.

The method I have come up with would involve reading the first 4 bytes of the input received on the server and interpreting that as the size of the first piece of data and read that amount of data from the socket and interpret it as the first string, read 4 more bytes and interpret that as the length of the second string then read exactly that many bytes and interpret it as the second string and so on. However, this seems like it could be error-prone or have some implementation details that could cause things to go awry. Is there a better way to accomplish this?

  • 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-26T16:53:16+00:00Added an answer on May 26, 2026 at 4:53 pm

    In order to avoid having your send/receive routines become insanely complicated as your data structures grow more elaborate, I recommend splitting the problem into separate steps:

    1. Write routines that can frame and send an arbitrary buffer of N bytes over the TCP connection. (This would involve sending the 4-byte length prefix, and then sending the N bytes, as you described). You might also want to include a 4-byte type-code header that the receiver can use to easily identify which of your data structures the received bytes are intended to represent.

    2. Write a routine that converts (your favorite data structure) into a series of N bytes that are held in RAM. Then write an associated routine that de-converts a series of N bytes held in RAM back into (your favorite data structure).

    3. Repeat step (2) for any other data structures you want to send over the wire. Note that once you have multiple data types, the type-code field mentioned in (1) will make it easier for the receiver to know which de-convert routine to call after it has completely received a byte-buffer.

    Once you’ve done the above, you can use your general-purpose send/receive-byte-buffers code from (1) to transport any of your data structures from (2), and thus you don’t have to write separate send/receive code for every data structure, which is a big win.

    Note that if you are worried about portability, you’ll need to make sure to convert any multibyte integer or float values you want to send to big-endian (or little-endian, doesn’t matter as long as you are consistent) before you send them, and then de-convert them back to the native endian on the receiver after you receiver them (but before you use them for anything). (You’ll also need to avoid the temptation of just memcpy()’ing C structs into a byte-buffer, since different platforms and even different compiler versions may pad out the C structs differently in memory, which would lead to disastrous results if your sender and receiver aren’t running the exact same executable)

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

Sidebar

Related Questions

curious what the BSON::ObjectId.id.data array represents? for example [77, 145, 20, 13, 225, 96,
Curious as to the inode data structure differences between 128-byte ext2 and 256-byte ext3
I'm curious if I'm following proper memory management with CGImageRef, which I'm passing through
Given a simple Flask application, I'm just curious about whether there is a proper
I'm curious what is the 'proper' pythonic way of handling this error with a
I'm curious what the proper way to trim a string is to ensure that
I was curious, when one goes ahead and executes their code for leaks checking
curious if anyone might have some insight in how I would do the following
Curious to know how people set up their personal and/or work development environment, in
Curious to get people's thoughts. I conduct frequent interviews, and have had enough in

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.