\

What is the difference between TCP and UDP? Why does TCP have three handshakes, not two

As a reliable transmission control protocol, TCP’s core idea is not only to ensure reliable transmission of data, but also to improve the efficiency of transmission, and three times can precisely meet the needs of the above two aspects!

The essence of TCP reliable transmission: The operating system dynamically selects A 32-bit Initial Sequence Number at random for party A of the TCP connection. Assuming that the Initial Sequence Number of PARTY A is 1000, it takes this Sequence Number as the origin and numbers each byte of data to be sent as follows: 1001,1002,1003… , the ISN is its initial serial number, so that B can have A mental preparation about what is legal and what is illegal. For example, the number 900 is illegal. At the same time, B can also confirm each byte of A’s data. If A receives THE confirmation number of 2001 from B, it means that the number of bytes is 1001-2000, and A total of 1000 bytes have arrived safely.

Similarly, B performs A similar operation, assuming that the initial ISN of B is 2000 and that the ISN is the origin, so that A can confirm each byte sent by B. If B receives A confirmation number of 4001, it means that the number of bytes is 2001-4000, and A total of 2000 bytes have arrived safely.

In short, the TCP handshake, what is it?

The serial number of the origin of the data on both sides of the communication!

With this core idea, we analyze the process of two, three, and four handshakes.

A <——-> B

The four handshakes:

1.1 A Sends A synchronization signal SYN + A’s Initial sequence number

1.2 B acknowledges that IT has received the synchronization signal from A, records A’s ISN to the local computer, and names B’s ACK sequence number

1.3 B sends the synchronization signal SYN + B’s Initial sequence number

1.4 A acknowledges that IT has received the synchronization signal from B, records B’s ISN to the local computer, and names A’s ACK sequence number

Obviously, the two steps of 1.2 and 1.3 can be combined, requiring only three handshakes and improving the speed and efficiency of the connection.

The process of a second handshake:

2.1 A sends A synchronization signal SYN + A’s Initial sequence number

2.2 B sends the synchronization signal SYN + B’s Initial sequence number + B’s ACK sequence number

There is A problem. A and B agree on the initial serial number of A, which in this case is 1000. However, B cannot know whether A has received its synchronization signal. If the synchronization signal is lost, A and B cannot agree on the initial serial number of B.

Therefore, TCP designers design the SYN flag bit to occupy A byte number (as does the FIN flag bit). Since it is A byte of data, according to TCP’s principle of confirming the TCP segment with data, A must give B A confirmation to confirm that A has received B’s synchronization signal.

Some children’s shoes will say, what if the confirmation sent by A to B is lost?

Will A retransmit this ACK on timeout? Don’t! TCP does not retransmit an ACK timeout without data. So what are we going to do? If B does not receive an ACK from A, it retransmits its OWN SYN synchronization signal until it receives an ACK from A.

Process:

The first packet, the SYN from A to B, was lost and did not reach B

User A periodically retransmits the data due to timeout until user B’s confirmation is received

The second packet, the SYN +ACK from B to A, is lost and does not reach A

B periodically retransmits the data after the timeout until IT receives the confirmation from A

The third packet, the ACK from A to B, was lost and did not reach B

After sending an ACK, USER A considers TCP to be in the Established state, while user B obviously considers TCP to be in the Active state

A. Assume that no data is sent by both parties. B periodically retransmits data after timeout until it receives the acknowledgement from A. After receiving the acknowledgement, B’s TCP connection is in the Established state and can send packets in both ways.

B. Assuming that A sends Data and B receives Data + ACK from A, b naturally switches to the Established state and accepts Data from A.

C. Assume that B has sent data. If B cannot send the data, THE SYN + ACK will be retransmitted periodically due to timeout until B receives the acknowledgement from A.

What are the IO models in JAVA? What’s the difference?

BIO synchronization blocks IO: Poor reliability, low throughput, used in scenarios where connections are few and fixed. There was only one model before JDK1.4. The programming model is the simplest

NIO synchronization non-blocking I/O: High reliability and throughput. It is applicable to short and numerous connections (light operation). (such as chat room, each person to send a sentence) jdK1.4 support. The programming model is the most complex

AIO asynchronously blocks IO: The reliability is the best and the throughput is high. This method is applicable to multiple and long connections (re-operation). (for example, file server) supported after JDk1.7. The programming model is simple, but the operating system is required for asynchronous notification

Synchronous blocking concept:

What are the core components of JAVA NIO? What do they do?

The Client will write the data to the Buffer, and the write will have a write event, which will register with the Selector via the Channel, notify the Selector, and the Selector will notify the Server to arrange for the idle thread to handle it

A Channel is similar to a stream. Each Channel corresponds to a Buffer Buffer. A Channel is going to register with a Selector.

According to the read and write events that occur on the Channel, the Selector will send the request to an idle thread for processing. The Selector corresponds to one or more threads, and the number of threads is controlled by the Server

Buffers and channels are both readable and writable

What is the difference between select, poll, epoll

These are the three implementation mechanisms for multiplexing in NIO, provided by the Linux operating system

User space and kernel space: To protect system security, the operating system divides the kernel into two parts: user space and kernel space. The user space cannot directly access the underlying hardware devices, but must go through the kernel space.

File Descriptor(FD) : an abstract concept, formally an integer, but actually an index value. A record table pointing to files opened in the kernel for each process maintenance process. When a program opens a file or creates a file, the kernel returns a FD to the process. Usually only on Unix, Linux systems.

Select mechanism: maintains a fD_set of FDS. Multiplexing is done by copying the FD_set from user space to kernel space and activating the socket connection, but the x64 operating system limits the size of the FD_set to 2048

Poll mechanism: This mechanism is similar to the SELECT mechanism, but it optimizes the fD_set structure so that the size of the FD set breaks the operating system limit. Fd_set is an array structure. Pollfd is used in poll instead of fd_set. Pollfd is implemented through a linked list.

EPoll mechanism (Event Poll) : proposed by Linux2.6, it is event-driven. It no longer scans all FDS, but only stores the FD events concerned by the user to an Event table in the kernel. This reduces the amount of data that needs to be copied between user space and kernel space.

Describe the difference between Http and Https