Socket is a set of interfaces built on the TCP/IP protocol is not a protocol
Application layer: HTTP FTP SMTP Web
Transport layer: Provides logical, not physical, communication between two applications (TCP UDP)
TCP reliable connection-oriented services
UDP unreliable, non-connected service
As long as the underlying implementation of the TCP IP protocol can be used to communicate with the socket
1. TCP and UDP
1.1 TCP Connections
The TCP protocol can provide a reliable communication connection for the application, so that the byte stream emitted by one computer is sent to other computers on the network without error, and the data communication system with high reliability requirements often uses the TCP protocol.
Mobile phone can use the network function is because the bottom of the mobile phone implementation of TCP/IP protocol, you can make the mobile phone terminal through the wireless network to establish a TCP connection. TCP protocol can provide an interface to the upper network, so that the transmission of the upper network data is based on the "No Difference" network.
Setting up a TCP connection requires a "three-time handshake":
First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;
(That is, make a connection request packet: "I want to send you data, OK?" ”)
Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;
(That is, send consent to connect and require synchronization (synchronization is a two host one in the send, one in the receiving, coordination work) packet: "Can, when do you send?" ”)
Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.
(That is, issue a packet acknowledgment that requires synchronization: "I'll send it now, you go on! ”)
The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection. When disconnected, both the server and the client can initiate a request to disconnect the TCP connection, and the disconnection process requires a "four handshake" (the process is not fine-grained, that is, the server interacts with the client and finally determines the disconnection)
TCP is responsible for data recovery when packets are received unordered, lost, or destroyed during delivery. It accomplishes this by providing a sequence number for each packet it sends. Remember that the lower network layer treats each packet as a separate unit, so the packets can be sent along a completely different path, even if they are part of the same message. This kind of routing is very similar to the way the network layer handles fragmentation and reassembly of packets, just a higher level.
To ensure that data is received correctly, TCP requires an acknowledgment (that is, an ACK) to be sent back when the destination computer successfully receives the data. If the corresponding ACK is not received within a certain time limit, the packet is re-routed. If the network is congested, this re-transfer will result in duplicate packets being sent. However, the receiving computer can use the sequence number of the packet to determine whether it is a duplicate packet and discard it if necessary.
1.2 UDP
UDP (User data Protocol, Subscriber Datagram Protocol) is the protocol that corresponds to TCP. It is a non-connected protocol that does not establish a connection with the other, but sends the packet over directly!
"Non-connected" is the need to establish a connection with the other party before the formal communication, regardless of the status of the other party directly sent. This is very similar to the current popular mobile phone messages: When you send text messages, you only need to enter the other mobile phone number is OK.
UDP is suitable for applications where only a small amount of data is transmitted at a time, and the reliability requirements are low . For example, we often use the "ping" command to test the TCP/IP communication between the two hosts is normal, in fact, the principle of "ping" command is to send UDP packets to the other host, and then the other host to confirm the receipt of the packet, if the packet arrives timely feedback back, then the network is through. For example, in the default state, a "ping" operation sends 4 packets (as shown in 2). As you can see, the number of packets sent is 4 packets, and the received is also 4 packets (because the other host receives a confirmation of the received packet back). This fully illustrates that the UDP protocol is for a non-connected protocol, and there is no process for establishing a connection. Because the UDP protocol does not have a connection process, it has high communication performance, but because of this, its reliability is not as high as the TCP protocol. QQ uses UDP to send messages, so there are times when messages are not received.
UDP does not contain congestion control mechanisms, so the sending process can send data to UDP sockets at any rate, although there is no guarantee that all data will reach the receiving socket, but there will be a considerable amount of data arriving from the column. Developers of real-time applications often choose to run their apps on UDP. Like TCP, UDP does not provide any delay guarantees.
1.3 TCP and UDP differences
|
Tcp |
Udp |
Whether to connect |
Connection oriented |
For non-connected |
Transmission Reliability |
Reliable, |
Not reliable. |
Application situations |
Transfer large amounts of data |
Small amount of data |
Speed |
Slow |
Fast |
TCP protocol and UDP feature differences Summary: 1.TCP protocol oriented connection, UDP protocol for non-connected (with or without link) 2.TCP protocol transmission speed is slow, UDP protocol transmission speed (transmission speed) 3.TCP protocol ensures data shun The UDP protocol does not guarantee (the order of the data, the packet becomes unordered at the IP layer) 4. The TCP protocol guarantees the correctness of the data, the UDP protocol may drop packets (TCP guarantees the reliability of the data) the 5.TCP protocol has more requirements for system resources and less UDP protocol (TCP and UDP resources)
1.4 Selection of TCP and UDP
If we compare the structure of UDP packets and TCP packets, it is obvious that UDP packets do not have the complex reliability and control mechanism of TCP packets. As with the TCP protocol, the number of source ports and destination ports for UDP also supports multiple applications on a single host. A 16-bit UDP packet contains a byte-long header and the length of the data, and the checksum field makes it possible to verify the whole. (Many applications only support UDP, such as multimedia data streams, without generating any additional data, even if they know that a damaged package is not re-sent.) )
Obviously, the TCP protocol is an option when the performance of the data transfer must be in the integrity, control, and reliability of the data transfer. UDP is the best choice when it comes to the emphasis on transmission performance rather than the integrity of the transmission, such as: Audio and multimedia applications. UDP is also a good choice in cases where the data transfer time is so short that the previous connection process becomes the entire flow body, such as: DNS Exchange. Part of the reason that SNMP is built on UDP is that the designer thinks that when network congestion occurs, the lower cost of UDP gives it a better chance to transfer management data. TCP-rich functionality can sometimes lead to unpredictable performance, but we believe that TCP's reliable point-to-point connections will be used for the vast majority of network applications in the near future.
Application |
Application Layer Protocol |
The transport protocol used to support |
Email |
SMTP (RFC82) |
Tcp |
Remote terminal access |
Telenet |
Tcp |
Web |
HTTP |
Tcp |
File transfer |
Ftp |
Tcp |
Remote file Server |
Nfs |
TCP or UDP |
Streaming multimedia |
Exclusive |
UDP or TCP |
Internet phone |
Exclusive |
UDP is generally |
In the table we see that e-mail, remote terminal access, Web and file transfer all use the TCP protocol, the main reason for these applications to choose TCP is that TCP provides reliable data transfer services that ensure that all data eventually reaches its destination. We also see that internet telephony generally runs on top of the UDP protocol, where both terminals of an Internet Telephony application send data across the network at a minimum rate, and Internet telephony can tolerate data loss, both of which are suitable for UDP and do not require reliable TCP transport services.
In general, TCP guarantees that all data is passed, and UDP is not, and they do not provide a delay guarantee. They do not provide a delay guarantee, and it does not mean that time-sensitive applications cannot run on the Internet today, but that the application is using some other means to ensure these needs.
2. HTTP connection
The HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the most commonly used protocols for mobile networking, an application built on the TCP protocol.
The most notable feature of an HTTP connection is that each request sent by the client requires a server loopback response, and the connection is actively released after the request has ended. The process from establishing a connection to closing a connection is called a "one-time connection."
1) in HTTP 1.0, each request from the client requires a separate connection to be established, and the connection is automatically freed after the request is processed.
2) in HTTP 1.1, multiple requests can be processed in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next request.
Because HTTP is actively releasing the connection after each request ends, the HTTP connection is a "short connection", which requires constant connection requests to the server to maintain the client program's online status. As a general practice, there is no need to obtain any data immediately, and the client will keep a "keep-connected" request to the server at regular intervals, and the server responds to the client after receiving the request, indicating that the client is "online". If the server can not receive the client's request for a long time, it is considered that the client "offline", if the client cannot receive a reply from the server for a long time, it is considered that the network has been disconnected.
3. Socket principle
3.1 Socket (socket) concept
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 made for network communication:
The protocol used by the connection,
The IP address of the local host,
Protocol port for the local process,
IP address of the remote host,
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.
3.2 Establishing a 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,
Client requests,
Connection acknowledgement.
Server Listener: Server-side sockets do not locate specific client sockets, but are in a state of waiting for connections, monitoring network status in real time, waiting for client connection requests.
Client Request: Refers to the client's socket to make a connection request, and the target to connect to 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 to 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 parties formally establish the connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.
4, Socket connection and TCP connection
When you create a socket connection, you can specify the transport layer protocol that is used, and the socket can support different transport layer protocols (TCP or UDP). When a connection is made using the TCP protocol, the socket connection is a TCP connection.
5, Socket connection and HTTP connection
Since the socket connection is usually a TCP connection, once the socket connection is established, the communication parties can begin to send data content to each other. Until both sides of the connection are disconnected. However, in real network applications, the client-to-server communication often needs to traverse multiple intermediary nodes, such as routers, gateways, firewalls, and so on, most firewalls will turn off long inactive connections and cause the Socket connection to be disconnected, so it needs to be polled to tell the network that the connection is active.
The HTTP connection uses a "request-response" approach that requires not only the connection to be established at the time of the request, but also the server's ability to reply to the server when a request is made from the client.
In many cases, the server side is required to proactively push data to the client, keeping the client and server data in real time and in sync. At this point, if the two sides established a socket connection, the server can directly transfer the data to the client, if the two sides establish an HTTP connection, the server needs to wait until the client sends a request before the data can be sent back to the client, so the client periodically sends a connection request to the server, not only to remain online, It also asks if the server has new data, and if so, passes the data to the client
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. 8875748
The principle of the "ping" command is to send a UDP packet to the other host, and HTTP will actively release the connection after each request ends, so the HTTP connection is a "short connection"