CS144 lab0 笔记 📒

如果是 MacBook (with the ARM64 M1 chip)

sudo -i切换为 root 用户

2、输入dhclient -v

再输入ifconfig就可以看到多了一个 ip 地址

有的同学说重启后命令失效,请参考下面的方法: 1、执行vim ~/.bashrc 2、在最后一行添加dhclient -v 3、保存退出 4、执行source ~/.bashrc使其立即生效

Preparations

cs144 官网

Virtual machine setup instructions

Lab_FAQs

origin https://github.com/cs144/sponge (fetch) origin https://github.com/cs144/sponge (push)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cs144@cs144vm:~/sponge/build$ git config --list
user.email=FacundoChan01@gmail.com
user.name=Facundo Chan
core.editor=vim
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/cs144/sponge
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.lab0-startercode.remote=origin
branch.lab0-startercode.merge=refs/heads/lab0-startercode
branch.lab1-startercode.remote=origin
branch.lab1-startercode.merge=refs/heads/lab1-startercode
branch.lab2-startercode.remote=origin
branch.lab2-startercode.merge=refs/heads/lab2-startercode

推荐使用:vscode + ssh 连接虚拟机

Lab Checkpoint 0: networking warmup

2.1 Fetch a Web page

1
2
3
4
5
$ telnet cs144.keithw.org http
Trying 104.196.238.229...
Connected to cs144.keithw.org.
Escape character is '^]'.

输入下面的内容(太慢可能会关闭,可以先输入好后复制粘贴)。Hit the Enter key two times

1
2
3
GET /hello HTTP/1.1
Host: cs144.keithw.org
Connection: close
1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 200 OK
Date: Fri, 28 Oct 2022 16:55:15 GMT
Server: Apache
Last-Modified: Thu, 13 Dec 2018 15:45:29 GMT
ETag: "e-57ce93446cb64"
Accept-Ranges: bytes
Content-Length: 14
Connection: close
Content-Type: text/plain

Hello, CS144!
Connection closed by foreign host.

2.3 Listening and connecting

可能出现下面的错误

1
2
cs144@cs144vm:~/sponge/build$ netcat -v -l -p 9090
netcat: getnameinfo: Temporary failure in name resolution

The problem could be with the DNS resolver. Try updating the namespace in /etc/resolv.conf as follows.

1
nameserver 8.8.8.8

3.4 Writing webget

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void get_URL(const string &host, const string &path) {
// Your code here.

// initialize variables
TCPSocket Tsocket;
Address addr = Address(host, "http");

Tsocket.connect(addr);
// Write a string, possibly blocking until all is written
string msg = "GET " + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
Tsocket.write(msg);
// socket.h -> SHUT_RD/WR/RDWR,先用SHUT_WR关闭写,避免服务器等待
Tsocket.shutdown(SHUT_WR);
while(!Tsocket.eof()){
cout << Tsocket.read();
}
Tsocket.close();
}

4 An in-memory reliable byte stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// byte_stream.hh
class ByteStream {
private:
// Your code here -- add private members as necessary.

std::deque<char> _deque{};
size_t _capacity;
// size_t _buffer_size; // == _deque.size() [dynamic]
size_t _bytes_written;
size_t _bytes_read;
// size_t _remaining_capacity; // _
bool _input_ended;
// bool _buffer_empty;
// bool _eof; // _buffer_empty && _input_ended

...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
ByteStream::ByteStream(const size_t capacity)
: _capacity(capacity), _bytes_written(0), _bytes_read(0),
_input_ended(false)
{
// DUMMY_CODE(capacity);
}

size_t ByteStream::write(const string &data) {
// DUMMY_CODE(data);
size_t input_size = min(remaining_capacity(), data.length());
for(size_t i = 0; i < input_size; ++i){
_deque.emplace_back(data[i]);
}
_bytes_written += input_size;
return input_size;
}

//! \param[in] len bytes will be copied from the output side of the buffer
string ByteStream::peek_output(const size_t len) const {
// DUMMY_CODE(len);
string output = "";
size_t output_size = min(buffer_size(), len);
for(size_t i = 0; i < output_size; ++i){
output += _deque[i];
}
// _bytes_read += output_size; // READ-ONLY!
return output;
}

//! \param[in] len bytes will be removed from the output side of the buffer
void ByteStream::pop_output(const size_t len) {
// DUMMY_CODE(len);
size_t pop_size = min(buffer_size(), len);
for(size_t i = 0; i < pop_size; ++i){
_deque.pop_front();
}
_bytes_read += pop_size;
}

//! 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) {
// DUMMY_CODE(len);
string ret = peek_output(len);
pop_output(len);
return ret;
}

void ByteStream::end_input() { _input_ended = true; }

bool ByteStream::input_ended() const { return _input_ended; }

size_t ByteStream::buffer_size() const { return _deque.size(); }

bool ByteStream::buffer_empty() const { return buffer_size() == 0; }

bool ByteStream::eof() const { return buffer_empty() && input_ended(); }

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(); }

CS144 lab0 笔记 📒
http://example.com/2023/10/28/CS144-note-lab0/
作者
臣皮蛋
发布于
2023年10月28日
许可协议