HTTP protocol: Simple Object Access Protocol, corresponding to the application layer, HTTP protocol is based on TCP connection
TCP Protocol: Corresponds to the transport layer
IP Protocol: Corresponds to the network layer
TCP/IP is a Transport layer protocol that mainly addresses how data is transmitted over the network, while HTTP is an application layer protocol that mainly addresses how data is packaged.
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 Connections: HTTP connection is the so-called short connection, that is, the client sends a request to the server, the server-side response will be disconnected;
Socket Connection : Socket connection is the so-called long connection, theoretically, the client and server end once the connection will not be actively broken; however, due to various environmental factors may be disconnected, such as: Server-side or client host down, network failure, Or there is no data transfer between the two for a long time, the network firewall may disconnect the connection to free up network resources. So when there is no data transmission in a socket connection, the heartbeat message needs to be sent to maintain the connection ~ ~ The specific heartbeat message format is defined by the developer.
the OSI Reference Model developed by ISO is too large and complex to incur many criticisms. In contrast, the TCP/IP protocol stacks developed by technicians themselves have been widely used. As shown in the diagram, is a comparison diagram of the TCP/IP Reference Model and the OSI reference Model.
The TCP/IP protocol stack is a reference model used by the U.S. Department of Defense's Advanced Research Planning Office computer Network (Advanced study Projects Agency Network,arpanet) and its successor Internet. Arpanet was made by the U.S. Department of Defense (U.S). Department of Defense,dod) sponsored research network. Initially, it connected only four universities in the United States. In the following years, it connected hundreds of universities and government departments through leased telephone lines. Eventually ARPANET developed into the world's largest interconnection network-the Internet. The original ARPANET was permanently closed in 1990.
The TCP/IP Reference Model is divided into four layers: application layer, Transport layer, network interconnect layer and host to network layer. As shown in the figure.
1. TCP/IP connection
The mobile phone can use the networking function because the mobile phone at the bottom of the implementation of the TCP/IP protocol, the mobile phone terminal through the wireless network to establish a TCP connection. The TCP protocol can provide an interface to the upper layer network, so that the transmission of the upper layer network data is based on the "No Difference" network.
Establishing a TCP connection requires a "three handshake":
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;
Second handshake: The server received the SYN packet, must confirm the customer's SYN (ACK=J+1), but also send a SYN packet (syn=k), that is Syn+ack packet, at this time the server into the SYN_RECV state;
Third handshake: The client receives the server Syn+ack packet, sends the confirmation packet ack (ACK=K+1) to the server, this packet sends completes, the client and the server enters established state, completes three times handshake.
Handshake process in the packet is not included in the data, three times after the handshake, the client and the server began to transfer data officially. 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 disconnect process requires a "four handshake" (the process is not fine, that is, the server and client interaction, the final determination of the disconnect).
2, HTTP connection
HTTP protocol, the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the foundation of Web networking and one of the common protocols of mobile networking, and HTTP protocol is an application based on TCP protocol.
The most notable feature of HTTP connections is that each request sent by the client requires a server loopback response, and the connection is actively released after the request is completed. The process from establishing a connection to closing a connection is called a "once connection."
1 in HTTP 1.0, each request from the client requires a separate connection to be established, and the connection is automatically released after the request has been processed.
2 in HTTP 1.1, you can handle multiple requests in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next request.
Because HTTP actively frees connections after each request, HTTP connections are a "short connection" that requires constant connection requests to the server to keep the client program online. The usual practice is to not need to get any data immediately, the client also maintains every fixed time to send the server a "Stay Connected" request, the server after receiving the request to the client reply, indicating that the client "online." If the server is unable to receive the client's request for a long time, the client is considered "offline" and if the client cannot receive a reply from the server for a long time, the network has been disconnected.
3, Socket principle
3.1 Socket (socket) Concept
Socket (socket) is the cornerstone of communication, is the basic operating unit of network communication that supports TCP/IP protocol. It is the abstract representation of the endpoint in network communication, including the five kinds of information that must be used for network communication: The Protocol of the connection, 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 is communicating through the transport layer, TCP encounters problems that simultaneously provide concurrent services for multiple application processes. Multiple TCP connections or multiple application processes may need to transmit data through the same TCP protocol port. To differentiate between different application processes and connections, many computer operating systems provide socket (socket) interfaces for applications to interact with the TCP/IP protocol. The application layer can communicate with the transport layer through the socket interface to distinguish 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 a 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 listening, client requests, connection confirmation.
Server listening: server-side sockets do not locate specific client sockets, but are in the state of waiting for connections, real-time monitoring of network status, waiting for client connection requests.
Client request: Refers to the client socket to make a connection request, the target to be connected is the server-side socket. To do this, the socket for the client must first describe the socket of the server to which it is connecting, 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 the server-side socket supervisor hears or says that a connection request for a client socket is received, in response to the client socket request, create a new thread, the server-side socket description to the client, once the client confirmed this description, the two sides formally establish a connection. While the server-side socket continues to be listening, it continues to receive connection requests from other client sockets.
4. Socket connection and TCP/IP connection
When creating 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 the TCP protocol is used to connect.
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 a transport layer protocol, mainly to solve the data in the network transmission, and HTTP is the application layer protocol, mainly to solve how to package data. On the relationship between TCP/IP and the HTTP protocol, the network has a relatively easy to understand introduction:
"When we're transmitting data, you can use only (transport Layer) TCP/IP protocol, but in that case, if there is no application layer, it can not recognize the content of the data, if you want to make the data transfer meaningful, you must use the Application layer protocol, Application layer protocol, 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 use TCP/IP as a Transport layer protocol to send it to the network. ”
We usually say the most socket is what, 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, there is no inevitable connection between the socket and the TCP/IP protocol. Socket programming interface in the design, it is expected to be able to adapt to other network protocols. So, the appearance of the socket just makes the programmer more convenient to use the TCP/IP protocol stack, 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 and write, and so on. The network has a section on the socket and TCP/IP protocol relationship is easy to understand the argument:
"TCP/IP is just a protocol stack, just like the operating system, it has to be implemented, but also to provide external operational interface." This is like the operating system will provide a standard programming interface, such as Win32 programming interface, TCP/IP also to provide for programmers to do network development interface, this is the socket programming interface. ”
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 an interface for TCP or UDP programming. Socket is a tool for port communication development, it needs to be more low-level.
5, Socket connection and HTTP connection
Since the socket connection is typically a TCP connection, once the socket connection is established, the two sides of the communication can start sending the data to each other until the connection is disconnected. But in the actual network application, communication between clients to the server often needs to traverse multiple intermediate nodes, such as routers, gateways, firewalls, and so on, most firewalls will shut down a long inactive connection and cause the Socket connection to disconnect, 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 when the request is made, but also to require the client to make a request to the server before the server can reply to the data.
In many cases, a server-side initiative is required to push data to the client, keeping the client and server data in real-time and synchronized. At this point if the two sides to establish a socket connection, the server can directly transfer data to the client; If the two sides establish an HTTP connection, the server needs to wait for the client to send a request before the data back to the client, so the client sends a connection request to the server, not only can remain online, It is also the "ask" whether the server has new data, and if so, pass the data to the client.
HTTP protocol is the protocol of application layer
The communication between two computers is nothing more than the data communication between the two ports, and what form the details will be displayed ' is defined as HTTP ' FTP ' with different application layer protocols.
There is a comparative image of the description: HTTP is a sedan, provides a package or display the specific form of data; Socket is the engine that provides the ability of network communication.
<1>socket is an interface for TCP and UDP programming that you can use to establish TCP connections and so on. The TCP and UDP protocols belong to the transport layer.
HTTP is an application-level protocol, and it is actually based on the TCP protocol (HTTP is a sedan that provides a specific form of encapsulation or display of data; the socket is the engine that provides the ability to communicate on the network).
<2>socket is the encapsulation of the TCP/IP protocol, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol. The presence of the socket only makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol, creating some of the most basic function interfaces we know. Short Connection
Connect-> Transmit data-> close connection
HTTP is stateless, and the browser and server make a connection once for each HTTP operation, but the connection is interrupted at the end of the task.
You can also say this: Short connection refers to the socket after the connection sent after receiving data immediately disconnect.
Long Connection
Connect-> transmit Data-> Keep connected-> transmit data-> ... -> closes the connection.
A long connection is a connection that is maintained after the socket connection is established, regardless of whether it is used, but is less secure.
long connection to HTTP
HTTP can also establish long connections, using Connection:keep-alive,http 1.1 to default to persistent connections. The biggest difference between HTTP1.1 and HTTP1.0 is the addition of persistent connection support (which appears to be the latest http1.0 the specified keep-alive), but stateless or untrustworthy.
when to use long connection, short connection.
Long connection is often used for frequent operation, point-to-point communication, and the number of connections can not be too much. Each TCP connection requires a three-step handshake, which takes time, if each operation is the first connection, then the processing speed will be reduced a lot, so after each operation is constantly open, secondary processing directly send packets on OK, do not establish a TCP connection. For example: A database connection with a long connection, if the use of short connection frequent communication will cause socket errors, and frequent socket creation is also a waste of resources.
HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server, the connections of thousands or even billions of clients, such as Web sites, are more resource-intensive, if connected with long, and thousands of users, Imagine if each user uses a connection. Therefore, a large number of concurrent, but each user without frequent operation of the need for a short link good.
In short, the choice between long and short connections depends on the situation.
Send receive Mode
1, asynchronous
Message sending and receiving are separate, independent, and do not affect each other. This approach is divided into two different situations:
(1) Asynchronous duplex: Receive and send in the same program, two different sub processes are responsible for sending and receiving
(2) Asynchronous Simplex: receiving and sending is done with two different programs.
2, synchronization
Message sending and receiving is synchronous, both message sending and waiting for receiving return message. The synchronization method generally needs to consider the timeout problem, that is, the message sent out after not infinite wait, need to set the timeout time, more than the time the sender is no longer waiting to read the return message, direct notification timeout return.
In the long connection there is generally no condition to determine when reading and writing to end, so must be added to the length of the message header. The reading function reads the length of the packet head, and then reads the corresponding length of the message according to the length.