preface

Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now

omg

Yesterday I finished the IO flow, we may not like how to see, because said, so simple things, who won’t? Ha ha blogger really can’t, also say not, can only say to use baidu, but I think of their own over, so review it, great oaks from little acorns, slowly come. Today is also one of the most basic concepts, also did not go deep, if there is a need to go deep, after all, the computer network is a university.

An overview of the

Computer network is through transmission medium, communication facilities and network communication protocol, scattered in different places of computer equipment interconnection, resource sharing and data transmission system. Network programming is writing programs to transfer data between two (or more) devices (such as computers) on the Internet. The Java language provides good support for network programming. Through the interface it provides, we can easily program the network.

From which we can extract the following contents:

  • The role of computer networks: resource sharing and information transfer.
  • Composition of a computer network:
    • Computer hardware: computers (large, medium-sized servers, desktops, notebooks, etc.), external devices (routers, switches, etc.), communication lines (twisted-pair cables, optical fibers, etc.).
    • Computer software: Network operating system (Windows 2000 Server/Advance Server, Unix, Linux, etc.), network management software (WorkWin, SugarNMS, etc.), network communication protocol (such as TCP/IP protocol stack, etc.).
  • The multiple computers in a computer network are functionally independent, rather than being unable to exist without the network.

OSI reference model

The connection and communication between different computers can be realized through the computer network, but the communication in the computer network must have some conventions namely the communication protocol, the rate, transmission code, code structure, transmission control steps, error control and other standards. Just as two people must use the same language in order to communicate smoothly, if one person only knows English and the other only knows Chinese, there will be no common language to communicate.

International Organization for Standardization (ISO, International Organization for Standardization) defines the basic framework of network communication protocol, known as OSI(Open System Interconnect) model. There are many communication rules to be formulated, such as how does COMPUTER A find computer B, whether computer B needs to give feedback when computer A sends information to computer B, and what is the format of data that computer A sends to computer B? The content is too complex, so the OSI model makes standards less complex by dividing these communication standards into layers, each addressing a category of problems. OSI model developed seven layers of standard model, respectively: application layer, presentation layer, session layer, transmission layer, network layer, data link layer, physical layer.

The OSI seven-layer protocol model is shown in the figure below:

  • The physical layer

    • The physical layer is at the lowest level of OSI and is the foundation of the entire open system. The physical layer is concerned with the raw bits transmitted on the communication channel. Its function is mainly to provide the data transmission path and data transmission for the data terminal device.
  • Data link layer

    • The main task of data link layer is to realize the reliable transmission between adjacent nodes in computer network. After adding the original and faulty physical transmission line to the data link protocol, the logical reliable data link is formed. The functions to be completed include link management, framing, error control, and flow control. Framing defines the original bit stream of the physical layer, and the data link layer can also deal with the loss of frames.
  • The network layer

    • The network layer involves reliable network transmission between source host nodes and destination host nodes. Its functions mainly include routing, network addressing, traffic control, congestion control, network interconnection and so on.
  • The transport layer

    • The transport layer acts as a link between the preceding and the following and involves reliable information transmission from source node to destination node. The transport layer deals with the establishment and release of connections across networks, requiring three handshakes to establish a connection and four waves to release a connection for the underlying unreliable network.
  • Session layer and presentation layer

    • Between the main function of the session layer is responsible for the application to establish, maintain and interrupt conversation, as well as between the equipment and node of session control, coordinate the communication between system and service, and through the provision of simplex, half-duplex and full-duplex three different ways of communication, communication between the systems and services in order.

    • The presentation layer is concerned with the format definition of the transmitted data information, and its main function is to transform the information provided by the application layer into a form that can be understood by all, providing unified representation of character code, data format, control information format, encryption, etc.

  • The application layer

    • The application layer is the highest layer of OSI and provides services directly to application processes. Its function is to realize the communication between multiple system application processes and complete a series of services required for business processing.

TCP/IP reference model

TCP/IP, short for Transmission Control Protocol/Internet Protocol, is the most basic Protocol of the Internet, the foundation of the Internet.

TCP/IP is an open network protocol cluster. Its name is derived from the most important network layer IP protocol and transport layer TCP protocol. The TCP/IP protocol defines the standard for how electronic devices are connected to the Internet and how data is transferred between them. The TCP/IP reference model adopts a hierarchical structure of four layers. Each layer calls the protocol provided by the next layer to fulfill its requirements. These four layers are network interface layer, network layer (IP layer), transport layer (TCP layer), and application layer.

  • Network interface layer

    • TCP/IP does not give a specific description of the network interface layer, which corresponds to the physical layer and data link layer of the OSI reference model
  • Network Layer (IP layer)

    • The network layer is the core of the TCP/IP stack. Its function is to send packets to the target network or host. At the same time, in order to send packets as quickly as possible, it may be necessary to pass packets along different paths simultaneously. As a result, packets may arrive in a different order than they are sent in, requiring the upper layer to sort the packets. In addition to the routing function, the network layer can also connect different types of networks (heterogeneous networks). In addition, the Internet layer also needs to complete the congestion control function.
  • Transport Layer (TCP layer)

    • The TCP layer is responsible for establishing end-to-end connections and reliable communication between application processes, and it exists only with the end node. The TCP layer involves two protocols, TCP and UDP. TCP provides connection-oriented services, orderly and reliable transmission of byte streams, and implements connection management, error control, flow control, and congestion control. UDP provides connectionless services for network applications that do not need or cannot implement connection-oriented services.
  • The application layer

    • The application layer provides services for various network applications on the Internet.

Network protocol

Just as people communicate with each other by following certain rules (such as language), computers communicate with each other because they all follow certain rules, namely network protocols.

The OSI reference model and TCP/IP model have many different network protocols at different levels, as shown in the figure:The relationship between network protocols is shown as follows:

IP protocol (at the network layer)

The FUNCTION of IP protocol is to prepare various data packets to the other party correctly, two important conditions are IP address and MAC address. As IP address is a rare resource, it is impossible for everyone to have an IP address, so our usual IP address is the IP address generated by the router, which will record our MAC address. MAC addresses are globally unique. For example, an IP address is like the address of our neighborhood, and a MAC address is the person in that room in that building. The IP address is in IPv4 format and is being transferred to IPv6

The iP protocol is responsible for forwarding a packet from one network segment to another that is, switching between different routes

TCP (Transport layer)

TCP (Transmission Control Protocol) is a connection-oriented transport layer protocol. The TCP layer is an intermediate layer above the IP layer and below the application layer. Reliable, pipe-like connections are often needed between application layers of different hosts, but the IP layer does not provide such a streaming mechanism, but rather unreliable packet switching. TCP uses byte stream to transmit data.

TCP packet format

A TCP packet segment consists of the protocol header and data. The fixed part of the protocol header is 20 bytes, and the options part is followed by the fixed part of the header.

The following are the meanings of each field in the header of the packet segment:

  • Source port number and destination port number: each two bytes, the port is the transport layer and application layer service interface, used to find the process of the sender and receiver, generally speaking, through – – port number and IP address, can uniquely determine a TCP connection, in network programming, commonly known as a socket interface.
  • Serial number: Seq serial number, consisting of 4 bytes and 32 bits. Identifies the byte stream of data sent from the TCP sender to the TCP receiver. This is flagged when the initiator sends data.
  • Confirmation number: Ack number, consisting of 4 bytes and 32 bits. Contains the next sequence number that the end that sent the acknowledgement expects to receive. The confirmation sequence number field is valid only when the ACK flag bit is 1. Therefore, the confirmation sequence number should be the sequence number of the last successfully received data byte plus 1, that is, ACK =Seq + 1.
  • Data offset: indicates the length of the TCP header. If no option is available, the value is 20 bytes. The maximum value of the data offset is 60 bytes.

Reserved fields occupy 6 bits and can be ignored for the time being. All values are 0.

  • Flag bit, six
    • URG(emergency) : a value of 1 indicates that the emergency pointer field is valid
    • ACK: 1 indicates that the ACK number field is valid
    • PSH(push) : If the value is 1, the receiver sends this packet segment to the application layer as soon as possible
    • RST: 1 indicates that the TCP connection is faulty and must be rebuilt
    • SYN(synchronization) : Used to synchronize sequence numbers when a connection is established
    • FIN: if the value is 1, the sender ends data transmission and releases the connection
  • Receive window: 2 bytes for flow control and congestion control, representing the current receive buffer size. In computer network, the amount of data sent by the receiver is usually controlled by the receiver’s receiving capacity. One end of a TCP connection determines the value of its receive window based on the size of the buffer and tells the other end the value so that the other end can determine the number of bytes of data to be sent.
  • Checksum: contains 2 bytes, including the header and data.
  • The option is optional. By default, it is not selected.

TCP three-way handshake and four-way wave

TCP is a connection-oriented protocol, so each TCP connection has three phases: connection establishment, data transfer, and connection release. Establishing a connection goes through three steps, often referred to as the “three-way handshake.”

The TCP three-way handshake process is as follows:

  • First handshake (client sends request)

    • The client sends the connection request segment to the server and enters the SYN_SENT state, waiting for the server to confirm. The content of the connection request packet segment is SYN=1, SEq = X. SYN=1 means a TCP packet with the SYN flag at position 1, indicating the port of the server to which the client intends to connect. Seq =x indicates the initial Sequence Number of the client, which is stored in the Sequence Number field of the packet header.
  • Second handshake (server confirmation)

    • The server receives a connection request packet from the client. If the client agrees to establish a connection, the server sends an ACK reply and allocates the TCP cache and variables to the TCP connection. SYN=1, ACK=1, SEq = Y, ACK= x+1. The SYN flag bit and ACK flag bit are both 1. At the same time, the ISN of the customer is set to 1, that is, X +1. Seq =y indicates the initial serial number of the server.
  • Third handshake (confirmation sent by the client)

    • When the client receives the acknowledgement segment from the server, it presents the acknowledgement segment (ACK) to the server and also allocates cache and variables to the connection. After the packet is sent, the client and server enter the ESTABLISHED state and complete the three-way handshake. The client sends the following confirmation packets: ACK=1, SEQ = X +1, ACK= Y +1. ACK=1 indicates the acknowledgement packet segment. Seq =x+1 Indicates the client number plus 1. Ack =y+1, is the initial sequence number field of the ACK sent by the server +1.

Note: the packet sent during the handshake does not contain data. After three handshakes, the client and server will officially start the data transfer. In vernacular, the client sends a message to the server saying I’m going to establish a connection with you, and the server receives a message saying. I allow you to set up a connection, send a message back to the client, and then the client sends a message back to the server saying I know you’ve returned, and then you can send data.

  • Think about it. Why three handshakes? Looks like two would do

If there are only two handshakes, at this point, the connection is established, but the client doesn’t have any data to send, and the server is still waiting for good news, which is a huge waste of resources. A third handshake is required, which can be avoided if the client responds again.

TCP four wave waving process is as follows:

Because the TCP connection is full-duplex, each direction must be closed separately. The principle is that a party can send a FIN to terminate the connection in that direction when it has finished sending its data. Receiving a FIN only means that there is no data flow in that direction, and a TCP connection can still send data after receiving a FIN. The party that closes first performs an active shutdown and the other party performs a passive shutdown.

1. The TCP client sends a FIN to stop data transmission from the client to the server, and the client enters the FIN_WAIT_1 state. Contents of the sent packet: FIN=1, SEq = U; FIN=1: Requests to sever the connection. Seq =u indicates the initial sequence number of the client request.

2. After receiving the FIN, the server sends an ACK to the client. The sequence number is the received sequence number plus 1. Like the SYN, a FIN takes a sequence number; The server enters the CLOSE_WAIT state. Contents of the sent packet: ACK=1, SEq = V, ACK= U +1. ACK=1 indicates an acknowledgement packet. Seq =v indicates the initial serial number of the server. Ack =u+1 Adds 1 to the initial number of the client.

3. The server closes the connection to the client and sends a FIN to the client. The server enters the LAST_ACK state. Contents of the sent packet: FIN=1, ACK=1, SEQ = W, ACK= U +1. FIN=1 indicates the request for disconnection, ACK=1 indicates the confirmation packet, and seq= W indicates the initial sequence number of the server’s request for disconnection.

4. After receiving the FIN packet, the client enters the TIME_WAIT state and sends an ACK packet to the server for confirmation. The server enters the CLOSED state and waves four times. Contents of the sent packets: ACK=1, SEq = U +1, ACK= W +1. ACK=1 indicates an acknowledgement packet. Seq = U +1 indicates the initial sequence number of the client plus 1. ACK= W +1 indicates the initial sequence number of the server plus 1.

  • Note: why three handshakes on connection and four waves on closure?

After receiving a SYN request packet from a client, the server sends a SYN+ACK packet. ACK packets are used for reply, and SYN packets are used for synchronization. However, when the server receives a FIN packet, it may not close the socket immediately. Therefore, the server can only reply an ACK packet to the client, saying, “I received the FIN packet you sent.” I can send FIN packets only after all packets are sent from the server. Therefore, I cannot send FIN packets at the same time. Therefore, I need to wave in four steps.

UDP protocol (Transport Layer)

UDP, the user datagram protocol, is a connectionless transport layer protocol in the TCP/IP protocol cluster.

  • UDP is a connectionless protocol that transmits data without establishing a connection between the source and the end. When it wants to transmit, it simply grabs the data from the application and throws it on the network as quickly as possible. On the sending side, the speed at which UDP can transmit data is limited only by the speed at which the application can generate the data, the power of the computer, and the transmission bandwidth; At the receiving end, UDP queues each message segment, and the application reads one message segment at a time from the queue.

  • Since no connection is established for data transmission, there is no need to maintain connection state, including sending and receiving state, so one server can simultaneously transmit the same message to multiple clients.

  • The header of a UDP packet is short, only 8 bytes, with little overhead compared to the 20 bytes of a TCP packet.

  • Throughput is not regulated by the congestion control algorithm, but is limited only by the rate of data generated by the application software, transmission bandwidth, and the performance of the source and terminal hosts.

  • UDP uses best effort delivery, that is, reliable delivery is not guaranteed, so hosts do not need to maintain complex linked state tables.

  • UDP is packet oriented. The UDP packets sent by the sender to the application are delivered to the IP layer after the header is added. Instead of splitting or merging, the boundaries of these messages are preserved, so the application needs to select the appropriate message size.

UDP protocol Format

UDP consists of two parts: header and data. The header contains only 8 bytes, including the source and destination ports, the length (UDP for datagrams), and the checksum.

Differences between TCP and UDP

  • TCP is connection-based while UDP is connectionless.
  • More TCP and less UDP are required for system resources.
  • UDP program structure is simple;
  • TCP is in stream mode, while UDP is in datagram mode.
  • TCP ensures data correctness, while UDP may lose packets. TCP guarantees data order, while UDP does not.

HTTP protocol (Application Layer)

HTTP, hypertext transfer protocol, is the most widely used network protocol on the Internet. HTTP is an application layer protocol. It is a request/response protocol based on TCP. HTTP is a standard protocol for communication between Web browsers and Web servers. HTTP specifies how the client establishes a connection with the server, how the client requests data from the server, how the server responds to the request, and finally how the connection is closed. HTTP connections use TCP/IP to transfer data.

For each request from client to server, there are four steps:

  • By default, the client opens a TCP connection to the server on port 80, and other ports can be specified in the URL.
  • The client sends a message to the server requesting resources on the specified path. The resource includes a header and optionally (depending on the nature of the request) a blank line followed by the data for the request.
  • The server sends the response to the client. The response begins with the response code, followed by the header containing the data, a blank line, and the requested document or error message.
  • The server closes the connection.

The HTTP message

HTTP is a request/response protocol based on TCP. The following describes the format of HTTP packets. HTTP packets are classified into request packets and response packets.

The format of the HTTP request packet is as follows:

An HTTP request packet consists of the request line, header line, and entity body. The browser sends an HTTP request packet to the server. In the diagram above, SP represents whitespace and CR LF represents carriage return and line feed. The following is an example of an HTTP request to view a server in Google Chrome:HTTP response packet format:The figure above shows an HTTP response message, which consists of a status line, a header line, and an entity body. The following figure shows an example of HTTP response packets:

Difference between HTTP and HTTPS

HTTPS (Hyper Text Transfer Protocol over Secure Socket Layer) is a Secure HTTP channel. In short, it is the Secure version of HTTP. That is, add SSL layer to HTTP, and SECURE Sockets Layer (SSL) is the basis of HTTPS security. Therefore, SSL is required for details of encryption. It is a URL scheme (abstract identifier system), syntactically similar to HTTP: system, used for secure HTTP data transfer. HTTPS: THE URL indicates that it uses HTTP, but HTTPS has a different default port than HTTP and an encryption/authentication layer (between HTTP and TCP).

Hypertext Transfer Protocol the HTTP protocol is used to transfer information between Web browsers and Web servers. HTTP sends content in plaintext and does not provide any data encryption. If an attacker intercepts a packet transmitted between a Web browser and a Web server, the attacker can directly understand the information in the packet. Therefore, HTTP is not suitable for transmitting sensitive information, such as credit numbers and passwords.

To address this shortcoming of HTTP, you need to use another protocol: secure Socket Layer Hypertext Transfer protocol HTTPS. To secure data transmission, HTTPS adds SSL to HTTP. SSL relies on certificates to verify the identity of the server and encrypts the communication between the browser and the server.

The differences between HTTPS and HTTP are as follows:

  • For HTTPS, you need to apply for a certificate from a CA. Generally, a few free certificates need to be paid.
  • HTTP is a hypertext transmission protocol, and information is transmitted in plain text. HTTPS is a secure SSL encryption transmission protocol.
  • HTTP and HTTPS use completely different connections and use different ports, the former 80 and the latter 443.
  • HTTP connections are simple and stateless; HTTPS is a network protocol that uses SSL and HTTP to encrypt transmission and authenticate identity. It is more secure than HTTP.

At the end

I’m just going to talk about the basic concepts today, so let’s get the basic concepts straight, and tomorrow we’ll start to understand things at the code level

Daily for praise

All right, everybody, that’s all for this article. All the people here are talented.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!