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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:13:17+00:00 2026-05-27T04:13:17+00:00

So, I’m trying to play around with sockets, but my read() call keeps hanging.

  • 0

So, I’m trying to play around with sockets, but my read() call keeps hanging. I’m connecting to a random web page (e.g. yahoo.com) on port 80. My output is showing that I’m writing 5 bytes, but I’m not getting anything in return. Can anyone point out what I’m doing wrong?

portno = 80;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {printf("could not open socket\n");}

server = gethostbyname("yahoo.com");
if (server == NULL) 
{
    printf("could not get hostbyname\n");
    exit(0);
}

printf("Server name:%s\n",server->h_name);

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

bcopy((char *)server->h_addr,   (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);


if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
{
    printf("connect error\n");
}
char buffer[1000];
strcpy(buffer,"GET \\");
printf("bytes in command:%d\n",sizeof(buffer));
n = write(sockfd,buffer,strlen(buffer));
if(n <= 0){printf("could not get page:\n");}
printf("Bytes that where writen:%d\n", n);

printf("reading from socket\n");
n = read(sockfd,buffer,sizeof(buffer));
printf("bytes that where read:%d",n);
  • 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-27T04:13:17+00:00Added an answer on May 27, 2026 at 4:13 am

    Your HTTP protocol is not complete enough. Use this…

    strcpy(buffer,"GET / HTTP/1.1\r\nHostname:www.yahoo.com\r\n\r\n");
    

    You must specify the “GET” and filename, the HTTP version, the Hostname, and two new lines to indicate the header is done. The server expects you to complete the header before it starts sending anything. This is why your call to read() is stalling. Take a look at the Client Request example on the HTTP article on Wikipedia.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.