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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:57:13+00:00 2026-06-09T15:57:13+00:00

I am working on a function that will (when it’s finished) return the output

  • 0

I am working on a function that will (when it’s finished) return the output of an html page. I just got help from StackOverFlow with understanding how recv works, and I am now at a point where I am finishing up the function. The problem I am having right now is that it appears that the socket is not opening properly. The recv function just returns -1 (which indicates a socket error I believe)

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#define BUFFERSIZE 4096

char *readsite(const char *url, const char *ip, const int port)
{
    char *req, *ret, *tmp, tmpbuff[256];
    int c, i, q;
    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    struct sockaddr_in sa = { 0 };
    sa.sin_port = htons(port);
    sa.sin_addr.s_addr = inet_addr(ip);

    for(i = 0; url[i] != 0x00; i++);
    req = malloc((i + 18) * sizeof(char));
    if(!req) return NULL;
    req[0] = 'G';
    req[1] = 'E';
    req[2] = 'T';
    req[3] = ' ';
    for(c = 4, q = 0; q <= i; c++, q++) req[c] = url[q];
    req[c - 1] = ' ';
    req[c + 0] = 'H';
    req[c + 1] = 'T';
    req[c + 2] = 'T';
    req[c + 3] = 'P';
    req[c + 4] = '/';
    req[c + 5] = '1';
    req[c + 6] = '.';
    req[c + 7] = '0';
    req[c + 8] = '\r';
    req[c + 9] = '\n';
    req[c + 10] = '\r';
    req[c + 11] = '\n';
    req[c + 12] = '\0';
    connect(sock, (struct sockaddr*)&sa, sizeof(sa));
    for(i = 0; req[i] != 0x00; i++);
    send(sock, req, i, 0);
    printf("%s %s", req, ip);
    free(req);
    ret = malloc(BUFFERSIZE);
    if(!ret) return NULL;
    c = 0;             // recv value
    i = BUFFERSIZE;    // buffer size
    q = 0;             // loaded bytes
    while((c = recv(sock, tmpbuff, sizeof(tmpbuff), 0)) > 0)
    {
        if((q + c + 1) > i)
        {
                i += BUFFERSIZE;
                if(!(tmp = realloc(ret, i))) return NULL;
                ret = tmp;
        }
    }
 }

int main(void)
{
        readsite("http://www.google.com/", "74.125.226.226", 80);
    return 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-06-09T15:57:14+00:00Added an answer on June 9, 2026 at 3:57 pm

    Use the error code in errno to figure out what went wrong; perror() is a great function for this, call it after a check for error code != 0. Here is a suggested way of doing so in your code:

    while(1)
    {
        if ((c = recv(sock, tmpbuff, sizeof(tmpbuff), 0)) < 0) {
            perror("Receive error");
        }
        else if (c == 0) { 
            break;
        }
        if((q + c + 1) > i)
        {
                i += BUFFERSIZE;
                if(!(tmp = realloc(ret, i))) return NULL;
                ret = tmp;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a function that will return a context menu for use within
I need a working code for a function that will return a random string
I am working on a function that will essentially see which of two ints
I'm working on a function that handles events from a number of buttons and
I am working on a function that will give me a Kermit CRC value
I'm working on a Function that will do a POW long handed. It's an
I am working on a PHP function that will recursively remove all sub-folders that
Working on a small program that will take a string from a user and
I'm looking for a function that will return a list of what is in
I'm working on a C++ application that will build a fixed-length record from 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.