This is the 30th day of my participation in the More Text Challenge. For more details, see more text Challenge

Computer network interview questions collation

Let’s review the installation and use of mongodb we shared last time

  • mongodbThe introduction of
  • mongodbHow to install
  • mongodbHow to Use it simply
  • How to GOmongodb

If you’re still interested in mongodb, check out the article on how to install and use mongodb in GO

Today let’s look at some interview questions

Computer network interview questions

Network byte order:

In big-endian mode, low addresses store high bytes

Local byte order:

Small endian mode, low address store low bytes

Three-way handshake

  • Initiates the connection, sends the SYN flag, and requests to establish the connection. Carries serial number, data byte size (0), sliding window size
  • Passive receiver, sendACKReply, SYN flag. Carry serial number, data byte size (0), confirm serial number, slide window size
  • Initiate the connection and sendACKAnswer, carry confirmation number

Four times to wave

  • Close the connection and send FIN.
  • Passive close end, sendACK. – half closed
  • The FIN is sent to the passive end
  • Actively close the end and sendACKReply – All connections closed

What happens when a TCP third handshake fails

If the ACK packet is lost on the network, the Server resends the SYN+ACK packet after the timeout timer expires

The retransmission times is specified based on /proc/sys/net/ipv4/tcp_synack_retries. The default is five times

If no ACK response is received after the specified number of retransmissions, the Server automatically closes the connection after a certain period of time

However, the Client considers that the connection has been established. If the Client writes data to the Server, the Server responds with an RST packet.

When the failure occurs, the server does not retransmit ACK packets, but directly sends RTS packets, and the server enters the CLOSED state

This is done to prevent SYN flood attacks

Long and short connection differences and advantages and disadvantages

Persistent connection: Connect -> Transfer data -> Maintain connection -> Transfer data ->… . -> Close the connection until one party closes the connection

A persistent connection means that the SOCKET connection is maintained regardless of whether the SOCKET connection is used, but the security is poor.

  • advantages

A long connection saves more TCP setup/shutdown operations, reducing waste and saving time. It is suitable for customers who frequently request resources.

  • disadvantages

With the increasing number of clients, sooner or later the server has hit a day, then need to take some strategies, such as the closure of some links don’t read and write operations for a long time, so that you can avoid some malicious links lead to server backend service is damaged, if conditions allow, can with the client for granularity, limit the maximum number of connections on each customer

Short connection

Connect -> Transfer Data -> Close the connection

For example, HTTP is a stateless short link. Every time the browser and server perform an HTTP operation, a connection is established, but the connection is interrupted when the task is complete.

  • Advantages:

Short connections are simpler for the server, and any connections that exist are useful connections that do not require additional control

  • Disadvantages:

Frequent client connections waste time on TCP setup and shutdown.

The sliding window

To ensure that data is not lost, send the buffer to the peer end.

The return value of the read function in network communication:

  • = 0

Indicates that the peer end has closed the connection

  • = 1

judgeerrnoIn the case

  • Errno == EAGAIN | EWOULDBLOCK

Set the non-blocking mode to read before the data has arrived

  • errno == EINTR

Interrupted by signal

  • Errno == Other cases

abnormal

Viewing the Port number

netstat -antp

Port Overcommitment

int opt = 1; 
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(void *)&opt,sizeof(opt));
Copy the code

Half off (FIN_WAIT_2)

It takes four handshakes to terminate a connection

This is caused by TCP’s half-close

CLOSE_WAITStatus of the problem situation

The parent process opens the socket and then uses the child to handle the business. The parent process continues to listen for network requests and never terminates

When the client sends the FIN to the client, the read of the child process returns 0. When the child process finds that the peer end has been closed, it directly calls close() to close the local end

In fact, simply subtracting the socket reference count by 1 does not close the socket. This will result in an additional socket for CLOSE_WAIT…

How can I avoid this?

The child process closing process should look like this:

shutdown(sockfd, SHUT_RDWR);

close(sockfd);

In this case, the server FIN is sent and the socket enters the LAST_ACK state. The socket waits for the final ACK to arrive and then enters the initial state CLOSED

Description of shutdown()

In Linux, the shutdown system call is used to control the closing mode of the socket

int shutdown(int sockfd,int how);
Copy the code

The How parameter allows you to select one of the following methods for shutdown:

  • SHUT_RD

Close the read end of the connection. That is, the socket will no longer accept data, and any data currently in the socket’s accept buffer will be discarded. The process will not be able to issue any read operations on the socket. Any data received on the TCP socket after this call is acknowledged and then discarded.

  • SHUT_WR

Close the write end of the connection.

  • SHUT_RDWR

This is equivalent to calling shutdown twice: first SHUT_RD, then SHUT_WR

Note:

If a process is shutdown(SFD, SHUT_RDWR), other processes cannot communicate with each other. If a process is closed (SFD), other processes will not be affected

2MSLThe length TIME_WAIT

It must appear at the end of the active close to ensure that the last ACK can be received by the peer end.

1.TTLWhat is?

TTL is short for Time To Live

This field specifies the maximum number of network segments that IP packets are allowed to pass through before being discarded by the router **.

TTL is an 8 bit field in the IPv4 header.

2. Functions of TTL

The PURPOSE of TTL is to limit the duration of IP packets in the computer network.

The maximum value of TTL is 255, and a recommended value of TTL is 64

3.TTLThe principle of

Although TTL is literally translated as the time to live, in fact TTL is the maximum number of hops that an IP packet can be forwarded on a computer network.

The TTL field is set by the sender of the IP packet along the entire forwarding path of the IP packet from the source to the destination

Each time it passes through a router, the router changes the TTL field value by subtracting 1 from the TTL value before forwarding the IP packet.

If the TTL is reduced to 0 before the IP packet reaches the destination IP address, the router dismisses the received IP packet with THE TTL=0 and sends an ICMP timeout packet to the sender of the IP packet.

Advantages and disadvantages of C/S model and B/S model

C/s model

Advantages:

The biggest advantage of C/S is that it can realize complex application structure, high security and fast data transmission speed.

  • Simple structure.
  • Support for distributed and concurrent environments. Effectively improve resource utilization and sharing.
  • Centralized server resource management facilitates permission control and system security.
  • Good scalability. Both the client and the server can be upgraded separately

Disadvantages:

  • Not easy to deploy (clients installed one by one, pick platforms)
  • Difficult maintenance (clients need to pay attention to updates)
  • Large development workload

The working process of the

  • Open a communication channel that tells the host on which the server process is running that it will accept customer requests on a certain port
  • Wait for the customer’s request to arrive at the port
  • The server receives the service request, processes the request, and sends a reply
  • Go back to step 2 and wait for and process another customer request
  • Shut down the server

b/smodel

Advantages:

B/S The biggest advantage is that it can be used anywhere without installing any special software, as long as there is a computer with Internet access

Zero client installation and maintenance. The system is very easy to scale.

Distributed, easy to expand, sharing strong

Compared with the advantages of traditional C/S:

  • 1. Easy deployment (each platform has its own universal browser)
  • 2. Easy to maintain (the server side changes the content of the web page can be synchronized by all users)
  • 3. The page is dynamically refreshed, and the response speed is significantly reduced.
  • Small development workload

Disadvantages:

  • Cannot cache large amounts of data

The working process of the

  • The user uses the browser toWebServer proposedHTTPThe request.
  • WebThe server calls up the corresponding file according to the browser request. After the corresponding file is not processed or interpreted, the server will be a pure clientHTMLThe code results are returned to the browser.
  • The browser receivesWebThe content of the page sent back by the server (pureHTMLCode) is displayed to the user.

The ping process andICMPagreement

Process example

  • Computer A (192.168.2.135) sends A ping request to ping 192.168.2.179
  • A Computer broadcast initiatesARPTo query the MAC address of 192.168.2.179.
  • B Computer replyARPRequest, send A one-way response to computer A, telling computer A that its MAC address is90:A4:DE:C2:DF:FE
  • After knowing the MAC address, the actual ping request begins. Since computer B knows the source MAC address based on the request sent by computer A, computer B can respond based on the source MAC address.

The ping command relies on the ICMP protocol. The ICMP protocol exists to forward IP datagrams more efficiently and improve the chance of successful delivery.

In addition to ICMP, the ping command also relies on ARP, which can find out the MAC address of a computer based on the IP address.

ARP is cached. To ensure the accuracy of ARP, the computer updates the ARP cache.

ICMP

ICMP is the Internet Control Message Protocol (ICMP).

ICMP is a connectionless protocol. It is a subprotocol of the TCP/IP protocol family. It is used to transmit control messages between IP hosts and routers. ICMP is a network layer protocol.

The main functions are:

  • confirmIPWhether the packet successfully reached the destination address
  • Notification is in the process of being sentIPReason for the packet to be discarded

conclusion

  • Three handshakes and four waves of the computer network
  • What happens when a TCP third handshake fails
  • Advantages and disadvantages of long and short connections
  • After the slide and
  • Network communicationread function
  • What is semi-closed
  • C/S model and B/S model
  • ICMP

My friends, writing is not easy

Your support and encouragement are the motivation for me to insist on sharing and improve the quality

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am nezha, welcome to like the collection, see you next time ~