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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:24:41+00:00 2026-06-12T13:24:41+00:00

I’m new to socketprogramming and wanted to write a little console app to receive

  • 0

I’m new to socketprogramming and wanted to write a little console app to receive a webpage and just print it out.

This is my code:

#include <stdio.h>
#include <winsock2.h>
#include <winsock.h>
#include <Ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib" )

#define BUFFERSIZE 5000

int main()
{
    WSADATA wsaData; 
    char buffer[BUFFERSIZE];
    char *getMsg = {"GET / HTTP/1.0\nUser-Agent: HTTPTool/1.0\n\n"};
    char * url = (char*)malloc(BUFFERSIZE);
    int socket_fd = 0;
    int receivingContent = 1; 
    int errorValue;
    int numbytes;
    struct addrinfo hints;
    struct addrinfo *servinfo, *p;  
    socklen_t addr_size;

    if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) 
    {
        fprintf(stderr, "WSAStartup failed.\n");
        exit(1);
    }

    memset(&hints, 0, sizeof(hints));   // empty struct
    hints.ai_family = AF_UNSPEC;        // IPv4 or IPv6
    hints.ai_socktype = SOCK_STREAM;    // using TCP

    printf("Give a website like for example: www.google.com \n");
    scanf("%s",url);

    //Error testing
    if ((errorValue = getaddrinfo(url, "80", &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(errorValue));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((socket_fd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
            perror("Failed to create socket");
            continue;
        }
        break;
    }

    //sending the http get message to the server
    send(socket_fd, getMsg, strlen(getMsg), 0);

    //While the full content isn't downloaded keep receiving
    while(receivingContent)
    {
        numbytes = recv(socket_fd, buffer,BUFFERSIZE , 0);
        if(numbytes > 0)
        {
            buffer[numbytes]='\0';
            printf("%s", buffer);
        }
        else
            receivingContent = 0; //stop receiving
    }

    free(url);
    free(getMsg);
    freeaddrinfo(servinfo);
    closesocket(socket_fd); //close the socket
    WSACleanup();
    return 0;
}

It compiles and then when I enter for example http://www.google.com it crashes and it gives me file of C. I’m looking for the error but in my opinion I’m doing it right…

Can someone help me please?

Kind regards,

  • 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-12T13:24:44+00:00Added an answer on June 12, 2026 at 1:24 pm

    I didn’t find connect in your program. You may need to do connect before do recv.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.