Socket Connection and HTTP Connection

Source: Internet
Author: User

When transmitting data, we can only use the (Transport Layer) TCP/IP protocol. However, without the application layer, we cannot identify the data content. If we want to make the transmitted data meaningful, you must use the application layer protocol. There are many application layer protocols, such as HTTP, FTP, and telnet. You can also define the application layer protocol by yourself. The Web uses HTTP as the application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it to the network.

1)

Socket is an interface for TCP and UDP programming. You can use it to establish TCP connections. TCP and UDP protocols belong to the transport layer.
HTTP is an application layer protocol, which is also based on the TCP protocol.

 

(HTTP is a car that provides a specific form of encapsulation or display data; socket is an engine that provides network communication capabilities .)

 

 

2)

Socket is the encapsulation of TCP/IP protocol. socket itself is not a protocol, but an interface (API) called. Through socket, we can use TCP/IP protocol. The appearance of socket only makesProgramThe TCP/IP protocol stack is more convenient for Members, which is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know.

 

 

The following are some important concepts, which are excerpted and summarized here.

I. What isTCPThree-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)

II. ExploitationSocketSteps for establishing a network 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.

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

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

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

3.HTTPLink features

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

4.TCPAndUDPDifference

1. TCP is link-oriented. Although network instability and instability determine the number of handshakes, the connection reliability cannot be guaranteed, however, TCP's three-way handshake ensures the connection reliability to a minimum (which is actually guaranteed to a great extent), while UDP is not connection-oriented, before UDP transmits data, it does not establish a connection with the other party, nor does it send a confirmation signal for the received data. The sender does not know whether the data will be correctly received, and of course no re-transmission is required, therefore, UDP is a connectionless and unreliable data transmission protocol.

2. It is also because of the characteristics described in 1 that the UDP overhead is smaller and the data transmission rate is higher, because it does not need to confirm the sending and receiving data, so UDP has a better real-time performance.

 

knowing the difference between TCP and UDP makes it hard to understand why MSN using TCP transmission protocol is slower than QQ using UDP, however, it cannot be said that QQ communication is insecure, because programmers can manually verify UDP data sending and receiving. For example, the sender can number each data packet and then verify it by the receiver, even so, UDP does not use a TCP-like "three-way handshake" in the encapsulation of the underlying protocol to achieve the transmission efficiency that TCP cannot achieve.

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.