{ "key_pair_value_system": true, "answer_rating_count": "", "question_feedback_html": { "html_star": "", "html_star_feedback": "" }, "answer_average_rating_value": "", "answer_date_js": "2024-09-25T06:31:51-04:00", "answer_date": "2024-09-25 06:31:51", "is_docs_available": "", "is_excel_available": "", "is_pdf_available": "", "count_file_available": 0, "main_page": "student_question_view", "question_id": "13478894", "url": "\/study-help\/questions\/in-c-there-is-a-very-useful-program-called-wget-13478894", "question_creation_date_js": "2024-09-25T06:31:51-04:00", "question_creation_date": "Sep 25, 2024 06:31 AM", "meta_title": "[Solved] In C, there is a very useful program call | SolutionInn", "meta_description": "Answer of - In C, there is a very useful program called wget. It's a command line tool that you can use to download a web page lik | SolutionInn", "meta_keywords": "c,program,called,wget,s,command,line,tool,download,web,page,http", "question_title_h1": "In C, there is a very useful program called wget. It's a command line tool that you can use to download a web page like", "question_title": "In C, there is a very useful program called wget. It's a", "question_title_for_js_snippet": "In C, there is a very useful program called wget It's a command line tool that you can use to download a web page like this wget http www gnu org software make manual make html which will download the make manual page, make html, and save it in the current directory wget can do much more (downloading a whole web site, for example) see man wget for more info Your job is to write a limited version of wget, which we will call http client c, that can download a single file You use it like this http client www gnu org 80 software make manual make html So you give the components of the URL separately in the command line the host, the port number, and the file path The program will download the given file and save it in the current directory So in the case above, it should produce make html in the current directory It should overwrite an existing file Hints The program should open a socket connection to the host and port number specified in the command line, and then request the given file using HTTP 1 0 protocol (See http www jmarshall com easy http for HTTP 1 0 protocol ) An HTTP GET request looks like this GET path file html HTTP 1 0 zero or more headers a blank line Include the following header in your request Host the host name you are connecting to Some web sites require it Use rather than as newline when you send your request It's required by the HTTP protocol Then the program reads the response from the web server which looks like this HTTP 1 0 200 OK Date Fri, 31 Dec 1999 23 59 59 GMT Content Type text html Content Length 1354 Happy New Millennium (more file contents) Just like in part 1, you can use fdopen() to wrap the socket with a FILE , which will make reading the lines much easier The 200 in the 1st line indicates that the request was successful If it's not 200, the program should print the 1st line and exit After the 1st line, a bunch of headers will come, then comes a blank line, and then the actual file content starts Your program should skip over all headers and just receive the file content Note that the program should be able to download any type of file, not just HTML files The server will terminate the socket connection when it's done sending the file You will need to pick out the file name part of a file path (make html from software make manual make html for example) Check out strrchr() You will need to convert a host name into an IP address Here is one way to convert a host name into an IP address in dotted quad notation struct hostent he char serverName argv 1 get server ip from server name if ((he gethostbyname(serverName)) NULL) die( gethostbyname failed ) char serverIP inet ntoa( (struct in addr )he h addr) The man pages of the functions will tell you which header files need to be included ", "question_description": "

In C, there is a very useful program called \"wget\". It's a command line tool that you can use to download a web page like this:<\/p>

wget http:\/\/www.gnu.org\/software\/make\/manual\/make.html<\/p>

which will download the make manual page, make.html, and save it in the current directory. wget can do much more (downloading a whole web site, for example); see man wget for more info.<\/p>

Your job is to write a limited version of wget, which we will call http-client.c, that can download a single file. You use it like this:<\/p>

.\/http-client www.gnu.org 80 \/software\/make\/manual\/make.html<\/p>

So you give the components of the URL separately in the command line: the host, the port number, and the file path. The program will download the given file and save it in the current directory. So in the case above, it should produce make.html in the current directory. It should overwrite an existing file.<\/p>

Hints:<\/p>

- The program should open a socket connection to the host and port number specified in the command line, and then request the given file using HTTP 1.0 protocol. (See http:\/\/www.jmarshall.com\/easy\/http\/ for HTTP 1.0 protocol.) An HTTP GET request looks like this:<\/p>

GET \/path\/file.html HTTP\/1.0 [zero or more headers ...] [a blank line]<\/p>

- Include the following header in your request:<\/p>

Host: the.host.name.you.are.connecting.to:<\/p>

Some web sites require it.<\/p>

- Use \" \" rather than \" \" as newline when you send your request. It's required by the HTTP protocol.<\/p>

- Then the program reads the response from the web server which looks like this:<\/p>

HTTP\/1.0 200 OK Date: Fri, 31 Dec 1999 23:59:59 GMT Content-Type: text\/html Content-Length: 1354

Happy New Millennium!<\/h1> (more file contents) . . . <\/body> <\/html><\/p>

Just like in part 1, you can use fdopen() to wrap the socket with a FILE*, which will make reading the lines much easier.<\/p>

- The \"200\" in the 1st line indicates that the request was successful. If it's not 200, the program should print the 1st line and exit.<\/p>

- After the 1st line, a bunch of headers will come, then comes a blank line, and then the actual file content starts. Your program should skip over all headers and just receive the file content.<\/p>

- Note that the program should be able to download any type of file, not just HTML files.<\/p>

- The server will terminate the socket connection when it's done sending the file.<\/p>

- You will need to pick out the file name part of a file path (make.html from \/software\/make\/manual\/make.html for example). Check out strrchr().<\/p>

- You will need to convert a host name into an IP address. Here is one way to convert a host name into an IP address in dotted-quad notation:<\/p>

struct hostent *he; char *serverName = argv[1];<\/p>

\/\/ get server ip from server name if ((he = gethostbyname(serverName)) == NULL) { die(\"gethostbyname failed\"); } char *serverIP = inet_ntoa(*(struct in_addr *)he->h_addr);<\/p>

The man pages of the functions will tell you which header files need to be included.<\/p>", "transcribed_text": "", "related_book": { "title": null, "isbn": null, "edition": null, "authors": null, "cover_image": null, "uri": null, "see_more_uri": "" }, "question_posted": "2024-09-25 06:31:51", "step_by_step_answer": "The Answer is in the image, click to view ...", "students_also_viewed": [], "next_back_navigation": { "previous": "", "next": "" }, "breadcrumbs": [ { "name": "Study help", "link": "https:\/\/www.solutioninn.com\/study-help\/questions-and-answers" }, { "name": "Computer Science", "link": "https:\/\/www.solutioninn.com\/study-help\/questions-and-answers\/computer-science" }, { "name": "Databases", "link": "https:\/\/www.solutioninn.com\/study-help\/questions\/computer-science-databases" }, { "name": "In C, there is a very useful program called wget. It's a", "link": "https:\/\/www.solutioninn.com\/study-help\/questions\/in-c-there-is-a-very-useful-program-called-wget-13478894" } ], "skill_details": { "skill_id": "656", "skill_name": "Databases", "parent_id": "8" } }