Skip to content

Commit 814b3d6

Browse files
committed
Update net.md guide with screenshots and update guide document
1 parent 1c1046d commit 814b3d6

File tree

4 files changed

+31
-87
lines changed

4 files changed

+31
-87
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
.*.swp
23
.vscode
34
*.dSYM

guides/net.md

+30-87
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,62 @@
11
## General Information about Networking
22

3-
Before learning about the web server, let's take a look at some general
4-
information about how networking works. Some of these terms will be familiar to
5-
you, and we'll expand their definitions a bit.
3+
Before learning about the web server, let's take a look at some general information about how networking works. Some of these terms will be familiar to you, and we'll expand their definitions a bit.
64

75
### Networking Protocols
86

9-
_This is background information. You will not need to use this directly in the
10-
web server._
7+
_This is background information. You will not need to use this directly in the web server._
118

12-
A _protocol_ is an agreement between two programs about how they will
13-
communicate. For the Internet, most protocols take the form of "If you send me
14-
_x_, I'll send you back _y_." Internet-related protocols are clearly written
9+
A _protocol_ is an agreement between two programs about how they will communicate. For the Internet, most protocols take the form of "If you send me _x_, I'll send you back _y_." Internet-related protocols are clearly written
1510
down in specifications, known as an _RFC_.
1611

17-
When you send some data out on the network, that data is wrapped up in several
18-
layers of additional data that provide information about data integrity,
19-
routing, and so on.
12+
When you send some data out on the network, that data is wrapped up in several layers of additional data that provide information about data integrity, routing, and so on.
2013

21-
At the highest level, you have your data that you want to transmit. As it is
22-
prepared for transmission on the network, the data is _encapsulated_ in other
23-
data to help it arrive at its destination. Any particular piece of data will be
24-
wrapped, partially unwrapped, and re-wrapped as it moves from wire to wire
25-
across the Internet to its destination.
14+
At the highest level, you have your data that you want to transmit. As it is prepared for transmission on the network, the data is _encapsulated_ in other data to help it arrive at its destination. Any particular piece of data will be
15+
wrapped, partially unwrapped, and re-wrapped as it moves from wire to wire across the Internet to its destination.
2616

27-
The act of wrapping data puts a new _header_ on the data. This header
28-
encapsulates the original data, _and all the headers that have been added before
29-
it_.
17+
The act of wrapping data puts a new _header_ on the data. This header encapsulates the original data, _and all the headers that have been added before it_.
3018

31-
Here is an example of a fully-encapsulated HTTP data packet.
19+
![Table of Protocols](table_of_internet_protocols.png)
3220

33-
```
34-
+-----------------+
35-
| Ethernet Header | Deals with routing on the LAN
36-
+-----------------+
37-
| IP Header | Deals with routing on the Internet
38-
+-----------------+
39-
| TCP Header | Deals with data integrity
40-
+-----------------+
41-
| HTTP Header | Deals with web data
42-
+-----------------+
43-
| <h1>Hello, worl | Whatever you need to send
44-
| d!</h1> |
45-
| |
46-
+-----------------+
47-
```
21+
The details of what data exists in each header type is beyond the scope of what most people need to know. It is enough to know the short description of what each does.
4822

49-
The details of what data exists in each header type is beyond the scope of what
50-
most people need to know. It is enough to know the short description of what
51-
each does.
23+
![Protocol Stack](protocol_stack.png)
5224

53-
As the data leaves your LAN and heads out in the world, the Ethernet header will
54-
be stripped off, the IP header will be examined to see how the data should be
55-
routed, and another header for potentially a different protocol will be put on
56-
to send the traffic over DSL, a cable modem, or fiber.
25+
As the data leaves your LAN and heads out in the world, the Ethernet header, which is responsible with routing specifically over the Local Area Network, will be stripped off, the IP header will be examined to see how the data should be routed, and another header for potentially a different protocol will be put on to send the traffic over DSL, a cable modem, or fiber.
5726

5827
The Ethernet header is created and managed by the network drivers in the OS.
5928

6029
### Sockets
6130

62-
_This is background information. You will not need to use this directly in the
63-
web server. This code is written for you._
31+
_This is background information. You will not need to use this directly in the web server. This code is written for you._
6432

65-
Under Unix-like operating systems, the _sockets API_ is the one used to send
66-
Internet traffic. It supports both the TCP and UDP protocols, and IPv4 and IPv6.
33+
Under Unix-like operating systems, the _sockets API_ is the one used to send Internet traffic. It supports both the TCP and UDP protocols, and IPv4 and IPv6.
6734

6835
The sockets API gives access to the IP and TCP layers in the diagram above.
6936

70-
A _socket descriptor_ is a number used by the OS to keep track of open
71-
connections. It is used to send and receive data. In our web server, this
72-
variable is called `fd`.
37+
A _socket descriptor_ is a number used by the OS to keep track of open connections. It is used to send and receive data. In our web server, this variable is called `fd`.
7338

7439
You can create a new socket (socket descriptor) with the `socket()` system call.
7540

76-
Once created you still have to _bind_ it to a particular IP address (which the
77-
OS associates with a particular network card). This is done with the `bind()`
78-
system call.
41+
Once created you still have to _bind_ it to a particular IP address (which the OS associates with a particular network card). This is done with the `bind()` system call.
7942

80-
Once bound, you can read and write data to the socket using the `recv()` and
81-
`send()` system calls.
43+
Once bound, you can read and write data to the socket using the `recv()` and `send()` system calls.
8244

8345
* See also [Beej's Guide to Network Programming](https://beej.us/guide/bgnet/)
8446

8547
## HTTP
8648

87-
_In the webserver, you will be writing code that parses down strings that hold
88-
HTTP requests, and builds strings that hold HTTP responses. Study what an HTTP
89-
request and response look like._
49+
_In the webserver, you will be writing code that parses down strings that hold HTTP requests, and builds strings that hold HTTP responses. Study what an HTTP request and response look like._
9050

91-
The final piece of information needed for web traffic is the _HyperText
92-
Transport Protocol_ (HTTP). While TCP deals with general data integrity and IP
93-
deals with routine, HTTP is concerned with `GET` and `POST` requests of web
94-
data.
51+
The final piece of information needed for web traffic is the _HyperText Transport Protocol_ (HTTP). While TCP deals with general data integrity and IP deals with routine, HTTP is concerned with `GET` and `POST` requests of web data.
9552

96-
Like the other stages of networking, HTTP adds a header before the data it wants
97-
to send with the packet. Like IP and TCP, this header has a well-defined
98-
specification for exactly what needs to be sent.
53+
Like the other stages of networking, HTTP adds a header before the data it wants to send with the packet. Like IP and TCP, this header has a well-defined specification for exactly what needs to be sent.
9954

100-
Though the specification is complex, fortunately only a small amount of information
101-
is needed to implement a barebones version.
55+
Though the specification is complex, fortunately only a small amount of information is needed to implement a barebones version.
10256

103-
For each _HTTP request_ from a client, the server sends back an _HTTP
104-
response_.
57+
For each _HTTP request_ from a client, the server sends back an _HTTP response_.
10558

106-
Here is an example HTTP `GET` request and response using version 1.1 of the HTTP
107-
protocol getting the page `http://lambdaschool.com/example`:
59+
Here is an example HTTP `GET` request and response using version 1.1 of the HTTP protocol getting the page `http://lambdaschool.com/example`:
10860

10961
```
11062
GET /example HTTP/1.1
@@ -124,11 +76,9 @@ Content-Type: text/html
12476
<!DOCTYPE html><html><head><title>Lambda School ...
12577
```
12678

127-
The end of the header on both the request and response is marked by a blank line
128-
(i.e. two newlines in a row).
79+
The end of the header on both the request and response is marked by a blank line (i.e. two newlines in a row).
12980

130-
If the file is not found, a `404` response is generated and returned by the
131-
server:
81+
If the file is not found, a `404` response is generated and returned by the server:
13282

13383
```
13484
HTTP/1.1 404 NOT FOUND
@@ -140,21 +90,14 @@ Content-Type: text/plain
14090
404 Not Found
14191
```
14292

143-
If you've ever looked in the Network panel of your web browser's debugger, some
144-
of these headers might look familiar.
93+
If you've ever looked in the Network panel of your web browser's debugger, some of these headers might look familiar.
14594

14695
Important things to note:
14796

14897
* For HTTP/1.1, the request **must** include the `Host` header.
149-
* The second word of the first line of the response gives you a success or
150-
failure indicator.
151-
* `Content-Length` gives the length of the request or response body, not
152-
counting the blank line between the header and the body.
153-
* `Content-Type` gives you the MIME type of the content in the body. This is how
154-
your web browser knows to display a page as plain text, as HTML, as a GIF
155-
image, or anything else. They all have their own MIME types.
156-
* Even if your request has no body, a blank line still **must** appear after the
157-
header.
158-
* `Connection: close` tells the web browser that the TCP connection will be
159-
closed after this response. This should be included.
98+
* The second word of the first line of the response gives you a success or failure indicator.
99+
* `Content-Length` gives the length of the request or response body, not counting the blank line between the header and the body.
100+
* `Content-Type` gives you the MIME type of the content in the body. This is how your web browser knows to display a page as plain text, as HTML, as a GIF image, or anything else. They all have their own MIME types.
101+
* Even if your request has no body, a blank line still **must** appear after the header.
102+
* `Connection: close` tells the web browser that the TCP connection will be closed after this response. This should be included.
160103
* The `Date` should be the date right now, but this field is optional.

guides/protocol_stack.png

24.8 KB
Loading
93.7 KB
Loading

0 commit comments

Comments
 (0)