The principle and introduction of TCP/IP protocol for Python Web learning notes

Source: Internet
Author: User
Tags ack ming

HTTP, FTP, SMTP, Telnet, etc. protocol, Oh! That HTTP protocol is the protocol that's used to access the Web. Actually ... You know, we should understand him from the reality, understand the function and function of the network protocol, and then from the fundamental point of view he is concrete how to achieve. We can start with HTTP, FTP, DHCP, DNS, ARP and other common protocols to understand how it works, and then follow this attitude to further understand the other protocols. Let's get down to the chase.

First, what is TCP/IP?

As the name implies, TCP/IP (transmission Control protocol/internet Protocol), Transmission Protocol/Internet Protocol, TCP/IP is the core technology of modern Internet, defined by IETF. All systems, terminals, lines, users, developers, must abide by the rules of the TCP/IP protocol family! The fundamental law of the IP World is the family TCP/IP protocol. Here I have the agreement family, like the happy family, not only we are familiar with the He jiong, Sheenah. Not only the host, but also the director, camera and other staff. TCP/IP is also like a group, protocol family. It also includes HTTP, Telnet, FTP, RIP, UDP, and so on. Its name comes from the two most important protocols-------TCP and IP. They are in the second and third tiers of the TCP/IP model.

Second, OSI Reference Model and TCP/IP Reference Model

The IP world is the process of the communication entity, and the communication entity is to be layered. The benefits of layering are:

1, each layer is independent.

2, good flexibility.

3, the structure can be divided open.

4, easy to implement and maintain.

5, can promote standardization work.

Therefore, the communication entity must be divided into layers to ensure that various network technologies can coexist harmoniously and cooperate well with each other, and constantly inspire technological innovation. So in 1984, the International Organization for Standardization (ISO) set up a very abstract hierarchical structure, which is known as the Iso/osi (International Organization for Standardization Development Network Architecture), which is very helpful for us to analyze network communication.

Physical layer (first layer): The physical layer solves the most basic transmission channel, which involves the mechanical, electrical/optical, functional and procedural characteristics required to establish, maintain and release physical links, such as how to resist the loss of optical cable, how to improve the transmitting power of wireless equipment, why the twisted pair should have a shielding layer.

Data Link layer (second layer): The data link in the physical layer on the basis of "bit" service, in the adjacent network nodes directly provide simple, transmission in the frame unit of data, but also responsible for flow control, error control (signal will be due to machine, electrical gas and other causes error). We often hear of "Ethernet frame" to know that Ethernet belongs to this layer.

Network layer (third layer): The network layer provides two services to the upper-----Transport layer, a network service called "Connection-oriented", a network service called "No Connection". The network layer is responsible for four major tasks: 1. Routing 2. Congestion Control 3. Inter-LAN interconnection 4. Statistics and control. The IP protocol we are talking about is playing an important role in this layer.

Transport Layer (layer Fourth): The task of the Transport layer is to provide users with reliable, transparent end-to-end data transmission, and error control and flow control mechanisms. Because its existence session layer, the presentation layer, the application layer design does not need to consider the underlying detail, therefore plays the "Connecting link" the function. If two computers A, B need to communicate, then A, B direct communication between the transport layer, and between A and b if there are several network nodes, such as Router x, Y, z then A and x,x and Y,y and Z,z and B are to be the third layer of communication. TCP and UDP belong to the transport layer.

Session layer (fifth level): The session layer provides many value-added services, such as interactive conversation management, allowing one-way interaction, two-channel switching, two-way simultaneous reply, management user login to the remote time-sharing system, transfer of files between two machines, synchronization control, etc.

Presentation layer (layer Sixth): The presentation layer handles the representation of data exchanged between communication processes, including syntax conversions, conversion of data formats, encryption and decryption, compression and decompression, and so on.

Layer Seventh (Application layer): The application layer is the product of the so-called stand on the shoulders of giants, the ultimate goal of all the underlying protocols is to provide the application layer with reliable means of transmission, the underlying protocol does not directly deceive the user of any actual needs. Our daily use of e-mail, transmission files, traffic Web pages, interactive and timely information, etc. belong to the application layer, is the most intuitive user experience services.

The so-called "survival of the fittest, the fittest", OSI because the system is more complex, less convenient computer software implementation, gradually quit people's attention, TCP/IP has been widely used.

In contrast to the seven-storey structure of Iso/osi, the reader will wonder: why is there no presentation layer and session layer? Indeed, these two layers are not required in the TCP/IP reference model. There is also a four-layer model that says the data link layer and the physical layer are represented as network interface layers. The core part of TCP\IP architecture is the above three layers: application layer, Transport layer, network layer.

(1) Network interface layer: TCP/IP does not strictly define this layer, it only requires a provider to its upper-----network layer of a provider, in order to pass IP packets on it. Because this level is not defined, its specific implementation will vary depending on the type of network. Ethernet is the most common form of the data link layer in IP communication, in addition to the PPP, HDLC and other 10gbit/s below the Ethernet is generally applied to the LAN, and PPP, HDLC is used in the majority of the WAN.

(2) Network layer: Commonly known as the IP layer, it handles communication between machines. IP is an unreliable, no-connection protocol that receives requests from the transport layer and transmits a packet with the destination address information. IP is responsible for assigning an address to each computer or terminal on the Internet and transmitting the information to the correct destination in the form of an IP packet. This is the core of TCP/IP. The most critical of the IP protocol family is the routing protocol.

(3) Transport Layer: Transport Layer Control Protocol (TCP) and User Datagram Protocol (UDP) are important protocols for this layer. TCP is a connection-oriented, reliable protocol. It sends a stream of bytes from one host to other hosts on the Internet without error. At the sending end, it is responsible for dividing the stream of bytes sent down into message segments and passing it to the lower layer. At the receiving end, it is responsible for reorganizing the received message to the upper layer. TCP also handles end-to-end traffic control (sliding window mechanism). UDP is an unreliable, non-connected protocol, which is mainly suitable for applications where messages are not ordered and flow control is required. (for example, in the transmission of information like important documents and text messages and so on for TCP and video transmission can use UDP).

Each level also corresponds to a different protocol:

Third, TCP message format

The TCP message includes the header (header) and the Data section, where the specific fields of the header are as follows.

(1) The source port number and destination Port field-----Each account for 16bit.

(2) The ordinal field-----seq sequence number, accounting for 32bit. Each byte in the data stream transmitted in a TCP connection is numbered, and the value of the ordinal field is the ordinal of the first byte of the data sent by this section.

(3) Confirm the serial number-----ACK sequence number, 32bit, is expected to receive the other party's write the next message segment of the data of the first byte ordinal. Only the ACK flag bit is 1, confirming that the ordinal field is valid, ack=seq+1.

(4) Sign: There are 6 of the following

URG-----Emergency signal indicates the emergency pointer (urgent pointer), which can tell the system that there is an emergency data in this segment and should be transmitted as soon as possible.

ACK-----Confirmation signal is valid only if the Ack=1 field is confirmed. The confirmation number is invalid when ack=0.

PSH-----Push signals receive TCP push bit 1 of the message segment, then delivered to the receiving application process as soon as possible, without caching.

RST-----Reset Signal When rst=1, indicates a fatal error in the TCP connection, releases the connection, and then re-establishes the connection.

SYN-----Synchronous Signal SYN Indicates a connection request or connection receive message.

FIN-----Termination signal is used to release a connection. When Fin=1, it indicates that the data of the sending end of the message segment has been sent and the transport connection is released.

(5) The data offset-----is 4bit, which is from the beginning of the data of the message segment from the beginning of the TCP message segment position.

(6) Window field-----Account for 16bit, to control the amount of data sent by the other side, in bytes. The end of a TCP connection determines its own receive window size based on the size of the cache space set, and then notifies the other party to determine the upper limit of the sending window.

(7) Inspection and-----accounted for 16bit, test the header and data of the message section, before the test to add 12 bytes of pseudo-header.

(8) Emergency pointer field-----16bit. The emergency pointer indicates the ordinal of the last byte of the emergency data in this section.

(9) Option field-----No fixed length, TCP only specifies one option maximum segment length MSS.

(10) the reserved field-----6-bit, temporarily no.

Four, three handshake and four waves of TCP

The connection and establishment of TCP are all based on the client server mode. The process that initiates the connection is called the client. A passive wait connection is established as a server.

Three handshake required to establish TCP connection

First handshake: Client (Host a) wants server (Host B) to send a connection request, in this package flag bit syn=1, send ordinal seq=x, x=200,client into Syn_send state, waiting for the server to confirm.

Second handshake: After the server receives the packet by the flag bit syn=1 knows the client request to establish a connection, the server sets the flag bit SYN and ACK to 1, confirms the ordinal ack=x+1=201, randomly interprets a send ordinal y, making y=500. The data is packets sent to the client to confirm the connection request and the server enters the SYN_RCVD state.

Third handshake: After the client has received the person, check to confirm that the ordinal ACK is x+1=201, the flag bit ACK is 1, if correct, the flag bit ACK 1, confirm the ordinal ack=y+1=501, and send the data packets to the server, The server checks to see if the ACK is y+1=501, the flag bit is 1, and if the connection is successful, the client and server enter the established state.

Image to understand, to join the client is Xiao Ming, the server is small red, Xiao Ming sent a message to the small red mail mark is Seq,syn is the text message inside Xiao Ming want to place object intention. Little Red received, also sent a letter to Xiao Ming, SEQ is also the number of small red mail, ack=seq+1 is the surface of his own has received a message, ACK sign position 1 indicated that Xiao Red agreed to the request of Xiao Ming's object, SYN also said that Little Red has the intention to get along with the object. So the same Li Xiaoming began to send a letter that I will start chasing you. Then the two began to talk about the future. Three-time handshake to the future ~~~~~~! So the boys still have to take the initiative.

(There will be a small discrepancy in image understanding)

Four waves of disconnection from a TCP connection

First wave: When client A to disconnect a TCP connection, send a packet, where the flag bit fin=1,ack=1, send the ordinal seq=x, confirm the sequence number ack=z,client into the fin_wait state.

Second wave: Customer B knows a to disconnect, send a confirmation package, where the flag bit ack=1, send the ordinal seq=z confirm the serial number ack=x+1,server into the close_wait state.

Third Wave: Customer B also disconnects the TCP connection, at which point a packet is sent, where the flag bit fin=1, sending the ordinal seq=z+1

, the server enters the Last_ack state.

Fourth wave: When customer a receives a disconnect request from B, the client enters the TIME_WAIT state, sends a confirmation packet, ack=1 the flag, sends the ordinal seq=x+1, and confirms that the serial number ack=z+2,server into the close state.

Also give a chestnut, do not take small red and Xiao Ming to compare after all just into the state of love, followed by small Tao and small Cui. The man has the money to become bad, the small Tao is a slag male, therefore first signed the divorce agreement also has own some documents (request a person small cui to handle) (ACK), then accompanies own breakup letter (SEQ) sends to the small cui. The ACK is placed 1,fin 1. Xiao Cui received the letter, but also without further ado, first back a letter and explained that has received Xiao Tao's break-up letter and agreed (seq+1) and then the document passed (ACK). Finally, wait for the divorce certificate (ack+1) and then send the divorce certificate to the small Tao (ack=1,fin=1). The last little Tao also shameful laughed at Xiao Cui A (seq+1) ack=1.

V. Common terminology of TCP/IP protocol architecture

(1) Package

A packet (Packet) is a fragment of data transmitted over a network, also known as a grouping. Package is a general term, in different protocols, different levels, packages have different names, such as TCP/IP, Data link layer of packets called frame (frame), IP layer packets called IP datagram, TCP layer packets are often referred to as TCP packets. Most packages consist of Baotou and information: Baotou often includes source address and destination address, package length and type indicator, and so on.

(2) package

The peer layer of different devices relies on encapsulation and encapsulation to achieve mutual communication. Encapsulation is like taking a bath naked and wearing clothes, unpacking is like undressing before bathing, taking off layer after layer until stripped. TCP/IP is also a standard method for packaging and addressing computer data. In data transfer, you can figure out that there are two envelopes, TCP and IP are like envelopes, the information to be passed is divided into segments, each paragraph is crammed into a TCP envelope, and the segment information is recorded and then stuffed into the IP envelope. Similarly, the receiving end is disassembled and verified, and if errors are found, the TCP mechanism will require a re-send. Therefore, TCP/IP can transmit data almost without error in the Internet.

(3) data segment, data packet. Data frame

During communication, the TCP/IP layer has a protocol data unit (PROTOCOL) between the layers to exchange information with each other to ensure communication between network devices. The transmission layer on the basis of the upper layer of data plus the TCP header obtained by the PDU is called the Data segment (Segment), down the data segment to the network layer, and add the IP header to get the PDU called the Packet (Packet), the packet to the data Link layer added header resulting PDU called the data frame ( Frame). The last frame is converted to bit at the physical layer through the network media.

The basic introduction of TCP/IP is here, if you want to learn more students, please do not fear the exertion of persistent learning. It is only through perseverance that there is a harvest.

Reference:

https://zhuanlan.zhihu.com/p/29478691

The principle and introduction of TCP/IP protocol for Python Web learning notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.