The difference between TCP/IP, Http, and socket

Source: Internet
Author: User
Tags ack

Network this piece the more understanding feeling know the less, so study still want to continue,,,,,,

Socket

0. Several nouns:

Ipc->inter process Communication, interprocess communication

Socket-> sockets

Tcp->transmission Control Protocol Transmission Protocol,

Provides a connection-oriented, reliable byte-stream service. Before the customer and the server Exchange data with each other, a TCP connection must be established between the two parties before the data can be transferred. TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other.

Note: The TCP protocol is also known as the "three-time Handshake protocol";

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

Udp:->user Datagram Protocol, User Datagram Protocol

is a simple transport-layer protocol for datagrams. UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is very fast.

Tcp/ip-> Transmission Control Protocol/Inter-network protocol,

Defines the standard of how hosts connect to the Internet and how data is transferred between them, which is a protocol family (many protocols ...)

The TCP/IP protocol family contains many sub-protocols with different functions. The TCP/IP hierarchy model is divided into four layers: application layer, Transport layer, network layer, data link layer. (different from the 7-layer model)

Ip->internet Protocol, Network Interconnection Protocol

is a set of rules that enable all computer networks connected to the Internet to communicate with each other, stipulating rules that should be observed when computers communicate on the Internet.

IP address->internet Protocol address, Internet Protocol, computer identity on the network.

Static IP Address: a fixed IP address that needs to be set manually by the user.

Dynamic IP Address: An IP address that is automatically generated through the DHCP protocol.

Subnet mask: Use with IP addresses to determine whether two computers are on the same subnet.

DNS server: You can convert a domain name (URL) to an IP address.

DHCP protocol, through the DHCP protocol, the user obtains the native dynamic IP address, the subnet mask, the gateway, the DNS server and so on.

ARP protocol, address Resolution Protocol, addresses resolution Protocol

is a TCP/IP protocol that obtains a physical address based on its address, and a MAC address via an IP address. With the ARP protocol, you can get the host MAC address within the same subnet. You can send packets to any host. Between different subnets: If the two hosts are not on the same subnet, Unable to get the MAC address of the other party, the packet can only be sent to the gateway at two sub-network connections for the gateway to process.

MAC address, network card address, network card's unique identity

Port number---transport layer to each application using a network card number, that is, "port" number, Interval 0-65536

The corresponding application can be found by port number;

The HTTP connection-->http protocol is the Hypertext Transfer Protocol (hypertext Transfer Protocol),

is the foundation of Web networking, is also one of the commonly used mobile phone network protocol, the HTTP protocol is built on the TCP protocol, an application, the most significant feature of the HTTP connection is that each request sent by the client requires a server loopback response, after the request ends, the connection is actively released. 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.

1.OSI seven-layer model

From the bottom of the first

Physical Layer

① function: Connect the computer, responsible for transmitting ' 0 ' and ' 1 ' electrical signals

② Typical equipment: optical fiber, coaxial cable, twisted pair, etc.

Data Link Layer

① function: Processing electrical signals, Ethernet protocols (packets) to group data. That is, the signal is transformed into a binary data that can be used in the network and then transmitted, where the data is framed as a processing unit.

② Typical equipment: bridges, switches, repeaters, etc.

Network layer

① function: Find the "only" computer in the network, establish the host (IP) and host (IP) connection;

It is also regarded as: the path selection between different network systems based on the network layer address (IP address)

② Typical device: router

③ If you are on the same subnet, send the packet in a broadcast manner. If you are in a different subnet, send the packet in a "routed" manner.

Transport Layer

① Effect: The network layer can only let the computer receive and send information, but do not know which application to pass to. The transport layer gives each application using a network card a number, which is the "port" number; 0-65536; establishes communication between "port/Application" to "Port/Application".

② Typical device: Terminal equipment (PC, mobile phone, tablet, etc.)

③socket

Socket = = host + port;

The most common socket programming is: TCP/IP programming;

It is important to note that the socket is only a communication model and does not belong to the Network Seven layer protocol.

Session Layer

① function: In order to realize automatic sending and receiving data, automatic addressing function, the session layer is introduced. The role of the session layer is to establish and manage communication between applications. The session layer does not participate in specific transports, it provides mechanisms for establishing and maintaining communication between applications, including access authentication and session management. If the server verifies that the user is logged in, it is done by the session layer.

② Typical device: Terminal equipment (PC, mobile phone, tablet, etc.)

Presentation Layer

① role: To solve the communication problems between different systems, can be regarded as a "translation officer."

② Typical device: Terminal equipment (PC, mobile phone, tablet, etc.)

Application Layer

① Effect: Specifies the data format for different applications.

② Typical device: Terminal equipment (PC, mobile phone, tablet, etc.)

HTTP protocol: Hypertext Transfer Protocol

File protocol: Document Transfer Protocol

Mail protocol: Email protocol

2. What is a socket? What is a socket used for?

Two processes (Programs) if you need to communicate the most basic premise can uniquely identify a process, in the local process communication we can use the PID (Progress ID) to uniquely identify a process, but the PID is only local only, the network of two process PID collision probability is very large, At this point we need to separate the path, we know that the IP address can uniquely identify the host, and the TCP layer protocol and port number can uniquely identify a host process, so that we can use the IP address + protocol + port number to uniquely identify a process in the network.

The ability to uniquely identify the processes in the network, they can use the socket to communicate, what is the socket it? We often translate the socket into sockets, the socket is an abstraction layer between the application layer and the transport layer, which abstracts the complex operations of the TCP/IP layer into a few simple interfaces to provide a layer to invoke the implemented process to communicate in the network.

What is the difference between 3.Http and socket connection?

TCP/IP is a protocol group that can be divided into three levels: Network layer, Transport layer and application layer. At the network layer are IP protocols, ICMP protocols, ARP protocols, RARP protocols, and BOOTP protocols. There are TCP protocols and UDP protocols in the transport layer. In the application layer, there are FTP, HTTP, TELNET, SMTP, DNS and other protocols. Therefore, HTTP itself is a protocol that transmits hypertext to a local browser from a Web server. 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, and the three are not inherently comparable.

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

4. Socket principle

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

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

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

6. Socket connection and HTTP connection

When we transmit data, we can only use the (Transport Layer) TCP/IP protocol, but then, if there is no application layer, it will not be able to identify the data content, if you want to make the transferred data meaningful, you must use the Application layer protocol, the application layer protocol, such as HTTP, FTP, Telnet, etc., You can also 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.

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.

7. Socket and HTTP differences

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.

Socket:socket is not a protocol category, but a calling interface (API), socket is the encapsulation of the TCP/IP protocol, by calling the socket, the TCP/IP protocol can be used. 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.

8. The difference between TCP and UDP

1. TCP is link-oriented, although the network's insecure instability determines how many times the handshake can not guarantee the reliability of the connection, but the TCP three handshake at least (in fact, to a large extent guaranteed) to ensure the reliability of the connection, and UDP is not a connection-oriented, UDP transmission data before the connection with the other party, the data received is not sent to confirm the signal, the sender does not know whether the data will be properly received, of course, there is no need to resend, so that UDP is a non-connected, unreliable data transmission protocol.

2. Also due to the characteristics of 1, so that the cost of UDP more small 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 the programmer can manually verify the data sent and received by UDP, For example, the sender of each packet number and then by the receiver to verify AH what, even so, UDP because the package on the underlying protocol does not adopt a TCP-like "three-time handshake" to achieve the TCP can not achieve the transmission efficiency.

The difference between TCP/IP, Http, and 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.