The difference between TCP/IP,HTTP,SOCKET,XMPP

Source: Internet
Author: User
Tags network function

The network is divided from bottom to top

The physical layer, the data link layer, the network layer, the Transport layer, the session layer, the presentation layer, and the application layer.

with a preliminary understanding, I know that the IP protocol corresponds to the network layer, the TCP protocol corresponds to the transport layer, and the HTTP protocol corresponds to the application layer,

The three are inherently not comparable,

The socket is the encapsulation and application of the TCP/IP protocol (at the programmer level).

It can also be said that the TPC/IP protocol is a transport layer protocol, mainly to solve how the data in the network transmission,

http is an application-layer protocol that primarily addresses how data is packaged.

Regarding the relationship between TCP/IP and HTTP protocol, the network has a relatively easy to understand introduction:

"When we transmit data, we can only use ( Transport Layer) TCP/IP protocol, but in that case, the data content cannot be recognized without the application layer.

If you want to make the transferred data meaningful, you must use the application-layer protocol.

There are many application layer protocols, such as HTTP, FTP, Telnet, etc., and you can define the application layer protocol yourself.

The WEB uses the HTTP protocol as an application-layer protocol to encapsulate HTTP text information and then send it to the network using TCP/IP as the Transport layer protocol. ”

and what we usually say the most socket is what, in fact, the socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API).

With the socket, we can use the TCP/IP protocol.

in fact, the Socket is not necessarily associated with the TCP/IP protocol.

The socket programming interface is designed to adapt to other network protocols as well.

So, the advent of sockets just makes it easier for programmers to use the TCP/IP protocol stack, which is an abstraction of the TCP/IP protocol.

This creates some of the most basic functional interfaces we know, such as Create, listen, connect, accept, send, read, write, and so on.

The Network has a section on the socket and TCP/IP protocol relationship is relatively easy to understand:

"TCP/IP is just a stack of protocols, just like operating systems, which must be implemented in a specific way, as well as providing an external interface for operations."

This is like the operating system will provide standard programming interfaces, such as the Win32 programming interface,

TCP/IP also provides the interface for programmers to do network development, which is the socket programming interface. ”

about the TCP/IP protocol only, with the broad and profound I think not too, just check the online about this kind of information and books only the number of documents to know,

This I am going to buy some classic books ( such as "TCP/IP Detailed: Volume One, Volume Two, Volume three") to learn, today, first summarizes some based on TCP/IP protocol-based applications and programming interface knowledge, that is, just said a lot of HTTP and sockets.

CSDN has a comparative image of the description: HTTP is a sedan, provides a package or display the specific form of data; The socket is the engine that provides the ability to communicate over the network.

in fact, the transport layer TCP is based on the network layer of IP protocol, and the application layer of the HTTP protocol is based on the transport layer of the TCP protocol, and the socket itself is not a protocol, as mentioned above, it just provides a TCP or UDP programming interface.

Here are some of the important concepts that are often encountered in a written test or interview and are summarized here.

One, what is the three-time handshake for a TCP connection

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 handshake" (the process is not fine-grained, that is, the server interacts with the client and finally determines the disconnection)

Ii. Steps 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.

Third, the characteristics 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."

iv. differences between TCP and UDP

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.

1 , TCP connections

To understand the socket connection, you need to understand the TCP connection first. 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 a SYN+ack packet from the server, sends an acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and server enter the established state, and three handshakes are completed.

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 handshake "(the process is not fine-grained, that is, the server interacts with the client and finally determines the disconnection)

2 , HTTP connection

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

HTTP The most significant feature of a 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 that a separate connection be established, and the connection is automatically freed after the request is processed.2) in HTTP 1.1You can process multiple requests in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next request.because the HTTPThe connection is actively released at the end of each request, so the HTTP connection is a"Short Connection", to keep the client program online, you need to continually initiate a connection request to the server. The usual practice is that no data is needed immediately, and the client keeps sending it to the server at regular intervals."Stay Connected"request, the server replies to the client after it receives the request, indicating that the client"Online". If the server is unable to receive client requests for a long time, the client is considered"downline", if the client is unable to receive a reply from the server for a long time, the network is considered disconnected.

3 , SOCKET principle

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

3.2 establish a socket connection to establish 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.

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

5 , 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 when the request requires a connection, but also after the client makes a request to the server , the server side can reply to the data.

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, therefore, the client periodically sends a connection request to the server side, Not only can stay online, but also in the " ask " the server whether there is new data, if any, the data to the client.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between TCP/IP,HTTP,SOCKET,XMPP

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.