HTTP, TCP/IP, socket

Source: Internet
Author: User
Tags ack

In the literal sense TCP/IP is the general term for TCP and IPs, but in fact the TCP/IP protocol refers to the entire TCP/IP protocol family. Unlike the ISO model's seven tiers, the TCP/IP protocol Reference Model classifies all TCP/IP series protocols into four layers of abstraction
Application layer: Tftp,http,snmp,ftp,smtp,dns,telnet, etc.
Transport Layer: TCP,UDP
Network layer: IP,ICMP,OSPF,EIGRP,IGMP
Data Link layer: SLIP,CSLIP,PPP,MTU
Each abstraction layer is built on the service provided on the lower tier and serves the upper tier, which looks like this



TCP initiates a timer each time a message segment is sent, and if no ACK is received after the timer expires, the message is re-transmitted. , the packet is sent by the buffer of a to b,b after the packet is received, an ACK acknowledgement packet is sent back to a, and a is freed from the buffer. Therefore, the packet is cached in a buffer until an ACK is confirmed. TCP initiates a timer each time a message segment is sent, and if no ACK is received after the timer expires, the message is re-transmitted. , the packet is sent by the buffer of a to b,b after the packet is received, an ACK acknowledgement packet is sent back to a, and a is freed from the buffer. Therefore, the packet is cached in a buffer until an ACK is confirmed.

In the TCP/IP protocol, the TCP protocol provides a reliable connection-oriented service, a three-time handshake (establishing a connection) and four waves (closing the connection), and a sliding window mechanism for flow control;

three-time handshake

In the TCP/IP protocol, the TCP protocol establishes a reliable connection through a three-time handshake

First handshake: The client tries to connect to the server, sends a SYN packet to the server (synchronization sequence number Synchronize Sequence Numbers), SYN=J, the client enters Syn_send state waits for the server to confirm

Second handshake: The server receives the client SYN packet and confirms (ACK=J+1), and sends a SYN packet (SYN=K) to the client, which is the Syn+ack packet, when the server enters the SYN_RECV state

Third handshake: The 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 server enter the established state, complete three handshake

The purpose of the three-time handshake is to synchronize the serial number and confirmation number of both parties and Exchange TCP window size information. Socket Communication Process
Socket originated from UNIX, in Unix everything is file philosophy, socket is an "open-read/write-off" mode implementation, the server and the client maintain a "file", after establishing a connection open, you can write to their own files for the other side to read or read the contents of the other side, Closes the file at the end of the communication.
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.

The socket is an implementation of "open-read/write-off" mode, which uses the TCP protocol communication socket as an example, and its interaction process is probably the same as the socket communication flow
Socket originated from UNIX, in Unix everything is file philosophy, socket is an "open-read/write-off" mode implementation, the server and the client maintain a "file", after establishing a connection open, you can write to their own files for the other side to read or read the contents of the other side, Closes the file at the end of the communication.
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.

The socket is an implementation of "open-read/write-off" mode, which uses the TCP protocol communication socket as an example, and its interaction flow is probably

To establish a network connection using a socket

 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.

  1, Server monitoring: Server-side sockets do not locate the specific client sockets, but in the status of waiting for the connection, real-time monitoring network status, waiting for the client connection request.

2, client request: Refers to the client 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.

3. Connection confirmation: When the server side socket is heard or received the connection request of the client socket, it responds to the request of the client socket, establishes a new thread, sends the description of the server end 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.

Features of HTTP links

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.

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

  Four, the difference between TCP and UDP (the most test). It's getting rotten, I think-----)

1,TCP is a link-oriented, although the security of the network instability characteristics determine how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at a minimum (in fact, to a large extent guaranteed) to ensure the reliability of the connection;

And UDP is not connection-oriented, UDP transmission data before the connection with the other, the docking received data does not send a confirmation signal, the sender does not know whether the data will be received correctly, of course, there is no need to resend, so that UDP is a non-connected, unreliable data transmission protocol.

2, also due to 1 of the characteristics of the said, so that the cost of UDP smaller data transmission rate is higher, because there is no need to send and receive data confirmation, so UDP real-time better.

 Know the difference between TCP and UDP, it is not difficult to understand why the use of TCP transmission protocol MSN than UDP transmission file slow, but can not say that QQ communication is not safe,

Because programmers can manually authenticate UDP data, such as the sender of each packet number and then by the receiver to verify ah what?

Even so, UDP does not have a TCP-like "three-time handshake" on the package of the underlying protocol to achieve the transmission efficiency that TCP cannot achieve.

We visit a Web page, where various protocols play a role.

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.


First of all, to correct the concept I have always misunderstood, I always thought that HTTP and TCP are two different, but the status of the same protocol, although know that TCP is the transport layer, and HTTP is the application layer learned today, know that HTTP is based on the TCP connection, simply say, TCP is simply to establish 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, the first is the TCP connection, we want to connect with the server TCP connection, need to pass three connections, including: request, confirm, establish the connection. The legendary "three-time Handshake protocol".

First: C sends a request for a concatenated bit code SYN and a randomly generated sequence number to the SEQ, and then s receives the data.

Second time: S received this request to connect the bit code, ah, someone sent me a request, then I would like to accept his request, to achieve 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 a confirmation of the code and before the SYN sent a comparison, even yo, on the, so he sent a ACN (seq+1) to s,s received after the establishment of a connection, so that the TCP connection is completed.

Simple is: request, confirm, connect.

Second: from the actual data application, http:

After the previous client and 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 sends an HTTP request to s,s received this HTTP request, and then returns to the Chttp response, then C middleware or browser to render the data into a Web page, displayed in front of the user.

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

Request Header:

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

The difference between a post and a GET request is that get puts the request content behind the URL, but the URL length is limited. Post is a form of the situation, suitable for entering a password, and so on, because it is not displayed in the URL, so it is more secure.

Request Body:

That is, the requested content.

Second: s receives an HTTP request and then returns an 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, C received a series of rendering by the browser, including the execution of JS script.




HTTP, TCP/IP, socket

Related Article

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.