The difference between HTTP, TCP/IP protocol and socket

Source: Internet
Author: User
Tags ack network function

The difference between HTTP, TCP/IP protocol and socket

  Network is divided from bottom to top:  www.2cto.com   Physical Layer-                    &NBSP Data Link layer--network layer--                      IP Protocol Transport Layer--                      TCP protocol Session Layer--presentation tier and application Layer--          HTTP protocol &nbsp ; 1, TCP/IP connection    mobile phone can use the network function is because the mobile phone bottom of the implementation of the TCP/IP protocol, 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.   Establishing a TCP connection requires a "three handshake":  the first handshake: the client sends the 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, You must confirm the customer's SYN (ACK=J+1), and also send a SYN packet (SYN=K), which is the Syn+ack package, when the server enters the SYN_RECV state;  the third handshake: the client receives the server's Syn+ack package, Send acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and server enter the established state, complete three handshake.   The packets delivered during the handshake do not contain data, and after three handshakes, the client and server formally begin transmitting the data. 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).   2, HTTP connection   The HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the basis for Web networking andOne of the most commonly used protocols for mobile phone networking, the HTTP protocol is an application built on the TCP protocol. The most notable feature of the  http connection is that each request sent by the client requires a server loopback response, and after the request is completed, the connection is actively released. 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 that a separate connection 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. &NBSP;&NBSP;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 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 simultaneously provides concurrent services for multiple application processes. 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 request, connectionRecognize.   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 connection request.   Client request: Refers to the client's socket to make a connection request, the target of the connection 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 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. &NBSP;&NBSP;4, socket connection and TCP/IP connection   When creating a socket connection, you can specify the transport layer protocol used, the socket can support different transport layer protocols (TCP or UDP), and when connecting using the TCP protocol, The socket connection is a TCP connection.   socket is the encapsulation and application of the TCP/IP protocol (at the programmer level). It can also be said that the TPC/IP protocol is the Transport Layer protocol, mainly to solve how the data in the network transmission, and HTTP is the application layer protocol, mainly to solve how to package the data. About the relationship between TCP/IP and HTTP protocol, the network has a relatively easy to understand the introduction of:  "We can only use (Transport Layer) TCP/IP protocol when transmitting data, but in that case, if there is no application layer, the data content cannot be recognized, if you want to make the transmitted data meaningful, You must use the Application layer protocol, there are many application layer protocols, such as HTTP, FTP, Telnet, etc., you can also define the application layer protocol. The web uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol. "  What do we usually say the most socket is, in fact, the 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. In fact, the socket is not necessarily associated with the TCP/IP protocol. The socket programming interface is designed to adapt to other network protocols as well. So, the advent of sockets just makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as create, listen, connect, accept, send, read, write, and so on. The network has a paragraph about the socket and TCP/IP protocol relationship is easier to understand:  "TCP/IP is just a protocol stack, just like operating system operation mechanism, must be implemented specifically, but also to provide external operating interface. This is like the operating system will provide a standard programming interface, such as the Win32 programming interface, TCP/IP should also provide the interface for programmers to do network development, this is the socket programming interface. "  In fact, the transport layer TCP is based on the network layer of IP protocol, and the application layer of the HTTP protocol is based on the transport layer of the TCP protocol, and the socket itself is not a protocol, as mentioned above, it just provides a TCP or UDP programming interface. Socket is the development of port communication tools, it is more lower.  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 the 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.    while an HTTP connection uses a "request-response" approach, the server can not only reply to the data until the request is made, but the client needs to make a request to the server.   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 synchronization. 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 the server if there is any new data, and if so, it passes the data to the client.  http protocol is the application layer of the semantic    has a comparative image of the description: HTTP is a sedan, providing a specific form of encapsulation or display data; the socket is the engine that provides the ability to communicate on the network. The communication between    two computers is nothing more than a data communication between two ports, and what form will the specific information represent ' as HTTP ' FTP ' ...  < as defined in a different application layer protocol!--[iF!supportlinebreaknewline]--><!--[endif]-->

The difference between HTTP, TCP/IP protocol and socket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.