Byte Interview: What happens when you connect to an IP address that doesn't exist?

2023.02.20

Byte Interview: What happens when you connect to an IP address that doesn't exist?


The client sends a UDP packet whose destination IP address exists but the port does not exist. UDP does not have an RST packet like TCP. What will happen at this time?

Hello everyone, I am Xiaolin.

Share two byte interview questions, which are network questions based on scenarios.

  • Q1: What happens when a client connects to a non-existent IP address?
  • Q2: What happens when a client connects to an existing IP address but the port does not exist?

PS: The "connection" here refers to the TCP connection.

For this kind of scenario problem, if you don't have a systematic network knowledge system and only rely on stereotyped essays, you will suffer old crimes.

In line with the trend of station B: save flow, I will give the conclusion directly

Q1: What happens when a client connects to a non-existent IP address?

This problem should be considered in two situations, and the conclusions obtained in different situations are different.

Case 1: The target IP address and the client's IP address are in the same LAN (same network number).

In the first case, the client cannot send a SYN message, and is mainly stuck at the data link layer.

Because the target address does not have an IP address, when the kernel of the client sends an arp request, it broadcasts to ask who the target IP address belongs to. Since the target IP address does not exist in the network, no device responds to the client's arp request.

Since the client cannot get the MAC of the target device, it cannot assemble the information of the MAC header, so the SYN message cannot be sent out.

Case 2: The target IP address and the client's IP address are not in the same LAN (different network numbers).

In the second case, the client will first send the SYN message to the router, and then the router will continue to forward it.

Since the target IP address does not exist, the SYN message will disappear in the network, so the client will not receive the confirmation message for the SYN message, and then the client will trigger a timeout retransmission, and retransmit the SYN message Text, until the number of retransmissions reaches the maximum number, the client connection will be released.

Some students may be curious, why can the SYN message of the client be sent out in this case?

Because when the target IP address and the client IP address are not in the same LAN, the client judges that the next step is to send the network packet to the router through the judgment of the routing table.

picture

At this time, the arp request at the data link layer will broadcast to ask who the IP address (router IP address) belongs to. The router finds that it is its own IP address, so it will tell the client its own MAC address.

Then the "Destination MAC Address" in the MAC header of the client's network message is filled with the MAC address of the router, so the SYN message can be sent out.

Since the target MAC address belongs to the router, it will be received by the router, and then the router will continue to judge through the routing table and forward it to the next router until the target device is found.

Q2: What happens when a client connects to an existing IP address but the port does not exist?

If the target IP address of the client connection exists, then the SYN packet can arrive at the target device correctly.

After the target device receives the SYN message, it finds that the port number is not monitored by the process. At this time, the kernel of the target device will return a RST message.

After the client receives the RST message, it releases the connection.

 …

So far, the conclusion has been said.

I don’t know if you think there is a lot of information. If you think there is a lot of information, then you should supplement your network knowledge.

The second question is not difficult, but the first question is difficult. If you do not understand how the two computers communicate, then you will not be able to answer it. Therefore, establish a systematic network knowledge and face this problem. For similar scene questions, you can draw inferences about other cases from one instance.

I've been busy recently, lazy, and didn't draw any pictures. If you don't understand something, you can read this article I wrote before: Explore ! The mental journey of a data packet in the network explains in detail how each layer encapsulates the header and how the routing table judges it.

Finally, one more question: the client sends a UDP packet whose destination IP address exists but the port does not exist. UDP does not have an RST packet like TCP. What will happen at this time?

Just sauce, lol!