所有资料都在官网https://cs144.github.io/
首先是lab0,特别要说的是,gcc我用的ubantu,默认版本7.5,所以还是要下,然后网上查的大多用源码,直接apt下的记不清是哪个版本了,因为当时下完之后没有设置优先级就-v看版本了,还是7.5
先sudo apt install gcc-8
后sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100
安g++时同理
然后就能make了,不然会报错
然后就是第一部分,我这里只写代码内容
void get_URL(const string &host, const string &path) {
// Your code here.
TCPSocket sk;
sk.connect(Address(host,"http"));
// 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.
sk.write("GET "+path+" HTTP/1.1\r\nHOST: "+host+"\r\n\r\n");
// 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).
string response;
while (sk.eof()==false)
{
response = sk.read();
cout<<response;
}
sk.close();
cerr << "Function called: get_URL(" << host << ", " << path << ").\n";
cerr << "Warning: get_URL() has not been implemented yet.\n";
}
make check_webget通过
然后是第二个部分
byte_stream.hh
#ifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
#define SPONGE_LIBSPONGE_BYTE_STREAM_HH
#include <string>
#include <deque>
//! \brief An in-order byte stream.
//! 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.
class ByteStream {
private:
// Your code here -- add private members as necessary.
size_t _capacity = 0;
deque<char> _buffer{};
bool _eof = false;
bool _input_end=false;
size_t _bytes_read = 0;
size_t _bytes_written = 0;
// 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.
bool _error{}; //!< Flag indicating that the stream suffered an error.
public:
//! Construct a stream with room for `capacity` bytes.
ByteStream(const size_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_t write(const std::string &data);
//! \returns the number of additional bytes that the stream has space for
size_t remaining_capacity() const;
//! Signal that the byte stream has reached its ending
void end_input();
//! Indicate that the stream suffered an error.
void set_error() { _error = true; }
//!@}
//! \name "Output" interface for the reader
//!@{
//! Peek at next "len" bytes of the stream
//! \returns a string
std::string peek_output(const size_t len) const;
//! Remove bytes from the buffer
void pop_output(const size_t len);
//! Read (i.e., copy and then pop) the next "len" bytes of the stream
//! \returns a string
std::string read(const size_t len);
//! \returns `true` if the stream input has ended
bool input_ended() const;
//! \returns `true` if the stream has suffered an error
bool error() const { return _error; }
//! \returns the maximum amount that can currently be read from the stream
size_t buffer_size() const;
//! \returns `true` if the buffer is empty
bool buffer_empty() const;
//! \returns `true` if the output has reached the ending
bool eof() const;
//!@}
//! \name General accounting
//!@{
//! Total number of bytes written
size_t bytes_written() const;
//! Total number of bytes popped
size_t bytes_read() const;
//!@}
};
#endif // SPONGE_LIBSPONGE_BYTE_STREAM_HH
byte_stream.cc
#include "byte_stream.hh"
// Dummy implementation of a flow-controlled in-memory byte stream.
// For Lab 0, please replace with a real implementation that passes the
// automated checks run by `make check_lab0`.
// You will need to add private members to the class declaration in `byte_stream.hh`
template <typename... Targs>
void DUMMY_CODE(Targs &&... /* unused */) {}
using namespace std;
ByteStream::ByteStream(const size_t capacity) {
_capacity=capacity;
_bytes_read=0;
}
size_t ByteStream::write(const string &data) {
size_t len_need2write = data.size();
size_t len_written;
size_t now_capacity=remaining_capacity();
if(now_capacity == 0)
return 0;
if (now_capacity < len_need2write) //overflow
len_written = now_capacity;
else
len_written = len_need2write;
for (size_t i = 0; i != len_written; i++)
_buffer.push_back(data[i]);
_bytes_written += len_written;
return len_written;
}
//! \param[in] len bytes will be copied from the output side of the buffer
string ByteStream::peek_output(const size_t len) const {
size_t true_len = min(len, _buffer.size());
return string().assign(_buffer.begin(), _buffer.begin() + true_len);
}
//! \param[in] len bytes will be removed from the output side of the buffer
void ByteStream::pop_output(const size_t len) {
size_t true_len = min(len, _buffer.size());
for(size_t i=0;i!=true_len;i++)
_buffer.pop_front();
_bytes_read+=true_len;
if(buffer_empty() && input_ended())
_eof = true;
}
//! 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(const size_t len) {
string ret_str{};
ret_str = peek_output(len);
pop_output(len);
return ret_str;
}
void ByteStream::end_input() {
_input_end=true;
if(buffer_empty())
_eof = true;
}
bool ByteStream::input_ended() const { return _input_end; }
size_t ByteStream::buffer_size() const { return _buffer.size(); }
bool ByteStream::buffer_empty() const { return _buffer.size() == 0; }
bool ByteStream::eof() const { return (_eof == true) && (_buffer.size()==0); }
size_t ByteStream::bytes_written() const { return _bytes_written; }
size_t ByteStream::bytes_read() const { return _bytes_read; }
size_t ByteStream::remaining_capacity() const {
return _capacity-_buffer.size();
}
但是我这个check时候前四个样例死活过不去,然后把别人答案复制粘贴上去还是前四个过不去,我就放弃了,不知道哪出问题
现在知道为啥了,在测试之前没有make -j4,原来写的是对的