[Android interview questions] 2023 latest interview topics: network programming (1)

1 Please describe the process and significance of TCP three-way handshake and four-way wave

What is this question trying to investigate?

This problem belongs to the basic theoretical knowledge in the network system. If you do not have a clear understanding of this type of problem, it will make you have no support when mastering some "high-end" technologies, and it will be difficult to straighten out the overall framework. For example, widely used application layer protocols such as Http, RTSP, and RTMP are implemented based on TCP. So it's not unusual for this question to be asked.

Knowledge points of inspection

Basics of networking

How Candidates Answer

The TCP/IP protocol defines how the computer sends data in the network, how to define the data format, how to address and find the target computer in the network after sending the message, and finally how the target computer checks the correctness of the received message, and finally disassembles the data. Get a set of processing criteria for message content.

With these standards, software vendors that produce and provide TCP/IP services will have a set of unified specifications, as long as they follow this specification to realize their software functions.

three handshake

Before conducting business communication, a connection must be established, and the establishment of a TCP/IP connection requires a three-way handshake process. The process is as follows:
insert image description here

  1. The first handshake: When the connection is established, the client sends a syn packet (syn=j) to the server, and enters the SYN_SENT state, waiting for the server to confirm; SYN: Synchronize Sequence Numbers.
  2. The second handshake: the server receives the syn packet, must confirm the customer's SYN (ack=j+1), and at the same time send a SYN packet (syn=k), that is, the SYN+ACK packet, and the server enters the SYN_RECV state at this time;
  3. The third handshake: the client receives the SYN+ACK packet from the server and sends an acknowledgment packet ACK (ack=k+1) to the server. After the packet is sent, the client and server enter the ESTABLISHED (TCP connection successful) state and complete three times shake hands.
Why the three-way handshake?
full duplex communication

The three-way handshake is the minimum number of times to determine that the communication line between the two parties is full-duplex. Full-duplex communication means that both parties to the communication can send and receive information at the same time.

Just like the two parties talking on the phone:

A: Hello, can you hear me?

B: yes

At this time, if A has no feedback, B cannot determine whether A can receive data.

Guaranteed reliability

In addition, TCP is a reliable transmission protocol, and the means to ensure the reliability of communication include sequence numbers and confirmation response mechanisms.

  • Serial number: During TCP transmission, each byte of data is numbered to ensure the order and reliability of the data (when the received data always lacks a certain serial number, it can be known immediately);
  • Confirmation response: In the process of TCP transmission, each time the receiver receives data, it will confirm the response to the transmitter. That is, an ACK message is sent. This ACK message contains the corresponding confirmation sequence number, which tells the sender what data has been received and where to send the next data.

The three-way handshake can also determine the initial serial numbers of the two parties in communication.

  1. C --> S SYN my sequence number is X
  2. S <-- C ACK your sequence number is X my sequence number is Y
  3. C --> S ACK your sequence number is Y

If C does not acknowledge receipt of B's. That is to say, only the packets sent from C to S are reliable, but the packets sent from S to C are not, so this is not a reliable connection.

Avoid wasting resources

In addition, the first handshake: the client sends a connection request message to the server, and the server needs to perform a second handshake after receiving the information: the response informs the client that the connection request has been received. The response message sent by the server needs to wait for the third handshake response from the client to determine that the connection is a valid connection.

If the first connection request sent by the client is delayed in the network node for some reason, and it does not reach the server until a certain point after the client gives up the connection, this is a message that has already expired, but At this time, the server still thinks that this is the first handshake of the client's connection establishment request, so the server responds to the client with the second handshake. If there is no third handshake, then here, the connection is established, but at this time the client does not have any data to send, which will make the server wait empty, resulting in waste of resources.

waved four times

After completing the data interaction, if you choose to close the connection to reclaim resources, complete four waved hands to perform a "peaceful breakup". The process is as follows:

insert image description here

  1. The first wave: the active closing party sends the first packet, in which the FIN flag is 1, and the sending sequence number seq is X.
  2. The second wave: the passive closing party sends the second packet after receiving the FIN packet, in which the sending sequence number seq is Z, and the receiving sequence number ack is X+1.
  3. The third wave: the passive closing party sends a third packet, in which the FIN flag is 1, the sending sequence number seq is Y, and the receiving sequence number ack is X.
  4. The fourth wave: the active closing party sends the fourth packet, in which the sending sequence number is X and the receiving sequence number is Y. So far, four waved hands have been completed.
Why does disconnecting require four hand waves?

The three-way handshake is because when a connection is established, ACK and SYN can be sent in one message. When closing the connection, the passive closing party may still need to send some data, and then send a FIN message to express agreement that the connection can be closed now, so the ACK message and the FIN message here are sent separately in most cases. So it takes 4 times to disconnect.


2 Talk about your understanding of the difference between TCP and UDP (Tencent)

What is this question trying to investigate?

In normal development, in most cases, the Http/Https protocol is used to complete the network interaction with the server, and the bottom layer of Http is a reliable connection based on TCP. There are two representative transport layer protocols in TCP/IP, namely TCP and UDP. Mastering the difference between the two allows us to choose the optimal transport protocol reasonably in different scenarios.

Knowledge points of inspection

Basics of networking

How Candidates Answer

TCP/IP is a general term for various Internet-related protocol clusters, such as: TCP, UDP, IP, FTP, HTTP, ICMP, SMTP, etc. all belong to the TCP/IP protocol cluster. The reason why it is named TCP/IP protocol is because TCP and IP protocols are two very important protocols, so they are named after them.

UDP

The full name of the UDP protocol is the User Datagram Protocol ( User Data Protocol ). In the network, it is used to process data packets like the TCP protocol. It is a connectionless protocol. In the OSI model, in the fourth layer - the transport layer, it is the upper layer of the IP protocol. UDP has the disadvantages of not providing data packet grouping, assembling and sorting of data packets, that is to say, after the message is sent, it is impossible to know whether it has arrived safely and completely.

It has the following characteristics:

  • connectionless oriented

    First of all, UDP does not need to perform a three-way handshake to establish a connection before sending data like TCP. If you want to send data, you can start sending. And it is only a porter of the data message, and will not perform any splitting and splicing operations on the data message.

    Specifically:

    • At the sending end, the application layer passes the data to the UDP protocol of the transport layer. UDP only adds a UDP header to the data to identify the UDP protocol, and then passes it to the network layer.
    • At the receiving end, the network layer passes the data to the transport layer, and UDP only removes the IP header and passes it to the application layer without any splicing operation
  • There are unicast, multicast and broadcast functions

    UDP not only supports one-to-one transmission mode, but also supports one-to-many, many-to-many, and many-to-one modes, that is to say, UDP provides unicast, multicast, and broadcast functions.

  • UDP is packet-oriented

    The sender's UDP sends the message to the application program, and after adding the header, it is delivered to the IP layer. UDP neither merges nor splits the packets delivered by the application layer, but preserves the boundaries of these packets. Therefore, the application must choose the appropriate size of the message

  • unreliable

    First of all, the unreliability is reflected in the lack of connection. UDP will only throw the data packets you want to send to the other party, and don't care whether the data arrives safely and completely. There is no need to establish a connection for communication, and you can send it whenever you want. Such a situation is definitely unreliable.

    And it will transmit whatever data is received, and it will not back up the data, and it will not care whether the other party has received the data correctly when sending the data.

    Furthermore, the network environment is good and bad, but because UDP has no congestion control, it will always send data at a constant speed. Even if the network condition is bad, the sending rate will not be adjusted. The disadvantage of this implementation is that it may cause packet loss when the network conditions are not good, but the advantage is also obvious. In some scenarios with high real-time requirements (such as conference calls), you need to use UDP instead of TCP.

  • The overhead of the header is small, and it is very efficient when transmitting data packets.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-YMyu3KmE-1689748943729)(images/udp%E5%A4%B4.png)]

​ The UDP header contains the following data:

​ 1. Two sixteen-digit port numbers, which are source port (optional field) and destination port

2. The length of the entire datagram

3. The checksum of the entire datagram (IPv4 optional field), which is used to find errors in header information and data

Therefore, the header overhead of UDP is small, only eight bytes, which is much less than the at least twenty bytes of TCP, and it is very efficient when transmitting data packets.

TCP

The full name of the TCP protocol is Transmission Control Protocol (**Transmission Control Protocol**), which is a connection-oriented, reliable, byte-stream-based transport layer communication protocol, defined by RFC 793 of IETF. TCP is a connection-oriented, reliable stream (uninterrupted data structure) protocol.

For the TCP connection process, see: Describe the process and significance of TCP three-way handshake and four-way wave

It has the following characteristics:

  • connection-oriented

    Connection-oriented means that a connection must be established at both ends before sending data. The method of establishing a connection is "three-way handshake", which can establish a reliable connection. Establishing a connection lays the foundation for reliable data transmission.

  • Only supports unicast transmission

    Each TCP transmission connection can only have two endpoints, and can only perform point-to-point data transmission, and does not support multicast and broadcast transmission methods.

  • stream-oriented

    Unlike UDP, TCP does not transmit individual packets independently, but transmits them in byte streams without retaining packet boundaries.

  • reliable transmission

    For reliable transmission, the judgment of packet loss and bit error depends on the segment number and confirmation number of TCP. In order to ensure the reliability of message transmission, TCP gives each packet a sequence number, and the sequence number also ensures the sequential reception of the packets transmitted to the receiving entity. The receiving entity then sends back a corresponding acknowledgment (ACK) for the successfully received bytes; if the sending entity does not receive an acknowledgment within a reasonable round-trip delay (RTT), then the corresponding data (assumed to be lost) will be retransmitted.

  • Provides congestion control

    When the network is congested, TCP can reduce the rate and amount of data injected into the network to relieve congestion

  • TCP provides full-duplex communication

    TCP allows applications on both sides of the communication to send data at any time, because both ends of the TCP connection have buffers for temporarily storing data for two-way communication. Of course, TCP can send a data segment immediately, or it can buffer for a period of time to send more data segments at a time (the maximum data segment size depends on MSS)

Comparison of TCP and UDP

UDP TCP
Is it connected no connection connection-oriented
Is it reliable Unreliable transmission, no use of flow control and congestion control Reliable transmission, using flow control and congestion control
Number of connection objects Support one-to-one, one-to-many, many-to-one and many-to-many interactive communication Only one-to-one communication
transfer method message-oriented stream-oriented
head overhead The header overhead is small, only 8 bytes The minimum of the header is 20 bytes, and the maximum is 60 bytes
Applicable scene Suitable for real-time applications (IP telephony, video conferencing, live broadcast, etc.) Suitable for applications requiring reliable transfers, such as file transfers

Summarize

  • TCP provides connection-oriented and reliable services to the upper layer, and UDP provides connectionless and unreliable services to the upper layer.
  • Although UDP is not as accurate as TCP transmission, it can also make a difference in many places with high real-time requirements
  • If the data accuracy is high and the speed can be relatively slow, TCP can be selected

at last

This interview question will continue to be updated, please pay attention! ! ! !
Friends who need this interview question can scan the QR code below to get it for free! ! !
At the same time, by scanning the QR code below, you can also enter the group to enjoy the service of the ChatGPT robot! ! !

Guess you like

Origin blog.csdn.net/datian1234/article/details/132111385