IOS Socket Theory Knowledge Preamble
The network seven layer is from the bottom to the physical layer, the data link layer, the network layer, the Transport layer, the session layer, the presentation layer and the application layer. The physical layer, the data link layer and the network layer are often referred to as media claims, and are the objects that network engineers study; The transport, session, presentation, and application layers are called Is what users are looking for and what they care about.
- HTTP protocol correspondence and Application layer
- TCP/UDP protocol corresponds to the transport layer
- IP protocol corresponds to the network layer
The three are not comparable in nature. Moreover, the HTTP protocol is based on TCP connections. TCP/IP is a Transport layer protocol that mainly addresses how data travels across the network, while HTTP is an application-layer protocol that addresses how data is packaged. When we transmit data, we can use only the transport layer (TCP/IP), but in that case, because there is no application layer, the data content is not recognized, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, there are HTTP, FTP, Telnet and so on, You can also define the application layer protocol yourself. The web uses HTTP as the Transport Layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol. Socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol.
What is a socket?
The socket, also known as "socket", is a system-provided method for network communication. Its essence is not an agreement, does not specify how the computer should deliver the message, but gives the programmer an interface to send messages, the programmer uses the method provided by this interface to send and receive messages.
The socket describes an IP, port pair. It simplifies the programmer's operation, knowing that the other's IP and port can send messages to each other, and the server side handles the messages sent. Therefore, the socket must contain both sides of the communication, that is, the client and the service side (server).
Tcp
TCP is connection-oriented, reliable transmission (guaranteed data correctness and guaranteed data order), transmission of large amounts of data (stream mode), slow speed, the need to establish a connection is more expensive (time, system resources).
TCP is a flow-mode protocol, is connection-oriented, that is, in the process of continuous connection, the data received in the socket is issued by the same host (hijacking what is not considered), therefore, know that the data is ordered to arrive on the line, as to how much data per read do not care.
TCP Three-time handshake
The so-called three-time handshake (three-way handshake) means that when a TCP connection is established, the client and server are required to send a total of 3 packets. The purpose of the three-time handshake is to connect the server to the specified port, establish a TCP connection, and synchronize the serial number and confirmation number of both parties and Exchange TCP window size information. In socket programming, when the client executes connect (), a three-time handshake is triggered (the image is from the network):
?
SYN (synchronous) is the synchronization flag; ACK (acknowledgement) is the confirmation flag, and SEQ is the serial number.
- First handshake: The client sends a TCP SYN flag to location 1 of the packet, indicating the port of the server to which the client intends to connect, and the initial sequence number x, which is stored in the header's Serial number field.
- Second handshake: The server sends back a confirmation packet (ACK) reply. That is, the SYN and ACK flag bits are both 1 and the confirmation sequence is set to the customer's serial number plus 1, which is x+1.
- Third handshake: The client sends a confirmation packet again (ACK) to the SYN flag bit for the 0,ACK flag bit of 1. and send the server an ACK to the ordinal field +1, put in the OK field sent to the other side. and write the serial number +1 in the data segment.
About three handshakes, I think it's a good idea to have a joke on it.
"Hey, hey, can you hear me?" 」
"No problem. I can hear it and return it. 」
"No problem. 」
TCP four times Wave
Four wave-waving process:
?
When the server receives the SYN connection request message from the client side, it can send the syn+ack message directly. Where the ACK message is used to answer, the SYN message is used for synchronization. However, when the connection is closed, when the server side receives the fin message, it is likely that the socket will not be closed immediately, so you can only reply to an ACK message, tell the client side, "You send fin message I received." I can't send fin messages until all the messages on my server end are sent, so I can't send them together. Therefore, four-step handshake is required.
Basic framework of TCP client-server programming
?
Udp
UDP is a transport layer protocol that is fast for non-connected, unreliable transmissions, and for transmitting small amounts of data (packet patterns). Note that UDP transmits a datagram packet, while TCP is a stream.
UDP is a non-connected protocol that, as long as the IP and port of the receiving end are known and the network is accessible, any host can send data to the receiving end. At this point, if you can read more than one message at a time, the data will be confused. For example, host A to send a message P1, Host B sent a message P2, if can read more than one message data, then will be P1 and P2 data merge together, such data is meaningless.
?
UDP client-server programming basic framework
The communication process of the socket
Each application, or service, has a port. For example, the port number for DNS 53,http 80 is the one that corresponds to an application or service. We can request from DNS to query information, because the DNS server is listening to 53 ports all the time, when we receive our query request, we can return the IP information we want. Therefore, in terms of programming, the following steps should be included:
- The service side uses the socket listening port;
- The client initiates the connection;
- The service side Returns information, establishes the connection, starts the communication;
- Client, the service side disconnects.
Socket principle
Socket (socket) is the cornerstone of communication and is the basic operating unit of network communication supporting TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process and contains five kinds of information that must be used for network communication: the protocol that the connection uses, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol port of the remote process.
When the application layer communicates data through the transport layer, TCP encounters a problem that provides concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may require data to be transmitted over the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide a socket (socket) interface for applications interacting with the TCP/IP protocol. The application layer can communicate with the transport layer through the socket interface, differentiate the communication from different application processes or network connections, and realize the concurrent service of data transmission.
Socket connection
Establishing a socket connection requires at least one pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket.
The connection process between sockets is divided into three steps:
-
Server monitoring: Server-side sockets do not locate specific client sockets, but are waiting for the status of the connection, real-time monitoring network status, waiting for the client's connection request
-
Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket
-
Connection confirmation: When a server-side socket hears or receives a connection request from a client socket, it responds to a client socket request, establishes a new thread, sends a description of the server-side socket to the client, and once the client confirms the description, the two sides formally establish the connection. While the server-side socket continues listening, it continues to receive connection requests from other client sockets
IOS Socket Theory knowledge