Http, sockets, and TCP/IP

Source: Internet
Author: User
Tags ack network function

The network seven layer is from the bottom up to the physical layer, the data link layer, the network layer, the transport layer, the conversation layer, the presentation layer and the application layer respectively.

The physical layer, the data link layer and the network layer are often called media layers, which are the objects researched by network engineers.

The transport layer, the session layer, the presentation layer, and the application layer are referred to as the host layer and are the content that the user is looking for and cares about.

The HTTP protocol corresponds to the application layer

The TCP protocol corresponds to the transport layer

IP protocol corresponds to the network layer

The three are not comparable in nature. Moreover, the HTTP protocol is based on TCP connections.

TCP/IP is a Transport layer protocol that mainly addresses how data travels across the network, while HTTP is an application-layer protocol that addresses how data is packaged.

When we transmit data, we can use only the transport layer (TCP/IP), but in that case, because there is no application layer, the data content is not recognized, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, there are HTTP, FTP, Telnet and so on, You can also define the application layer protocol yourself. The web uses HTTP as the Transport Layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol.

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.

HTTP and socket connection differences

I believe many of the novice mobile phone networking development friends want to know what is the difference between the HTTP and socket connection, hope that through their own shallow understanding can help beginners.

1. TCP connection

To understand the socket connection, you need to understand the TCP connection first. 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, 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 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 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 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 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 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 sides 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 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.


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

what does an HTTP connection mean?

HTTP is an object-oriented protocol belonging to the application layer, which is suitable for distributed hypermedia information System because of its simple and fast way. It was proposed in 1990, after several years of use and development, has been continuously improved and expanded. Currently used in the WWW is the sixth edition of Http/1.0, http/1.1 standardization work is in progress, and Http-ng (Next Generation of HTTP) has been proposed. (Agreement, Global Positioning!) )

The core--http Protocol of WWW

As we all know, the basic protocol of the Internet is TCP/IP protocol, the current widely used FTP, Archie Gopher, etc. are based on the TCP/IP Protocol Application layer protocol, different protocols corresponding to different applications. The main protocol used by the WWW server is the HTTP protocol, the hyper-stylistic transfer protocol. Because the HTTP protocol supports services that are not limited to WWW, it can be other services, and thereforeThe HTTP protocol allows users to access different services under a unified interface, such as FTP, Archie, SMTP, NNTP, and so on, with different protocols .。 In addition, the HTTP protocol can also be used for name servers and distributed object management.

2.1 HTTP Protocol Introduction
HTTP is an object-oriented protocol belonging to the application layer, which is suitable for distributed hypermedia information System because of its simple and fast way. It was proposed in 1990, after several years of use and development, has been continuously improved and expanded. Currently used in the WWW is the sixth edition of Http/1.0, http/1.1 standardization work is in progress, and Http-ng (Next Generation of HTTP) has been proposed.
The main features of the HTTP protocol can be summarized as follows:
1. Support client/server mode.
2. Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server.because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.
3. Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.
4. No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.
5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.

Several important concepts of 2.2 HTTP protocol
1. Connection (Connection): The actual circulation of a transport layer, which is built between two applications that communicate with one another.
2. Message: The basic unit of HTTP communication, including a structured eight-tuple sequence and transmission via a connection.
3. Request: A request from the client to the server includes the method that is applied to the resource, the identifier of the resource, and the version number of the Protocol
4. Response (Response): A message returned from the server includes the version number of the HTTP protocol, the status of the request (for example, "succeeded" or "not Found"), and the MIME type of the document.
5. Resource (Resource): A network data object or service that is identified by a URI.
6. Entity: A special representation of a data resource or a reflection from a service resource, which may be surrounded by a request or response message. An entity includes entity header information and the entity's own content.
7. Client: An application that establishes a connection for the purpose of sending a request.
8. User agent: Initializes a requested client. They are browsers, editors, or other user tools.
9. Server: An application that accepts a connection and returns information to the request.
10. Source Server (Origin server): is a server on which a given resource can reside or be created.
11. Proxy: An intermediary program that can act as a server or as a client to establish requests for other clients. Requests are either internally or passed to other servers through possible translations. An agent must interpret and overwrite it if possible before sending the request information.
Proxies are often used as portals through the firewall's client side, and proxies can be used as a help app to handle requests that are not completed by the user agent through the protocol.
12. Gateway: A server that acts as an intermediary for other servers. Unlike the proxy, the gateway accepts the request as if it were the source server for the requested resource, and the requesting client is unaware that it is dealing with the gateway.
Gateways are often used as server-side portals through firewalls, and gateways can be used as a protocol translator to access resources stored in non-HTTP systems.
13. Channel (tunnel): is a broker that acts as a two connection relay. Once activated, the channel is considered not to be an HTTP communication, although the channel may be initialized by an HTTP request. The channel disappears when both ends of the relayed connection are closed. A channel is often used when a portal must exist or the intermediary (intermediary) cannot interpret the relay's traffic.
14. Cache: Local storage of the reaction information.

2.3 How the HTTP protocol works
The HTTP protocol is based on the request/response paradigm. After a client establishes a connection with the server, it sends a request to the server in the form of a request,the Uniform Resource Identifier, the protocol version number, and the MIME information behind it include the request modifier, client information, and possible content. After the server receives the request, it gives the corresponding response information in the formatA status line includes the protocol version number of the information, a successful or incorrect code, followed by MIME information including server information, entity information, and possible content。
Many HTTP traffic is initialized by a user agent and includes a request to request resources on the source server. The simplest scenario could be a separate connection between the user agent (UA) and the source server (O) (see Figure 2-1).

When one or more mediations appear in the request/response chain, the situation becomes more complex. The mediation consists of three types: proxy, gateway, and channel (tunnel).

An agent accepts requests based on the absolute format of the URI, rewrites all or part of the message, and sends the formatted request to the server through the URI's identity.

The gateway is a receiving agent that acts as the upper layer of some other servers and, if necessary, translates the request to the underlying server protocol.

A channel acts as a relay point between two connections that do not change the message. The channel is often used when the communication needs to pass through an intermediary (for example, a firewall, etc.) or if the content of the message is not recognized by the intermediary. Figure 2-2
Figure 2-2 above shows that there are three mediations (a, B, and C) between the user agent (UA) and the source server (O). A request or response message through the entire chain must pass through four connection segments. This distinction is important because some of the HTTP communication choices may be applied to the nearest connection, the neighbor without the channel, to the end of the chain, or to all connections that are applied along the chain. Although figure 2-2 is linear, each participant may engage in multiple, concurrent communication. For example, B may receive a request from many clients without passing a, and/or send the request to a without C, at the same time it may also process a request.
Any aggregation against non-as-a-channel may enable an internal cache for processing requests. The effect of caching is that the request/response chain is shortened, and the condition is that one of the participants along the chain has a cached response acting on that request. Describes the result chain, which is conditional on a request not being cached by UA or a, and B has a cached copy of a pre-response through C from O.
Figure 2-3
On the Internet, HTTP traffic typically occurs on top of a TCP/IP connection. The default port is TCP 80, but the other ports are also available. However, this does not imply that the HTTP protocol can be completed on top of other protocols on the Internet or other networks. HTTP is only indicative of a reliable transmission.
The above is a brief introduction of the HTTP protocol macro operation, the following describes the HTTP protocol internal operation process.
First, the information exchange process of the client/server mode based on the HTTP protocol is briefly introduced, and 2-4 shows that it is divided into four processes, which establish the connection, send the request information, send the response information, and close the connection.
Figure 2-4

In www, "Customer" and "server" are a relative concept that exists only during a particular connection, that is, a customer in one connection may be a server in another connection. When the WWW server is running, it is listening on the TCP80 port (the default port of www), waiting for the connection to appear.

Below, we discuss the implementation of the information exchange in the client/server mode under the HTTP protocol.

1. The establishment of a connection connection is achieved through the application of a socket socket. The client opens a socket and constrains it to a port, which, if successful, is the equivalent of creating a virtual file. You can then write data on the virtual file and send it out through the network.
2. Sending the request
After a connection is opened, the client sends the request message to the server's dwell port to complete the request action.
The format of the http/1.0 request message is:
Request message = Request Line (General Information | request Header | entity header) crlf[entity content]
Request line = Method Request URL http version number CRLF
Method =get| head| Post| extension methods
U R l= protocol name + host name + directory and file name
The methods in the request line describe the actions that should be performed in the specified resource, and the commonly used methods are, and post.

The result of a different request object corresponding to get is different, and the corresponding relationship is as follows:
Result of Object get
Contents of the File file
Program execution results of the program
Database query Query Results
head--requires the server to look up the meta information of an object, not the object itself.
post--transmits data from the client to the server, and the Post method is used when the server and CGI are required to do further processing. Post is primarily used to send the contents of a form in HTML text to be processed by the CGI program.

An example of a request is:
GET http://networking.zju.edu.cn/zju/index.htm http/1.0
Header information is also called meta-information, that is, information, the use of meta-information can be used to achieve conditional requests or responses.
The request header-tells the server how to interpret the request, mainly including the types of data that the user can accept, the compression method, and the language.
Entity Header--Entity information type, length, compression method, last modification time, data expiration, etc.
Entity--The request or response object itself.
3. Send a response
The server sends a response message to the client after it has finished processing the client's request.
The response message format for http/1.0 is as follows:
Response message = Status line (General Information Header | response header | entity header) crlf(entity content)
Status line =http version number status code reason narration
Status codes represent response types
1XX reserved
2XX indicates that the request was successfully received by
3XX request further refinement of requests for completion of customer requests
4XX Customer Error
5XX Server Error
The response header information includes the service program name, notifies the client that the requested URL requires authentication, and when the requested resource is available.
4. Close the connection
Both client and server can end TCP/IP conversations by closing sockets

Http, sockets, and TCP/IP

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.