// 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.
// 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 (!mysocket.eof()) { cout<<mysocket.read(); }
mysocket.close();//为什么不可以只关闭SHUT_RD来充当close?SHUT_WR已经关闭了 cerr << "Function called: get_URL(" << host << ", " << path << ").\n"; cerr << "Warning: get_URL() has not been implemented yet.\n"; }
intmain(int argc, char *argv[]){ try { if (argc <= 0) { abort(); // For sticklers: don't try to access argv[0] if argc <= 0. }
// The program takes two command-line arguments: the hostname and "path" part of the URL. // Print the usage message unless there are these two arguments (plus the program name // itself, so arg count = 3 in total). if (argc != 3) { cerr << "Usage: " << argv[0] << " HOST PATH\n"; cerr << "\tExample: " << argv[0] << " stanford.edu /class/cs144\n"; return EXIT_FAILURE; }
// Get the command-line arguments. const string host = argv[1]; const string path = argv[2];
//! Bytes are written on the "input" side and read from the "output" //! side. The byte stream is finite: the writer can end the input, //! and then no more bytes can be written. classByteStream { private: // Your code here -- add private members as necessary.
// Hint: This doesn't need to be a sophisticated data structure at // all, but if any of your tests are taking longer than a second, // that's a sign that you probably want to keep exploring // different approaches.
public: //! Construct a stream with room for `capacity` bytes. ByteStream(constsize_t capacity);
//! \name "Input" interface for the writer //!@{
//! Write a string of bytes into the stream. Write as many //! as will fit, and return how many were written. //! \returns the number of bytes accepted into the stream size_twrite(const std::string &data);
//! \returns the number of additional bytes that the stream has space for size_tremaining_capacity()const;
//! Signal that the byte stream has reached its ending voidend_input();
//! Indicate that the stream suffered an error. voidset_error(){ _error = true; } //!@}
//! \name "Output" interface for the reader //!@{
//! Peek at next "len" bytes of the stream //! \returns a string std::string peek_output(constsize_t len)const;
//! Remove bytes from the buffer voidpop_output(constsize_t len);
//! Read (i.e., copy and then pop) the next "len" bytes of the stream //! \returns a string std::string read(constsize_t len);
//! \returns `true` if the stream input has ended boolinput_ended()const;
//! \returns `true` if the stream has suffered an error boolerror()const{ return _error; }
//! \returns the maximum amount that can currently be read from the stream size_tbuffer_size()const;
//! \returns `true` if the buffer is empty boolbuffer_empty()const;
//! \returns `true` if the output has reached the ending booleof()const; //!@}
//! \name General accounting //是总计数器,记录了这个类一共读写多少,所以要定义私有成员变量来记录 //!@{
//! Total number of bytes written size_tbytes_written()const;//
//! Total number of bytes popped size_tbytes_read()const; //!@} };
//! \param[in] len bytes will be copied from the output side of the buffer string ByteStream::peek_output(constsize_t len)const{ size_t length=len; if(length>_buffer.size()) length=buffer_size(); returnstring().assign(_buffer.begin(), _buffer.begin() + length);
}
//! \param[in] len bytes will be removed from the output side of the buffer voidByteStream::pop_output(constsize_t len){ size_t length=len; if(length>_buffer.size()) length=_buffer.size(); _read_count+=length;
//! Read (i.e., copy and then pop) the next "len" bytes of the stream //! \param[in] len bytes will be popped and returned //! \returns a string std::string ByteStream::read(constsize_t len){ string result = ByteStream::peek_output(len); ByteStream::pop_output(len);