Eleven, network programming

The goal of network programming

Write a C/S or B/S architecture based network communication software

C/S Architecture: Client---Server
B/S Architecture: Browser---Server

Learning socket programming is to write client software and server software, and then implement network-based communication between server and client

Features of the server: 1. Provide services uninterrupted; 2. The server must support concurrency + high performance

 

2. Network

network = physical connection medium + internet protocol

 

The Internet Protocol is divided into seven layers of OSI, or five layers of TCP/IP:

Physical layer: sending electrical signals - based on binary

Data link layer: ethernet Ethernet protocol-----based on mac address, communication within the local area network

Network layer: IP protocol-----based on IP address

Transport layer: TCP/UDP protocol ------ based on port (port is the number associated with the application and the network card)

Application layer: HTTP, mail, ftp protocols, etc.

 

The role of the IP protocol: one is to assign an IP address to each computer, and the other is to determine which addresses are in the same subnet

ARP protocol: In the same local area network, the mac address is obtained according to the IP address, and the operating system automatically converts

Routing Protocols: Gateway-to-Gateway Communication

TCP protocol: TCP establishes a two-way connection, also known as a streaming protocol

                  3-way handshake to establish a two-way path between client and server

                  Wave 4 times to disconnect, the server initiates disconnection first

 

The mac address + IP address + port can locate the unique application software in the world; with the ARP protocol, the IP address + port can locate the unique application software in the world .

 

Three, socket programming

Sockct (socket) is an abstraction layer located between the application layer and the transport layer, which specifically encapsulates the protocol below the transport layer into an interface for the application layer to use.

There are two (or two races) sockets, file-based and network-based.

Socket family name: AF_UNIX
Socket family name: AF_INET

 

1. Socket workflow

服务端:
socket()---bind()---listen()---accept()---recv()---send()---close()

 The server first initializes the socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect.

 

Server:

socket()---connect()---send()---recv()---close()

The client initializes a socket, and then connects to the server (connect). If the connection is successful, the connection between the client and the server is established.

 

The client sends a data request (send), the server receives the request and processes the request (recv), then sends the response data to the client (send), the client reads the data (recv), and finally closes the connection (close), an interaction end.

 

bind() binds an IP address and port. Local loopback address: 127.0.0.1, port: 0 to 65535, of which 0 to 1024 are the
listen() for the system The numbers in the brackets represent how many requests can only come in at the same time
accept() Waiting for information
reav(1024) represents receipt message, 1024 is a maximum limit
send() returns a message
close() closes the connection

 

2, sticky package

Only TCP has sticky packet phenomenon, UDP will never stick packets, because TCP protocol is a stream-oriented protocol, and UDP is a message-oriented protocol.
The so-called sticky packet problem is mainly caused by the fact that the receiver does not know the boundaries between messages and does not know how many bytes of data to extract at one time .

 

Sticky packets will occur in two cases:
1. The sender needs to wait for the buffer to be full before sending out, resulting in sticky packets (the time interval for sending data is very short, the amount of data is small, and they are combined together to generate sticky packets)
2. Receiver The packets in the buffer are not received in time, resulting in multiple packet reception (the client sends a piece of data, the server only receives a small part, and the server will take the data left from the last time from the buffer when it receives it next time, resulting in sticky package)

 

TCP is based on data streams, so the messages sent and received cannot be empty, which requires adding a processing mechanism for empty messages on both the client and the server.

 

The solution for sticky packets:
add a custom fixed-length header to the byte stream, the header contains the length of the byte stream, and then send it to the peer at one time. When the peer receives it, it first takes out the fixed-length header from the cache, and then Get real data

 

Use the struct module to solve sticky packages

The role of the struct module:
1. Convert integer numbers to bytes type
2. The converted bytes are of fixed length

 

The header can be made into a dictionary, the dictionary contains the details of the real data to be sent, then json serialized, and then the length of the serialized data is packaged into 4 bytes with struck (4 is enough for itself)

When sending:
first send the header length
, then encode the header content, and then send
the final real content

When receiving:
first check the header length, use struct
to retrieve the header content according to the retrieved length, then decode and deserialize
to retrieve the details of the data to be retrieved from the deserialized result, and then retrieve the real data content

 

3. Socket based on UDP protocol communication
UDP is based on datagrams, one sending corresponds to one receiving
Features: no connection
Advantages: high sending efficiency, but the maximum amount of data transmitted effectively is 512bytes
Disadvantages: unreliable (no need for the other party to send data Confirm, easy to lose packets)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325267304&siteId=291194637