473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Downloading (a webpage) in C++

15
Hi, I'm fairly new to socket/network programming.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9.  
  10. using namespace std;
  11. int main() {
  12.     int sockfd;
  13.     sockaddr_in addr;
  14.     char rqt[] = "GET /forum HTTP/1.1\nhost: cplusplus.com\nAccept: text/html";
  15.     char data[1024*1024];
  16.     fstream stream;
  17.  
  18.     addr.sin_family = AF_INET;
  19.     addr.sin_port = htons(80);
  20.     addr.sin_addr.s_addr = inet_addr("50.22.100.250");
  21.     memset(&(addr.sin_zero), '\0', 8);
  22.  
  23.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  24.     connect(sockfd, (sockaddr *) &addr, sizeof(sockaddr_in));
  25.  
  26.     write(sockfd, rqt, strlen(rqt)); // send(sockfd, rqt, strlen(rqt), 0);
  27.     read(sockfd, data, 1024*1024); // recv(sockfd, data, 1024*1024, 0);
  28.  
  29.     stream.open("saved.htm", ios::out | ios::trunc);
  30.     if(stream.good() && stream.is_open()) {
  31.         for(int i=0; i<strlen(data); i++) {
  32.             stream << data[i];
  33.         }
  34.     }
  35.  
  36.     stream.close();
  37.     close(sockfd);
  38.     return 0;
  39. }
  40.  
My question is, why does the program get stuck at read()?
And what's the difference between send()/recv() and read()/write()?
Thanks for help...
Aug 29 '11 #1
2 3924
This article might help you.
http://joysofprogramming.com/download-webpage-curl-c/
Sep 18 '11 #2
johny10151981
1,059 1GB
Your program stuck at read?

Not really, you never finished sending header to http server
a line in a herder get end when it get the sequence of "\r\n"
not just "\r"

and header ends when you send "\r\n\r\n" exact pattern
so your actual header would be
Expand|Select|Wrap|Line Numbers
  1. char rqt[] = "GET /forum HTTP/1.1\r\nhost: cplusplus.com\r\nAccept: text/html\r\n\r\n";
  2.  
Sep 20 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Patrick | last post by:
I am trying to download the html page at http://www.dreamteamfc.com/dtfc04/servlet/PostPlayerList?catidx=1 Using the code public class DownloadWebPage { public static void main (String...
4
by: Luke StClair | last post by:
Only marginally belonging in this newsgroup... but oh well. I've just started writing in python, and I want to make the files available on the web. So I did the standard <a...
11
by: JS | last post by:
Dear Group, I have a webpage of small images. At the bottom is a Javascript function. If someone scrolls impatiently to the function link, when the action is complete the html does not continue...
4
by: Jeff Cooper | last post by:
There must be something I'm missing. No else else seems to have this issue -- at least I don't see it mentioned anywhere. (Also, my appologies for my earlier posts in which I mistakenly referred...
23
by: Doug van Vianen | last post by:
Hi, Is there some way in JavaScript to stop the downloading of pictures from a web page? Thank you. Doug van Vianen
7
by: Steve | last post by:
Hello, I am a beginner/intermediate c++ coder. I have a program that I am writing and would like to be able to distribute to some of my friends. The problem with this is that the data files...
0
by: =?Utf-8?B?am5ubA==?= | last post by:
Both of my computers cannot even start downloading the software from either the ideawins.com site or the microsoft site. They both say: "Internet Explorer cannot display webpage". I have...
1
by: JRussell | last post by:
Hi, I am trying to download sql database through asp on my webpage and I keep getting weird outcomes in Excel. I have had to put in <char10> and <char13> into my sql data to have it display...
1
by: jambalapamba | last post by:
Hi all, I want to download images,css,js files referenced by a webpage. I am doing this by downloading the HTML of webpage and getting all the URL references in the html and using...
1
by: syam s | last post by:
Hi , I am beginner in jquery , i am using Asp.net , jquery for providing UI elements . when web pages are load we can see the html code using view code option .And when downloading the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.