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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:49:52+00:00 2026-06-11T22:49:52+00:00

I have this simple C code that try to connect to www host by

  • 0

I have this simple C code that try to connect to www host by using 80 port. I’m getting normally http headers response, but instead of HTML contents, I’m getting unknow characters(am I printing garbage?)

(facebook.com is only example, it happens with any server)
check out my code:

  #define BUF_SIZE 4096
  struct sockaddr_in sockaddr;
  struct hostent *host;

  char buffer[BUF_SIZE] = { 0 };
  int sock;

  static const char const headers[] =
    "GET / HTTP/1.1\r\n"
    "Host:www.facebook.com\r\n"
    "User-Agent:Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n"
    "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
    "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    "Accept-Encoding:gzip,deflate\r\n"
    "Connection:close\r\n"
    "\r\n\r\n";

  if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    perror("Cannot create TCP socket");
    exit(1);
  }

  if((host = gethostbyname("www.facebook.com")) == NULL) {
    perror("Cannot get host");
    exit(1);
  }

  memset(&sockaddr, 0, sizeof(sockaddr));
  sockaddr.sin_family = AF_INET;
  memcpy(&sockaddr.sin_addr,
     host -> h_addr_list[0],
     (size_t) host -> h_length);
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_port = htons(80);

  if(connect(sock, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)) == -1) {
    perror("cannot connect");
    exit(1);
  }

  if(send(sock, headers, strlen(headers), 0) == -1) {
    perror("Cannot send packet");
    exit(1);
  }

  ssize_t readed;
  while((readed = recv(sock, buffer, BUF_SIZE, 0)) > 0) {
    write(fileno(stdout), buffer, readed);
    memset(buffer, 0, readed);
  }

  close(sock);

Example of response:

HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, must-revalidate
Expires: Sat, 01 Jan 2000 00:00:00 GMT
P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
Pragma: no-cache
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Set-Cookie: datr=7HVjUC3-OlTdOMH3HivA_8Ge; expires=Fri, 26-Sep-2014 21:38:52 GMT; path=/; domain=.facebook.com; httponly
Set-Cookie: reg_ext_ref=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.facebook.com
Set-Cookie: reg_fb_gate=http%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com
Set-Cookie: reg_fb_ref=http%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
X-FB-Debug: 36+IohXvxWRp+3LHt+aLebawuS3W/MaBkmgR9TJGMrk=
Date: Wed, 26 Sep 2012 21:38:52 GMT
Transfer-Encoding: chunked
Connection: close

2f03
�}Yo�X��{�
�S-U)���P!����V��2�
                  F�A�AFr       -9
                                        �
��0�p�{�1cx��1`▒������0m�                �
                         �\��XH)3U�v��yy���{��g�O��:�l_�lKoh�����L��7��'K�֐{jW����,uM�ue+��L�uU[:�=U�T�ս��{�LM���Oݮc����ou=ö$�▒;XR��G;K]�e;KcՑ4ɰ$u��Z{�P�c���[�6,;^u�w���������K��m��v����A���v!�pm�m�W���'�A�ծٹk��q���������Z�g�Ǘ�#�+���\��tU�������*s�V�ڶ7��W��[�����������xO��uM��jZ���ptR����Z5qv�QO��Ʀww���5K7�����Mg�c^쩻C+h��M<;V՛ڕ��e���P�1��գw���?���~w~�����ܖ�ky�U����X�����UTk\�Xw�'�b�V\����<Єҳ���ؖbڪ��z*�'%���⍬[od��]�H��W��?F��q� ��M�<�n�Owl)�>�fK�R~E�
                                                                                ��
[...]

How to fix this?

  • 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-11T22:49:53+00:00Added an answer on June 11, 2026 at 10:49 pm

    remove the line:

    "Accept-Encoding:gzip,deflate\r\n"
    

    as it tells the remote host that yoz accept compressed data, which you dont!

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

Sidebar

Related Questions

I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have this code that asks for a username and password. Just a simple
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have this simple code where I am sending http request and reading all
i have this simple code to load data from other php page using get
I have this simple code to test that a DB is ready: Function testlocalcon()
I have this simple code, that makes a get to a webservice. For some
I have a bit of sample code that is throwing this warning: main.c: In
I have a data that looks like this . And my code below simply
I have this simple code (for making things shorted, the important bits are probably

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.