The 22nd day socket introduction Internet communication protocol tcp protocol

Sockets
The purpose of learning socket programming is to develop a c/s or b/s architecture software

Client------Network-------server
cs architecture needs to write two softwares, both the client and the server need to be developed to write.
Advantages: development can write software according to their own ideas
Disadvantages: server, client The client needs to be developed to write, and the workload of development increases.
Browser------Network------
The client of the server bs architecture is a browser, and the development only needs to write the software on the server.
Advantages: The workload of development is reduced. , You only need to write server-side software.
Disadvantage: To write software, you need to write in accordance with the browser’s standards

Internet = physical connection medium + communication protocol The
Internet communication protocol is equivalent to a standard for Internet communication

The socket encapsulates the transport layer, network layer, data link layer, and physical layer. Application layer data needs to be sent to the outside,
you can directly use the socket interface to call the function of the socket

osi seven-layer protocol
application layer-presentation layer-session layer-transport layer-network layer-data link layer-physical layer
Insert picture description here
Physical layer : responsible for sending electrical signals (1, 0 to indicate high and low electrical frequencies)
Data link layer : etherent protocol It is stipulated that a group of electrical signals constitute a data frame, called a "frame".
Each data frame is divided into two parts: header head and data data.
Head contains: sender (source address), receiver (destination address), data type
data: data The specific content of the package
mac address: Etherent stipulates that all devices connected to the Internet must have a network card. The
addresses of the sender and receiver refer to the address of the network card, that is, the mac address. The mac address
can only be used in the local area network. The
network layer : ip address plus On the subnet mask, you can find which local area network the computer is in, which is called the packet
transmission layer : tcp/udp protocol, port
application layer : http, ftp, mail protocol

Sending data
Assuming that the client needs to send a piece of data to the server, the application layer sends the data (with or without encapsulation) and
transmits it to the transport layer. The transport layer encapsulates the source port and destination port on the source data; passes it to the network layer and the network The layer
will encapsulate the source ip and target ip; pass it to the data link layer, the data link layer will encapsulate the source mac address, the destination mac address, and
pass it to the physical layer, the physical layer will convert the data into binary and send it to the transit device, the transit device Will be sent to the server.

Receiving data After the
server receives the data, the physical layer first converts the binary into a data frame, and then disassembles it in turn. The data link layer confirms the mac address, the
network layer confirms the ip address, the transport layer confirms the port number, and the application layer receives the client data .

arp protocol: ip address resolves to mac address
arp protocol function: send data packets by broadcast to obtain the mac address of the target host

ip+mac+port=" identifies the unique computer software port in the world

Socket schematic diagram :
Insert picture description here

The socket encapsulates the transport layer, network layer, data link layer, and physical layer. Application layer data needs to be sent to the outside,
you can directly use the socket interface to call the function of the socket

The two protocols of the transport layer
tcp protocol (slow data
transmission ) tcp protocol is reliable transmission: the data generated by the program is stored in the memory first, and the data is not deleted immediately after the data is sent to the client at the sender end, and will wait for the client to return a confirmation message After that,
delete the data. If the sender does not receive the ack confirmation message returned by the client, it will be sent again after a period of time.

Insert picture description here
The status of the tcp protocol:
SYN_SENT: the client has just sent the data request
LISTEN: the server is waiting for the client's request to connect
SYN_RCVD: the SYN request is received
ESTABLISHED: the connection is established successfully (note that the establishment of the client is successful, not necessarily the establishment of the connection, the service The establisged display on the end means that the connection is established successfully)

syn=1 (flag bit) represents a request to establish a connection. It
takes three interactions to establish a connection. Assuming that the client first sends a connection request to the server, the server returns and sends a connection request to the client at the same time after receiving the request, and the
client receives the service After the request from the client, a confirmation connection request is sent to the server.
When a connection is established, the client will send a serial number (seq=a string of numbers)
to the server and the client will return (ack=1+a string of numbers) to determine whether the connection between the server and the client is the other party they are looking for

fin represents a request to disconnect.
Disconnecting requires four interactions. Assuming that the data that the client needs to send to the server has been transmitted, it will send a disconnect request to the
server. After the server receives the request, it will send The client confirms the disconnect request. When the data transmission of the server is completed, it will also send a disconnect
request to the client. After receiving the request from the server, the client sends a disconnect confirmation request to the server.

ack=1 means to confirm the connection/disconnection. During the transmission, it means to confirm whether the serial number is correct

Flood attack
backloa (semi-connection pool): The request sent by the client to the server will be placed in the semi-connection pool first. If
the speed of processing the request cannot keep up with the speed of the access request, it will cause the semi-connection pool to fill up space, commonly known as flood Attack The
semi-connection pool occupies memory space. Once the semi-connection pool is full, it will fail when accessed by other users.

udp protocol (fast data transmission)
udp protocol is unreliable data: data transmission does not need to establish a connection, and does not need to wait for ack. Delete the data after sending the data.

Guess you like

Origin blog.csdn.net/Yosigo_/article/details/112627233