1. TCP protocol
Reliable transmission, TCP packets do not have a length limit, theoretically can be infinitely long, but in order to ensure the efficiency of the network, usually the length of TCP packets will not exceed the length of IP packets, to ensure that a single TCP packet no longer split.
(1) Three-time handshake build link
(2) Four-time wave break link
(3) Status of the TCP protocol
(4) SYN flood attack and semi-connection pool
123456, the server side receives the ACK from the client and becomes established. At this point 3 handshake is completed, the connection is established!
Wave three times
123456, after a 2MSL long time to enter the closed state. The server receives an ACK from the client and enters the closed state. At this point, there is another state that does not come out: closing state. Closing status indicates that the client sent fin, but did not receive the server's ACK, but received the server's fin, which occurs when the server sends the ACK drops, because the network transmission is sometimes unexpected.
Wave four times
LISTEN: Waits for a connection request from any remote TCP and port. Syn_sent: Waits for a matching connection request after sending a connection request. Syn_received: The connection request is sent and a matching connection request is received waiting for the connection request to be confirmed. Established: Represents an open connection in which the received data can be posted to the user. The normal state of the data transfer phase of the connection. Fin_wait_1: Waits for a connection termination request from the remote TCP, or waits for confirmation of a connection termination request sent before. Fin_wait_2: Waits for a connection termination request from the remote TCP. Close_wait: Waits for the local user's connection termination request. CLOSING: Wait for the remote TCP connection to terminate the request acknowledgement. Last_ack: Waits for confirmation of a connection termination request that was previously sent to the remote TCP (including confirmation of its byte connection termination request) Time_wait: Wait for enough time to pass to ensure that the remote TCP receives its acknowledgement of the connection termination request. Time_wait two reasons for existence:1. Reliable termination of the TCP full-duplex connection; 2. Allow old repeating sections to fade in the network. CLOSED: Not connected (this is for the convenience of describing the hypothetical state, actually does not exist)
three handshake, four waves of 11 states
fromSocketImportsocket, af_inet, SOCK_STREAMIP='127.0.0.1'PORT= 8888ADDRESS=(IP, PORT) BUFSIZE= 1024#1. Create a server-side object (Af_inet:ipv4,sock_stream:sock stream)Ser_socket =socket (af_inet, sock_stream)#2. IP address of the binding server, port ()Ser_socket.bind (ADDRESS)#3. Set the semi-connection pool to limit the number of requestsSer_socket.listen (5)#4. Waiting for client requestsCli_socket, CLI_ADRR =ser_socket.accept ()Print(Cli_socket)Print(CLI_ADRR)#5. Send and receive Datadata =cli_socket.recv (BUFSIZE)Print(Data.decode ('Utf-8')) Cli_socket.send ('the server has received data'. Encode ('Utf-8'))#can only be sent as a byte type#6. Turn off the connection to the clientcli_socket.close ()#7. Shutting down the server (typically does not shut down the server)#ser_socket.close ()TCP Service SideTCP Service Side
fromSocketImportsocket, af_inet, SOCK_STREAMIP='127.0.0.1'PORT= 8888ADDRESS=(IP, PORT) BUFSIZE= 1024#1. Create a client socket objectCli_socket =socket (af_inet, sock_stream)#2. Connect to the serverCli_socket.connect (ADDRESS)#3. Data collectionCli_socket.send ('Hello'. Encode ('Utf-8'))#can only be sent as a byte typedata =cli_socket.recv (BUFSIZE)Print(Data.decode ('Utf-8'))#Close Client Connectionscli_socket.close () TCP clientTCP Client
2, socket socket
(1) socket Layer
socket is an intermediate software abstraction layer for the application layer and TCP/IP Protocol family Communication, It is a set of interfaces. In design mode, socket is actually a façade mode, which hides the complex TCP/IP protocol family behind socket interface, For the user, a simple set of interfaces is all, let socket to organize the data to conform to the specified protocol. The
socket is an interface that encapsulates the TCP/IP protocol.
(2) What is socket
(3) How to use Socke
Python TCP protocol, socket sockets