From 3e44d2cc2c515fe75a2b3bc99ec63fdee3dec042 Mon Sep 17 00:00:00 2001 From: Chengjie Huang Date: Mon, 30 May 2022 22:38:16 +0100 Subject: [PATCH] lab0: implement get_URL --- apps/webget.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apps/webget.cc b/apps/webget.cc index 3b85ce3f..6dc55ec2 100644 --- a/apps/webget.cc +++ b/apps/webget.cc @@ -8,15 +8,26 @@ using namespace std; void get_URL(const string &host, const string &path) { // Your code here. + const uint16_t portnum = ((std::random_device()()) % 50000) + 1025; // You will need to connect to the "http" service on // the computer whose name is in the "host" string, // then request the URL path given in the "path" string. + TCPSocket socket; + socket.connect(Address(host, "http")); + socket.write("Get " + path + "HTTP/1.1\r\n"); + auto recvd = socket.read(); // Then you'll need to print out everything the server sends back, // (not just one call to read() -- everything) until you reach // the "eof" (end of file). + while (!recvd.empty()) { + cout << recvd; + recvd = socket.read(); + } + socket.write("Connection: close\r\n"); + socket.close(); cerr << "Function called: get_URL(" << host << ", " << path << ").\n"; cerr << "Warning: get_URL() has not been implemented yet.\n"; }