HTTP vs. TCP with socket definition, difference

Source: Internet
Author: User
Tags ack network function

http://blog.csdn.net/jenminzhang/article/details/47017741

HTTP protocol : In the application layer of Internet Protocol, the role is to specify the format of data transmission, HTTP is based on the socket (here the context refers to the HTTP socket) above the

     TCP/IP protocol is the Transport Layer protocol , which mainly solves how data is transmitted in the network, andHTTP is the application layer protocol , which mainly solves how to wrap the data. The HTTP protocol details the rules for communication between the browser and the server, and is the basis for the World Wide Web exchange of information. HTTP is based on the request-response form and is a short connection, and is a stateless protocol. For its stateless characteristics, in the actual application needs to have the form of the state, so generally through the Session/cookie technology to solve this problem.

Short Connection: Once an HTTP request has been completed, 建立TCP链接 客户端发送报文 服务端返回报文 服务端或客户端关闭此连接 This process, because each time to establish a connection, and after the completion of a request response to close the connection, so everyone called " Short connection ".

1. TCP connection

Mobile phone can use the network function is because the bottom of the mobile phone implementation of TCP/IP protocol, you can make the mobile phone terminal through the wireless network to establish a TCP connection. TCP protocol can provide an interface to the upper network, so that the transmission of the upper network data is based on the "No Difference" network.

Setting up a TCP connection requires a " three-time handshake ":

First handshake: The client sends a SYN packet (SYN=J) to the server and enters the Syn_send state, waiting for the server to confirm;

Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=J+1), and also send itself a SYN packet (syn=k), that is, the Syn+ack packet, when the server enters the SYN_RECV state;

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 the server enter the established state, and the handshake is completed three times.

The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. 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 disconnection process requires a " four-time handshake".

Socket: Socket is not a protocol category, but a calling interface (API),socket is the TCP/IP protocol encapsulation , by calling the socket to use the TCP/IP protocol. The socket connection is a long connection, in theory the client and server side will not actively disconnect this connection once the connection is established. The socket connection is a request-response form, and the server can proactively push messages to the client.

1. Establishing a Socket connection

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.

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

Client request: Refers to the client's 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.

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

2. Socket connection and TCP connection

When you create 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 a connection is made using the TCP protocol.

3. 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. But in the actual network application, 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 you need to tell the network through polling 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.

HTTP vs. TCP with socket definition, difference

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.