TCP connections, HTTP connections and socket connections

Source: Internet
Author: User
Tags ack

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

Second: First of all, to correct the concept that I have misunderstood before, I've always thought HTTP and TCP are two different, but Peer-to-peer protocols, although knowing that TCP is the transport layer, and that HTTP is the application layer today, learning that HTTP is based on TCP connectivity, simply, TCP is simply a connection that does not involve any actual data that we need to request, simple transmission. HTTP is used to send and receive data, which is actually applied.

First: from the transport layer, first of all, the TCP connection, we want to connect with the server TCP connection, need to connect through three times, including: request, confirm, establish the connection. The legendary "three handshake agreement".

The first time: C sends a request to connect the bit code SYN and a randomly generated serial number to SEQ, then s receives the data.

Second time: s receives the bit code for this request connection, oh, has someone made a request to me, then I have to accept his request, to achieve a confirmation, so, sent a confirmation code ACN (seq+1), and Syn,seq to C, and then C received, this is the second connection.

The third time: C received the confirmation of the code and sent before the Syn a comparison, even yo, on the Mody, so he sent a ACN (seq+1) to s,s received after the determination to establish a connection, thus, the TCP connection is established.

Simple is: request, confirm, connect.

Second: from the actual data application http:

After the previous client and the application server establish a TCP connection, it is necessary to use the HTTP protocol to transmit the data, the HTTP protocol simply, or request, confirm, connect.

The overall is C send an HTTP request to s,s received this HTTP request, and then return to the chttp response, then C middleware or browser to render the data as a Web page, displayed in front of the user.

First: Send an HTTP request to S, which includes the request header and the request content:

Request Header:

Included, 1. The requested method is Post/get, the requested Url,http protocol version 2, the requested data, and the encoding mode 3 whether there are cookies and cooies, caching, etc.

The difference between a post and a GET request is that it places the request behind the URL, but the URL length is limited. And post is a form of the situation, appropriate to enter the password, and so on, because it is not displayed in the URL, so more secure.

Request Body:

That is the requested content.

Second: s receives the HTTP request, and then returns the HTTP response based on the request header.

Response header: Includes 1.cookies or Sessions2. Status 3. Content size etc

Response Body:

That is, the content of the response, including, JS or something.

Third, after the C received, the browser to complete a series of rendering, including the implementation of JS script.

This is what I understand webtcp,http basic knowledge, to be continued ...

Third, TCP is the underlying communication protocol, defined is the data transmission and connection mode of the specification
HTTP is the application layer protocol that defines the specification of the content that transmits data

The data in the HTTP protocol is transmitted using the TCP protocol, so supporting HTTP also necessarily supports TCP

HTTP is supported by WWW service
and TCP/IP is protocol
It is the foundation of Internet International Network. TCP/IP is the basic communication protocol used in the network.

TCP/IP is actually a set of protocols that includes hundreds of different features, such as remote logins, file transfers, and e-mail, while TCP protocols and IP protocols are the two basic protocols that guarantee the full transmission of data. TCP/IP is often said to be an Internet protocol family, not just TCP and 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.