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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:36:18+00:00 2026-05-18T02:36:18+00:00

I have a couple of questions related to the following code: char buffer[256]; memset(buffer,0,256);

  • 0

I have a couple of questions related to the following code:

char buffer[256];
memset(buffer,0,256);

read(socket_fd,buffer,255);

The questions:

  1. Why I read 255 not 256 ?
  2. Let’s say I want to send the word: “Cool” from the client to the server. How many bytes should I write “in client” and how many bytes should i read “in the server”?

I’m really confused.

  • 1 1 Answer
  • 1 View
  • 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-18T02:36:18+00:00Added an answer on May 18, 2026 at 2:36 am

    You already have good answers here, but I think there’s a concept we should explain.

    When you send data through streams (that is, something that writes a number of bytes from one end, and those bytes can be read in the same order in the other end), you almost always want to know when to stop reading. This is mandatory if you’ll send more than one thing: when does the first message stop, and the second begin? In the stream, things get mixed up.

    So, how do we delimit messages? There are three simple ways (and many other not so simple ones, of course):

    1 Fixed-length messages:
    If you know beforehand that every message is, say, 10-bytes long, then you don’t have a problem. You just read 10 bytes, and the 11th one will be part of another message. This is very simple, but also very rigid.

    2 Delimiting characters, or strings:
    If you are sending human-readable text, you might delimit your messages the same way you delimit strings in your char*: putting a 0 character at the end. That way, when you read a 0, you know the message ended and any remaining data in the stream belongs to another message.

    This is okay for ascii text, but when it comes to arbitrary data it’s also somewhat rigid: there’s a character, or a sequence of characters, that your messages can’t contain (or your program will get confused as to where a message ends).

    3 Message headers:
    This is the best approach for arbitrary length, arbitrary content messages. Before sending any actual message data, send a fixed-length header (or use technique nr 2 to mark the end of the header), specifying metadata about your message. For example, it’s length.

    Say you want to send the message ‘Cool’, as you said. Well, first send a byte (or a 2-byte short, or a 4-byte integer) containing ‘4’, the length of the message, and receive it on the other end. You know that before any message arrives, you must read 1 byte, store that somewhere and then read the remaining specified bytes.

    A simplified example:

    struct mheader {
        int length;
    }
    
    // (...)
    
    struct mheader in_h;
    read(fd, &in_h, sizeof(struct mheader);
    
    if (in_h.length > 0) {
        read(fd, buffer, in_h.length)
    }
    

    In actual use, remember that read doesn’t always read the exact amount of bytes you request. Check the return value to find out (which could be negative to indicate errors), and read again if necessary.

    Hope this helps. Good luck!

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

Sidebar

Related Questions

I have a couple of questions related to TFS and source control: 1) I
Have a couple questions, using GSON. I have a feeling that GSON might not
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
Firstly, I have found a couple of related questions on the issue with links
I have went through couple of questions already asked related with this and i
I'm considering micro-optimization of a huge CSS stylesheet and have a couple questions related
I have a couple of questions related to NCover: 1) I'm using MSBuild to
I have couple of questions related to the usage of Dynamics CRM: Can it
I have a couple of questions that are somewhat related so I'm posting them
So I have looked at a couple of related questions, but still can't seem

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.