The relationship between tcp/ip, http, socket

The network is divided into

  physical .

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

  three are essentially incomparable.

  Socket is the encapsulation and Application (programmer level).

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

  while 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, it cannot be recognized Data content.

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

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

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

  And what is the socket we usually talk about the most? In fact, socket is the encapsulation of the TCP/IP protocol. Socket It is not a protocol itself, but a calling interface (API).

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

  In fact, Socket has no necessary connection with the TCP/IP protocol.

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

  Therefore, the appearance of Socket just makes it easier for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol,

  thus forming some of the most basic functional interfaces we know, such as create, listen, connect, accept, send, read and write, etc.

  The network has a statement 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 an operating system, it must be implemented specifically, and an external operation interface must also be provided.

  This Just as the operating system provides standard programming interfaces, such as the win32 programming interface,

  TCP/IP also provides an interface that programmers can use for network development, which is the Socket programming interface."

  The only thing related to the TCP/IP protocol is, I don't think it's too much to talk about it in terms of breadth and depth. Just check the number of online materials and books about this kind of thing.

  I plan to buy some classic books (such as "TCP/IP Detailed Explanation: Volume 1, Volume 1"). Two, Volume Three") to learn, today I will summarize some knowledge of applications and programming interfaces based on the TCP/IP protocol, that is, HTTP and Socket I just said a lot.

  There is a more vivid description on CSDN: HTTP is a car, which provides a specific form of encapsulation or display 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, and the Socket itself is not a protocol. As mentioned above, it only provides a Interface for TCP or UDP programming.

  The following are some important concepts that are often encountered in written tests or interviews, and are hereby excerpted and summarized.

  1. What is the three-way handshake of a 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 the syn packet and must confirm the client's SYN (ack=j+1) At the same time, it also sends a SYN packet (syn=k), that is, a SYN+ACK packet. At this time, the server enters the SYN_RECV state;

  the third handshake: the client receives the SYN+ACK packet from the server and sends an acknowledgement packet ACK to the server ( ack=k+1), after the packet is sent, the client and 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 the server officially begin to transmit data.

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

  When the connection is disconnected, both the server and the client can actively initiate a request to disconnect the TCP connection. The disconnection process needs to go through a "four-way handshake" (the process is not detailed, that is, the server and the client interact, and finally determine the disconnection)

  2 Steps

  to establish At least one pair of sockets is required to establish a Socket connection, one of which runs on the client side, called ClientSocket, and the other runs on the server side, 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 a specific client socket, but is in a state of waiting for a connection, monitors the network status in real time, and waits for a client's connection request.

  2. Client request: refers to the connection request made by the client's socket, and the target to be connected is the socket of the server.

  To do this, the client's socket must first describe the server's socket to which it wants to connect, point out 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-side socket, it responds to the client-side socket's request, establishes a new thread, and converts the server-side socket's connection request. The description is sent to the client, and once the client confirms the 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-side sockets.

  3. Characteristics of HTTP Links

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

  The most notable feature of HTTP connections is that each request sent by the client requires the server to send back a response, and the connection will be actively released after the request ends. The process from establishing a connection to closing it is called a "one-time connection".

  Fourth, the difference between TCP and UDP (the most tested.. I think it will be rotten by the test - -\\)

  1. TCP is link-oriented, although the insecure and unstable nature of the network determines how many handshakes cannot be guaranteed. The reliability of the connection, but the three-way handshake of TCP guarantees the reliability of the connection at a minimum (in fact, to a large extent);

  while UDP is not connection-oriented, and UDP does not establish a connection with the other party before transmitting data. It does not send an acknowledgment signal to the received data. The sender does not know whether the data will be received correctly, and of course does not need to retransmit it. Therefore, UDP is a connectionless and unreliable data transmission protocol.

  2. Because of the characteristics mentioned in 1, the overhead of UDP is smaller and the data transmission rate is higher. Because there is no need to confirm the data sent and received, the real-time performance of UDP is better.

  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 transmit files, but it cannot be said that QQ communication is insecure,

  because programmers can manually UDP data For example, the sender numbers each data packet and then the receiver verifies it.

  Even so, UDP does not use the "three-way handshake" similar to TCP in the encapsulation of the underlying protocol. unattainable transmission efficiency.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711281&siteId=291194637