[读书笔记]网络协议 —————— TCP/IP
---------------------------------- IP ----------------------------------
IP is the workhorse protocol of the TCP/IP protocol suite. All TCP, UDP, ICMP and IGMP data gets transmitted as IP datagrams. IP provideds an unreliable, connectionless datagram delivery service.
By unreliable we mean there are no guarantees that an IP datagram successfully gets to its destination. IP provides a best effort service. When something goes wrong, such as a router temporarily running out of buffers, IP has a simple error handling algorithm: throw away the datagram and try to send an ICMP message back to the source. Any required reliability must be provided by the upper layers(e.g. TCP).
The term connectionless means that IP does not maintain any state information about grams. This also means that IP datagrams can get delivered out of order. If a source sends two consecutive datagrams(first A, then B) to the same destination, each is routed independently and can take different routes, with B arriving before A.
Host routing can be simple: the destination is either on a directly connected network, in which case the datagram is sent derectly to the destination, or a default router is chosen.
Hosts and routers have a routing table that is used for all routing decisions. There are three types of routes in the table: host specific, network specific, and optional default routes. There is a priority to the entries in a routing table. A host route will be chosen over a network router, and a default route is used only when no other route exists to the destination.
IP routing is done on a hop-by-hop basis. The destination IP address never changes as the datagram proceeds through all the hops, but the encapsulation and destination link-layer address can change on each hop. Most hosts and many routers use a default next-hop router for all nonlocal traffic.
---------------------------------- TCP ----------------------------------
Even though TCP and UDP use the same network layer (IP), TCP provides a totally different service to the application layer than UDP does. TCP provides a connection-oriented, reliable, byte stream service.
The term connection-oriented means the two applications using TCP (normally considered a client and a server) must establish a TCP connation with each other before they can exchange data. The typical analogy is dialing a telephone number, waiting for the other party to answer the phone and say “hello,” and then saying who’s calling.
TCP provides reliability by doing the following:
Ø The application data is broken into what TCP considers the best sized chunks to send. This is totally different from UDP, where each write by the application generates a UDP datagram of that size. The unit of information passed by TCP to IP is called a segment.
Ø When TCP sends a segment it maintains a timer, waiting for the other end to acknowledge reception of the segment. If an acknowledgment isn’t received in time, the segment is retransmitted.
Ø When TCP receives data from the other end of the connection, it sends an acknowledgment. This acknowledgment is not sent immediately, but normally delayed a fraction of a second.
Ø TCP maintains a checksum on its header and data. This is an end-to-end checksum whose purpose is to detect any modification of the data in transit. If a segment arrives with an invalid checksum, TCP discards it and doesn’t acknowledgment receiving it. (It expects the sender to time our and retransmit.)
Ø Since TCP segments are transmitted as IP datagrams, and since IP datagrams can arrive out of order, TCP segments can arrive out of order. A receiving TCP resequences the data if necessary, passing the received data in correct order to the application.
Ø Since IP datagrams can get duplicated, a receiving TCP must discard duplicate data.
Ø TCP also provides flow control. Each end of a TCP connection has a finite amount of buffer space. A receiving TCP only allows the other end to send as much data as the receiver has buffers for. This prevents a fast host from taking all the buffers on a slower host.
A stream of 8-bit bytes is exchanged across the TCP connection between the two applications. There are no record markers automatically inserted by TCP. This is what we called a byte stream service. If the application on one end writes 10 bytes, followed by a write of 20 bytes, followed by a write of 50 bytes, the application at the other end of the connection cannot tell what size the individual writes were. The other end may read the 80 bytes in four reads of 20 bytes at a time. One end puts a stream of bytes into TCP and the same, identical stream of bytes appears at the other end.
Also, TCP does not interpret the contents of the bytes at all. TCP has no idea if the data bytes being exchanged are binary data, ASCII characters, EBCDIC characters, or whatever. The interpretation of this byte stream is up to the applications on each end of the connection.
TCP provides a reliable, connection-oriented, byte stream, transport layer service. TCP packetizes the user data into segments, sets a timeout any time it sends data, acknowledges data received by the other end, reorders out-of-order data, discards duplicate data, provides end-to-end flow control, and calculates and verifies a mandatory end-to-end checksum.
TCP is used by many of the popular applications, such as Telnet, Rlogin, FTP, and electronic mail (SMTP).