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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:03:43+00:00 2026-05-27T20:03:43+00:00

Update: I add a while to get the remain data, problem solved. thanks you

  • 0

Update:
I add a while to get the remain data, problem solved. thanks you guys.

while(res != rcvread->size + 4) {/* do not get full data */
    tmp = recv(connfd, (void *)rcvread + res, rcvread->size + 4 - res, 0);
    if(tmp == -1)
        printf("error:%s\n",(char *)strerror(errno));
    res+=tmp;
}

I want to send a structure mfsio through socket, the return value of write is 37772, but when I got it from the other program, the return value of read is 32768, that’s really odd.

The definition of this structure is here

struct mfsio{
    size_t size;
    char buf[];
};

The send code is here

struct mfsio *sndread;
sndread = malloc(sizeof(struct mfsio) + rcvcmd->size);
res = pread(fd, &(sndread->buf), rcvcmd->size, rcvcmd->offset);
sndread->size = res;
res = write(connfd, sndread, sizeof(struct mfsio) + res);

The receive code is here

struct mfsio *rcvread;
rcvread = malloc(sizeof(struct mfsio) + size);
res = 0;    
res = read(connfd, rcvread, sizeof(struct mfsio) + size);

size equals to rcvcmd->size, and the res of pread is 32678, the res of write is 32772.
but res of read is 32678.

How can such thing happened? Does I do something wrong here?

If there is no input from write, the read function will just waiting for some data and just hanging there

If I use a loop to avoid such problem, how can I finally get the entire data structure, I mean if I do loop reading, I will get the remain bytes from the socket, but how can I combine these clips I get?

  • 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-27T20:03:43+00:00Added an answer on May 27, 2026 at 8:03 pm

    You need to do your reads and writes in a loop until all of the expected data is read/written. For the read, you have to read until it returns 0, indicating nothing else is available. Or -1 indicating an error.

    Your more recent problem is that you are using pointer arithmetic with your struct pointer:

    tmp = recv(connfd, rcvread + res, rcvread->size + 4 - res, 0);
    

    In C when you do pointer arithmetic you are not working with bytes, but you are working with the size of the object (rvcread for example). You should define a char * which is the byte location of where you will read into and then update this as you read through the data.

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

Sidebar

Related Questions

Update mysql with $_POST data from while loop I'm trying to get this round
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
I am trying a add data to an array using a while loop but
I am trying to update data while I am reading them from database, see
I have several wpf pages with update/delete/add buttons. I want to display to the
Update: looks like this is indeed a WiX limitation - Cannot add 64bit com+
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to
Often I want to add a value to a table or update the value
Dynamically I want to edit/update hosts(etc/hosts) file to add domain. To edit hosts(etc/hosts) file
I'm trying to figure out a way to add a self-update feature to a

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.