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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:26:42+00:00 2026-05-24T18:26:42+00:00

I’m writing a basic HTTP client and have ran into a problem – some

  • 0

I’m writing a basic HTTP client and have ran into a problem – some HTTP servers are forcing resets, causing a “Connection reset by peer” error. Many HTTP servers do close the connection gracefully, though none seem to keep the connection alive.

However, I’m sure it’s my client because HTTP clients using very similar source code don’t exhibit the same behaviour: their connections to the same servers are either closed gracefully or kept alive.

What is causing this seemingly inconsistent problem?

Relevant code:

/* socket */
if ((context->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    perror("Failed to create socket");
    exit(-1);
}

/* connect */
if (connect(context->socket, &context->tx_addr, sizeof(struct sockaddr)) != 0) {
    perror("Couldn't connect to server");
    exit(-1);
}

/* create header */
snprintf(context->packet, BUFF_SIZE,
         "GET %s HTTP/1.1\r\n" \
         "Host: %s\r\n\r\n", 
         conf->request, conf->host);

/* send header */
if ((sendto(context->socket, context->packet, BUFF_SIZE, 0, 
            NULL, 0))) != BUFF_SIZE) {
    perror("Failed to send");
    exit(-1);
}

/* receive response */
do {
    if ((received = recvfrom(context->socket, context->packet, BUFF_SIZE, 0, NULL, 
                             NULL)) < 0) {

        /* THIS is where RST occurs with some servers */

        perror("Failed to receive");
        exit(-1)
    }

    if (received >= 0)     
            context->packet[received] = '\0';
    printf("%s", context->packet);

} while (received > 0);
  • 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-24T18:26:43+00:00Added an answer on May 24, 2026 at 6:26 pm

    After some investigation, it became apparent that my HTTP requests were malformed.

    The problem lies in this code:

    /* create header */
    snprintf(context->packet, BUFF_SIZE,
             "GET %s HTTP/1.1\r\n" \
             "Host: %s\r\n\r\n", 
             conf->request, conf->host);
    
    /* send header */
    if ((sendto(context->socket, context->packet, BUFF_SIZE, 0, 
                NULL, 0)) != BUFF_SIZE) {
        perror("Failed to send entire buffer");
        exit(-1);
    }
    

    Notice that BUFF_SIZE bytes from the buffer are sent, even though we almost certainly won’t fill the entire buffer when creating the header with snprintf. The garbage proceeding the generated header was also being transmitted. Some servers just ignored the bad request, others just gave up and reset (RST) the connection.

    Simply change the send code to something like this to fix the problem:

    /* assuming context->packet is a string */
    int len = strlen(context->packet);
    int sent = 0, total = 0;
    
    while (total < len) {
        if ((sent = sendto(context->socket, context->packet + total, 
                           len - total, 0, NULL, 0)) <= 0) {
            perror("Failed to send");
            exit(-1);
        }
        total += sent;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.