tcp、udp、http、socket

Network structure:

The network is divided into physical layer, data link layer, network layer, transport layer, session layer, presentation layer, and application layer from bottom to top.

TCP/IP protocol suite:

TCP/IP is a protocol group, which can be divided into three levels: network layer, transport layer and application layer.

At the network layer, there are IP protocol, ICMP protocol, ARP protocol, RARP protocol and BOOTP protocol.
There are TCP protocol and UDP protocol in the transport layer.
At the application layer, there are protocols such as FTP, HTTP, TELNET, SMTP, and DNS.

 

Http protocol:

HTTP belongs to the application layer protocol, and the underlying transport layer is still the tcp protocol. The http protocol is the transport protocol for transmitting hypertext from the Web server to the local browser.

 

TCP/UDP

TCP and UDP belong to the transport layer protocol. One is based on a stable and certain connection (similar to a call, and the call starts after the other party answers), and the other is a connection that is not based on a stable and certain connection (similar to a message, I sent it to you anyway, no Make sure you don’t receive it).

 

TCP (Transmission Control Protocol, Transmission Control Protocol).

TCP is a connection-based protocol, and " connection-oriented" means that a connection must be established with the other party before formal communication. For example, if you call someone else, you must wait until the line is connected and the other party picks up the microphone to talk to each other. In other words, before officially sending and receiving data, a reliable connection must be established with the other party. A TCP connection can only be established after three "dialogs". The process is very complicated. We only make a simple and vivid introduction here. You only need to be able to understand the process.

Let's take a look at the simple process of these three conversations:

1. Host A sends a connection request packet to host B: "I want to send you data, can I?" This is the first conversation;

2. Host B sends to host A a data packet agreeing to connect and requesting synchronization (synchronization means that one of the two hosts is sending, the other is receiving and coordinating work): "Yes, when will you send it?" This is the second conversation ;

3. Host A sends another data packet to confirm host B's request synchronization: "I will send it now, you can continue!" This is the third conversation.

The purpose of the three " dialogues" is to synchronize the sending and receiving of data packets. After three "dialogues", host A officially sends data to host B. The TCP protocol can provide a reliable communication connection for applications, so that the byte stream sent by a computer can be sent to other computers on the network without errors. Data communication systems with high reliability requirements often use the TCP protocol to transmit data.

A tcp connection can only be confirmed to be disconnected after four conversations. The process of four conversations is as follows:

 

UDP (User Data Protocol, user datagram protocol)

UDP is a protocol corresponding to TCP . It is a non-connection-oriented protocol. It does not establish a connection with the other party, but sends the data packet directly!

UDP is suitable for application environments that only transmit a small amount of data at a time and do not require high reliability. For example, we often use the "ping" command to test whether the TCP/IP communication between two hosts is normal. In fact, the principle of the "ping" command is to send a UDP data packet to the other host, and then the other host confirms the receipt of the data packet. If the message of whether the packet arrives is fed back in time, then the network is connected. For example, in the default state, 4 data packets are sent in one "ping" operation (as shown in Figure 2). As you can see, the number of data packets sent is 4 packets, and the number of data packets received is also 4 packets (because the other host will send back a data packet confirming receipt after receiving it). This fully shows that the UDP protocol is a connection-oriented protocol and there is no process of establishing a connection. Because the UDP protocol has no connection process, its communication effect is high; but also because of this, its reliability is not as high as the TCP protocol. QQ uses UDP to send messages, so sometimes it may not receive messages.

The following table lists the comparison of tcp and udp:

 

 

tcp

udp

Whether to connect

Connection-oriented

Connection-oriented

Transmission reliability

reliable

Unreliable

Application occasion

Transfer large amounts of data

Small amount of data

speed

slow

fast


socket programming

What is the socket we usually talk about most ? In fact, socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a call interface (API). Through Socket, we can use TCP/IP protocol.

  In fact, Socket is not necessarily related to the TCP/IP protocol.

  When the Socket programming interface was designed, it was hoped that it could also adapt to other network protocols.

  Therefore, the emergence of Socket only makes it more convenient for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol.

  Thus formed some of the most basic functional interfaces we know, such as create , listen, connect, accept, send, read, write and so on.

  There is a more vivid description on CSDN : HTTP is a car, which provides a specific form of encapsulation or display of data; Socket is an engine, which provides the ability of network communication.

  In fact, the TCP of the transport layer is based on the IP protocol of the network layer, and the HTTP protocol of the application layer is based on the TCP protocol of the transport layer. Socket itself is not a protocol. As mentioned above, it only provides a Interface for TCP or UDP programming.

 

 

 


Guess you like

Origin blog.csdn.net/u012049463/article/details/51104880