HTTP and Socket Connection differences

Source: Internet
Author: User
Tags ack

1, TCP connection

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


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.

Turn from: http://hi.baidu.com/%D2%B9%D1%A9%B3%E6/blog/item/d6a72d2bbf467cf2e7cd406d.html

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.