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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:33:12+00:00 2026-06-14T17:33:12+00:00

I am having an issue with recv . I wrote a function that fills

  • 0

I am having an issue with recv. I wrote a function that fills a structure with data, and the length (in bytes) of that data which is read from a socket.

For testing I am just printing the data to stdout byte by byte based on the total amount of bytes read by recv. For some reason the number of bytes being read seems to be correct sometimes and incorrect other times depending on what site I am querying. For example the following code works as intended on some sites:

data->data_sz = 0;
while((i = recv(sock, data->data + data->data_sz, CHUNKSIZE, 0)) > 0)
{
    data->data_sz += i;
    if(databff - data->data_sz < CHUNKSIZE)
    {
        databff *= 2;
        if(!(tmp = realloc(data->data, databff)))
        {
            free(data->data);
            (void) WSACleanup();
            return 0;
        }
        data->data = tmp;
    }
}
i = strsbstr(data->data, "\r\n\r\n") + 4;                   //i = the position of the first char after header info  
if(i >= 0)  
{
    data->data_sz = data->data_sz - i;                      //data->data_sz = number of bytes without header info
    memmove(data->data, data->data + i, data->data_sz);
    if(!(tmp = realloc(data->data, data->data_sz)))
    {
        free(data->data);
        (void)WSACleanup();
        return 0;
    }
        data->data = tmp;                       
    }
    else
    {
        free(data->data);
        (void) WSACleanup();
        return 0;
    }
    return 1;
}

To print the data to stdout I just use a for loop:

//t_html->data_sz points to my data->data_sz structure
//t_html->data points to my data->data structure
for(i = 0; i <= t_html->data_sz; i++) (void)fputc((int)t_html->data[i], stdout);

The above code works for some sites but fails on others (for example when querying http://www.google.com I expect the final characters to be </html> but I get </html>l).

Basically my problem is that data->data_sz (the amount of bytes received) is not being calculated correctly, which makes it impossible to correctly use gathered data. I am really at a loss of what to do right now.

EDIT:
here is the strsbstr function which is called in the above code:

int strsbstr(const char *str, const char *sbstr)
{
    char *sbstrlc;

    if(!(strcmp(str, sbstr))) return 0;
    if(!(sbstrlc = strstr(str, sbstr))) return -1;
    return (int) (sbstrlc - str);
}
  • 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-14T17:33:13+00:00Added an answer on June 14, 2026 at 5:33 pm
    1. recv(sock, data->data + data->data_sz, CHUNKSIZE, 0) is potentially a problem. Why? Because you may not have CHUNKSIZE room left in your buffer. You have databff - data->data_sz left, actually (assuming data is allocated to a size of databff). It all depends on the initial values of databff and CHUNKSIZE, which I can’t see and figure I’d point this out just in case.
    2. Data is not guaranteed to be NULL-character terminated. Your printing loop says i <= t_html->data_sz; which is wrong. It should be i < t_html->data_sz;. If you use <=, you’re accessing one past your buffer, which is likely why you get a weird character sometimes, and sometimes not.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having issue when sending data from Service to Activity through Notification ,
Having issue with displaying the Data from a second Activity. when I run the
I'm having trouble unencoding some data I get from a socket connection through messagepack,
I have a non-blocking winsock socket that is recv 'ing data in a loop.
I having issue that content assistant / intellisense is working in methods such as
Hi guys I am having issue I have this query: SELECT * FROM useraccount
Having an issue with a salesforce login on our site. When the customer fills
Having an issue here that I have tried everything I can think of but
I am having an issue in my recv() loop for winsock. I am trying
Having issue with prefetching data and displaying on uitableview. So basically I want to

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.