Tcp http socket

Source: Internet
Author: User

1. TCP Connection

The mobile phone can use the internet function because the mobile phone bottom layer implements the TCP/IP protocol, which enables the mobile terminal to establish a TCP connection through the wireless network. The TCP protocol can provide interfaces for upper-layer networks, so that data transmission over the upper-Layer Networks is established on a "non-differentiated" network.

To establish a TCP connection, you must go through the "three-way handshake ":

The first handshake: the client sends the SYN Packet (SYN = J) to the server and enters the syn_send status, waiting for confirmation from the server;

The second handshake: when the server receives the SYN packet, it must confirm the customer's Syn (ACK = J + 1) and send a SYN Packet (SYN = K), that is, the SYN + ACK packet, the server enters the syn_recv status;

The third handshake: the client receives the server's SYN + ACK package and sends the ACK (ACK = k + 1) Confirmation package to the server. After the package is sent, the client and server enter the established status, complete three handshakes.

The package transmitted during the handshake does not contain data. After the three-way handshake is completed, the client and the server formally start to transmit data. Ideally, once a TCP connection is established, the TCP connection will remain until either of the two parties closes the connection. When a TCP connection is disconnected, both the server and client can initiate a request to disconnect the TCP connection. The disconnection process must go through the "Four handshakes" (the process is not detailed, that is, the server interacts with the client, final confirmation of disconnection)


2. HTTP Connection

The HTTP protocol, Hypertext Transfer Protocol, is the foundation of Web networking and one of the commonly used protocols for mobile phone networking. HTTP is an application built on the TCP protocol.

The most notable characteristic of HTTP connection is that each request sent by the client requires a server to send a response. After the request ends, the connection is released. The process from establishing a connection to closing a connection is called "One connection ".

1) in HTTP 1.0, each client request requires a separate connection. After processing the request, the connection is automatically released.

2) in HTTP 1.1, multiple requests can be processed in a single connection, and multiple requests can be overlapped without waiting for a request to end before sending the next request.

As HTTP actively releases connections after each request ends, HTTP connections are short connections. To keep the client program online, you must continuously initiate connection requests to the server. The common practice is to immediately get no data, and the client also keeps sending a "keep connection" request to the server at a fixed time, the server replies to the client after receiving the request, indicating that the client is "online ". If the server cannot receive a request from the client for a long time, the client is considered to be "offline". If the client cannot receive a response from the server for a long time, the network is considered disconnected.


3. Socket Principle

3.1 socket Concept

Socket is the cornerstone of communication and the basic operation unit for network communication that supports TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process. It contains five types of information required for network communication: the protocol used for connection, the IP address of the local host, and 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 uses the transport layer for data communication, TCP may encounter concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transmit data through the same TCP port. To differentiate different application processes and connections, many computer operating systems provide socket interfaces for applications to interact with TCP/IP protocols. The application layer and the transport layer can use socket interfaces to distinguish between processes or network connections of different applications to implement concurrent data transmission services.


3.2 establish a socket connection

To establish a socket connection, you must have at least one socket. One of them runs on the client, which is called clientsocket, And the other runs on the server, which is called serversocket.

The connection process between sockets is divided into three steps: server listening, client requests, and connection confirmation.

Server listening: the server socket does not locate the specific client socket, but is waiting for connection. It monitors the network in real time and waits for client connection requests.

Client request: the client socket initiates a connection request, and the target is the server socket. Therefore, the client socket must first describe the socket of the server to be connected, point out the address and port number of the socket on the server, and then submit a connection request to the socket on the server.

Connection Confirmation: when the server socket monitors or receives a connection request from the client socket, it responds to the request from the client socket and creates a new thread, send the description of the server socket to the client. Once the client confirms the description, both parties establish a connection. The server socket continues to be in the listening status, and 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. The socket can support different transport layer protocols (TCP or UDP). When using the TCP protocol for connection, the socket connection is a TCP connection.


5. socket connection and HTTP Connection

Generally, a socket connection is a TCP connection. Once a socket connection is established, both parties can send data to each other until the connection is closed. However, in actual network applications, communication between the client and the server often needs to traverse multiple intermediate nodes, such as routers, gateways, and firewalls, by default, most firewalls disable connections that are not active for a long time and cause the socket connection to be disconnected. Therefore, the network needs to be notified through polling that the connection is active.

The HTTP connection uses the "request-response" method. Not only do you need to establish a connection during the request, but you also need the client to send a request to the server before the server can reply to the data.

In many cases, the server needs to actively push data to the client to ensure real-time and synchronous data between the client and the server. If both parties establish a socket connection, the server can directly transmit data to the client. If both parties establish an HTTP connection, the server must wait until the client sends a request before sending the data back to the client. Therefore, the client periodically sends a connection request to the server, which can not only be kept online, at the same time, it is also "asking" whether the server has new data. If yes, it will send the data to the client.

 

 

Ii. First of all, let's correct the concept that I have always misunderstood. I always thought that HTTP and TCP are two different kinds, but although the status-Peer protocol knows that TCP is the transport layer, HTTP is learned at the application layer today, knowing that HTTP is based on TCP connections. Simply put, TCP simply establishes a connection and does not involve any actual data that we need to request, simple transmission. HTTP is used to send and receive data, that is, the actual application.

First:From the transport layer, let's talk about TCP connections. to connect to the server, we need to use three connections, including request, confirmation, and connection establishment. The legendary three-way handshake protocol ".

First time: C sends a request to connect to the SYN and a random serial number to seq, then s receives the data.

Second time: S received the connection location code for this request. Ah, has someone sent a request to me? Do I want to accept this request? I have to confirm it. So, sent a confirmation code ACN (SEQ + 1), and SYN, seq to C, then C received, this is the second connection.

The third time: C received the confirmation code and compared with the SYN sent earlier, even yo, on it, then he sent an ACN (SEQ + 1) to S, s. After receiving the message, confirm to establish the connection. At this point, the TCP connection is established.

Simple: request, confirm, and connect.

Second:HTTP:

After a TCP connection is established between the client and the application server, the HTTP protocol is used to transmit data. In simple terms, the HTTP protocol is still a request, confirmation, and connection.

In general, C sends an HTTP request to S. S receives the HTTP request and returns the response to chttp. Then C's middleware or browser renders the data into a webpage, displayed in front of the user.

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

Request Header:

Including: 1. The request method is post/get, the request URL, the HTTP protocol version 2. The request data, and the encoding method 3. Whether there are cookies and CooIes, and whether the request is cached.

The difference between post and get requests is that get places the request content behind the URL, but the URL length is limited. Post is a form-based scenario. It is suitable for entering passwords and the like, because it is not displayed in the URL, so it is safer.

Request body:

That is, the request content.

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

Response Header: including 1. Cookies or sessions2. status? 3. Content size?

Response body:

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

Third, after C receives it, the browser completes a series of rendering tasks, including executing JS scripts.

This is what I understand: Basic knowledge of webtcp and HTTP, To be continued .....

 

 

 

 

3. TCP is the underlying communication protocol and defines the specification of data transmission and connection methods.
HTTP is an application layer protocol and defines the content specification for data transmission.

Data in the HTTP protocol is transmitted over TCP. Therefore, HTTP must be supported.

HTTP supports the WWW Service.
While TCP/IP is the Protocol
It is the basis of the Internet. TCP/IP is the basic communication protocol used in the network.

TCP/IP is actually a set of protocols, including hundreds of functional protocols, such as remote logon, file transfer, and email, TCP and IP protocols are two basic important protocols to ensure complete data transmission. Generally, TCP/IP is an Internet Protocol family, not just a TCP/IP or an IP.

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