The difference between HTTP, TCP/IP protocol and socket

Source: Internet
Author: User
Tags ack network function

The network is divided from bottom to top:

Physical Layer--

Data Link layer--

Network Layer--IP protocol

Transport Layer--TCP protocol

Session Layer--

Presentation layer and Application Layer-HTTP protocol

1. TCP/IP connection

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;

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;

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.


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, any one of the two communication
Before the party actively closes the connection, the TCP
The connection will be maintained forever. When disconnected, both the server and the client can proactively initiate a request to disconnect a TCP connection, which requires a "four-time handshake" (the process is not fine-grained, that is, the server and guest
End-of-the-door interaction and finally determine the disconnection).

model
Client-----Establish a connection-----server through the socket
Client-----Request-----> Server
Client <-----Response-----Server
Client-----Disconnecting-----Server
Next connection ....



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" that requires constant connection to the server to keep the client program online
Request. As a general practice, there is no need to obtain any data immediately, and the client also keeps sending a "keep-connected" request to the server at regular intervals, and the server returns to the client after the request is received.
The client is known to be "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 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 need to pass the same
The TCP protocol port transmits data. 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. Should
Layer can and transport layer through the socket interface, distinguish the communication from different application process or network connection, 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 between sockets is divided into three steps: Server listening, client request, connection acknowledgement.

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, 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 to a client socket, it responds to a client socket request, creates a new thread, and the server-side socket
Client, and once the client confirms the description, the two parties formally establish a 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/IP connection

When you create a socket connection, you can specify the transport layer protocol used, which can support different transport layer protocols (TCP or UDP), which is a TCP connection when a connection is made using the TCP protocol.

The 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. Regarding the relationship between TCP/IP and HTTP protocol, 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 you want to make the transferred data meaningful, you must use the Application layer protocol, there are many application layer protocols, such as HTTP, FTP, Telnet, etc.
You can define the application layer protocol yourself. 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. Socket programming
At the time of design, we hope 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, which forms 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 section on the socket and TCP/IP protocol relationship is relatively easy to
Solution:

"TCP/IP is just a stack of protocols, just like operating systems, which must be implemented in a specific way, as well as providing an external interface for operations." 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. The socket is a tool for developing port communication, and it needs to be 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 start sending data content to each other until the two sides 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, not only to establish a connection at the time of the request, but also to be able to reply to the server after the client has made 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 in sync. At this point, if the two sides are connected to a socket, the server can directly
The data is passed to the client, and if an HTTP connection is established, the server needs to wait until the client sends a request to send the data back to the client, so the client sends the connection to the server periodically
Not only to stay online, but also to "ask" the server if there is new data, and if so, pass the data to the client.

HTTP protocol is the application layer of the semantic

There is 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.


HTTP common status code

1xxrepresents a notification message, such as a request received or being processed. 2xxto express success, as accepted or known. 3xxindicates redirection, indicating that further action must be taken to complete the request. 4xxrepresents a customer error, such as an incorrect syntax in the request or a failure to complete. 5xxindicates a server error, such as failure of the server to complete the request. 0K;The request is successful and the requested information is returned in the response message.
  
301Moved Permanently:The requested object has been permanently migrated; the newURLin this response message, theLocation :head pointed out. The client software will automatically request this newURL.
  
Request;A status code indicating that the server cannot understand the normal error of the corresponding request 404Not Found:The requested document does not exist on the server.
  
●HTTP Version not support:The server does not support the requestedHTTPThe protocol version.
  

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.