Home  >  Article  >  php教程  >  The difference between TCP/IP, HTTP and Socket

The difference between TCP/IP, HTTP and Socket

高洛峰
高洛峰Original
2016-12-13 09:54:591809browse

 The network is divided from bottom to top into

 physical layer, data link layer, network layer, transport layer, session layer, presentation layer and application layer.

 Through preliminary understanding, I know that the IP protocol corresponds to the network layer, the TCP protocol corresponds to the transport layer, and the HTTP protocol corresponds to the application layer.

 There is no comparability between the three in essence.

 Socket is a comparison of TCP /IP protocol encapsulation and application (programmer level).

 It can also be said that TPC/IP protocol is a transport layer protocol, which mainly solves how to transmit data in the network,

 And HTTP is an application layer protocol, which mainly solves how to package data.

  Regarding the relationship between TCP/IP and HTTP protocols, the network has a relatively easy-to-understand introduction:

  "When we transmit data, we can only use the (transport layer) TCP/IP protocol, but in that case, if there is no application layer , the data content cannot be identified.

  If you want to make the transmitted data meaningful, you must use an application layer protocol.

  There are many application layer protocols, such as HTTP, FTP, TELNET, etc. You can also define your own application layer protocols. .

 WEB uses HTTP protocol as the application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it to the network. ”

 What is the socket we usually talk about most? The upper socket is an encapsulation of the TCP/IP protocol. The socket itself is not a protocol, but a calling interface (API).

 Through Socket, we can use the TCP/IP protocol.

In fact, Socket is not necessarily related to the TCP/IP protocol.

When the Socket programming interface was designed, it was hoped that it could also adapt to other network protocols.

 So, the emergence of Socket only makes it easier for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol,

 This forms some of the most basic function interfaces we know, such as create, listen, connect, accept, send, read, write, etc.

 There is a statement on the Internet about the relationship between socket and TCP/IP protocol that is easier to understand:

 “TCP/IP is just a protocol stack, just like the operating mechanism of the operating system, it must be implemented concretely, and at the same time, it must provide external Operation interface.

 Just like the operating system provides a standard programming interface, such as the win32 programming interface, TCP/IP also provides an interface that programmers can use for network development. This is the Socket programming interface.

  Regarding the TCP/IP protocol, I can’t overestimate it in terms of its breadth and depth. Just check the amount of information and books on this type of information on the Internet.

I plan to buy some classic books on this. (For example, "TCP/IP Detailed Explanation: Volume 1, Volume 2, Volume 3") to study, today I will summarize some knowledge based on applications and programming interfaces based on the TCP/IP protocol, that is, the HTTP and Socket that I just talked about a lot .

There is a more vivid description on CSDN: HTTP is a car, which provides a specific form of encapsulating or displaying data; Socket is an engine, which provides the ability of network communication.

 In fact, the TCP of the transport layer is based on the IP protocol of the network layer, and the HTTP protocol of the application layer is based on the TCP protocol of the transport layer. Socket itself is not a protocol. As mentioned above, it only provides An interface for TCP or UDP programming.

 The following are some important concepts that are often encountered in written examinations or interviews. They are excerpted and summarized here.

  1. What is the three-way handshake of TCP connection?

The 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;

The second handshake: the server receives syn packet, you must confirm the client's SYN (ack=j+1), and at the same time send a SYN packet (syn=k), that is, SYN+ACK packet. At this time, the server enters the SYN_RECV state;

 The third handshake: client The client receives the SYN+ACK packet from the server and sends an acknowledgment packet ACK (ack=k+1) to the server. After the packet is sent, the client and the server enter the ESTABLISHED state and complete the three-way handshake.

 The packet transmitted during the handshake process does not contain data. After the three-way handshake is completed, the client and server officially start transmitting data.

  Ideally, once a TCP connection is established, the TCP connection will be maintained until either party actively closes the connection.

When disconnecting, both the server and the client can actively initiate a request to disconnect the TCP connection. The disconnection process requires a "four-way handshake" (the process will not be detailed, it is the interaction between the server and the client, and the disconnection is finally determined. )

 2. Steps to establish a network connection using Socket

 Establishing a Socket connection requires at least a pair of sockets, one of which runs on the client, called ClientSocket, and the other runs on the server, called ServerSocket.

 The connection process between sockets is divided into three steps: server monitoring, client request, and connection confirmation.

 1. Server monitoring: The server-side socket does not locate the specific client socket, but is in a state of waiting for connection, monitoring the network status in real time, and waiting for the client's connection request.

 2. Client request: refers to the client’s socket making a connection request, and the target to be connected is the server’s socket.

 To do this, the client's socket must first describe the socket of the server it wants to connect to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

  3. Connection confirmation: When the server-side socket listens or receives a connection request from the client socket, it responds to the client socket request, establishes a new thread, and connects the server-side socket. A description of the word is sent to the client. Once the client confirms this description, the two parties formally establish a connection.

 The server-side socket continues to be in the listening state and continues to receive connection requests from other client sockets.

3. Characteristics of HTTP links

HTTP protocol is the Hypertext Transfer Protocol, which is the basis of Web networking and one of the commonly used protocols for mobile phone networking. The HTTP protocol is based on the TCP protocol. application.

 The most notable feature of HTTP connections is that each request sent by the client requires the server to send back a response. After the request is completed, the connection will be actively released. The process from establishing a connection to closing the connection is called "a connection".

  4. The difference between TCP and UDP (most tested...I think I am about to fail the exam - -\)

  1. TCP is link-oriented, although the insecure and unstable characteristics of the network determine the number of handshakes Neither can guarantee the reliability of the connection, but TCP's three-way handshake guarantees the reliability of the connection to a minimum (in fact, to a large extent);

And UDP is not connection-oriented, and UDP does not transmit data before it is transmitted. Establishing a connection with the other party does not send a confirmation signal for the received data. The sending end does not know whether the data will be received correctly, and of course there is no need to resend it. Therefore, UDP is a connectionless and unreliable data transmission protocol.

  2. It is precisely because of the characteristics mentioned in 1 that UDP has smaller overhead and higher data transmission rate. Because there is no need to confirm the sending and receiving data, UDP has better real-time performance.

 Knowing the difference between TCP and UDP, it is not difficult to understand why MSN using TCP transmission protocol is slower than QQ using UDP to transfer files, but it does not mean that QQ communication is unsafe,

 Because programmers can manually Verify UDP data transmission and reception, such as the sender numbering each data packet and then verifying it by the receiver, etc.

Even so, UDP does not use a "three-way handshake" similar to TCP in the encapsulation of the underlying protocol. "And achieves transmission efficiency that TCP cannot achieve.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn