I have been writing code for 7 years, and this is the first time I have seen such a nasty little bug!

The following article comes from programmer Yupi, author Yupi

Nie Qi

Bugs happen every year, but this year there are especially many. Some time ago I shared with you a  particularly annoying little bug , but in the past two days, I, the unlucky guy, encountered a particularly outrageous bug. How outrageous is it? You can watch the video: https://www.bilibili.com/video/BV1Fc411y7HS

picture

Official flow saving: My back-end Spring Boot project is started on port 8101. After startup, I can access  localhost:8101/api/v3/api-docs the JSON data of the interface document. My front-end needs this data to generate the request code.

picture

As a result, the generation keeps getting errors. After some investigation, I found that the front end did not obtain the JSON data correctly, but instead obtained a hello world string.

picture

This moment made me confused. Where did hello world come from?

picture

So I opened the browser and accessed the address: 127.0.0.1:8101/api/v3/api-docs , and the result was hello world!

picture

Strange, my back-end program does not output hello world code. So I used the command to check which programs occupied port 8101:

picture

Oh, it turns out that a program called aDrive also occupies port 8101!

This is a well-known network disk software. As long as you run it on a MAC computer, you can see hello world by accessing the local port 8101.

Seeing this really made me dumbfounded. I didn't expect that the port number I used when doing my own project actually conflicted with other programs.

By the way, why do you need to occupy this port to output hello world? Did you forget to delete the test code?

picture

Okay, this is the introduction to bugs, let’s talk about some little knowledge.

tips

In the perception of many students:

1) The same port can only be occupied by one program.

In other words: Only one Tomcat can be started on the same port, otherwise there will be a conflict.

2) The responses obtained when accessing localhost and accessing 127.0.0.1 are consistent.

am I right?

It is normal to have these understandings. Many big guys who have worked for many years will think so if they have never experienced such a bug.

But in fact, these two perceptions are wrong.

1. Can the same port be occupied by only one program?

This sentence is missing an important qualification. Different network protocols can share the same port.

For example, what I encountered this time is that aDrive and my back-end program use different network protocols, which are IPv4 and IPv6 respectively, so there is no conflict.

picture

Similarly, TCP and UDP, as two different transport layer protocols, can also use the same port number.

2. localhost is not equal to 127.0.0.1

First of all, localhost  and  127.0.0.1  are completely two different concepts!

localhost is  the hostname  , which is usually used to represent the local computer and is resolved to the address 127.0.0.1 by default.

But as long as you modify the hosts file (hosts), you can resolve localhost to other addresses, similar to domain name resolution.

And 127.0.0.1 is a special  IP address of IPv4 , which points directly to the local loopback interface and is used for local network communication.

In order to be compatible with IPv4 and IPv6, when we access localhost, the operating system will first try to resolve the IPv6 loopback address  ::1 . If the system does not support IPv6, or cannot be resolved to an IPv6 address, it will continue to be resolved to the IPv4 loopback address 127.0.0.1.

When accessing 127.0.0.1 directly, the system will resolve to the IPv4 loopback interface by default. This leads to the fact that when entering these two addresses, the services used and the content accessed are different.
 



Ok, even with such a small bug, I can learn a lot of knowledge. I hope it is useful. Please give it a thumbs up if you learn it~

Note: In my opinion, I often encounter small scratching bugs, especially the small problems mentioned in this article, which will drag down a lot of time in development. Therefore, these problems can only be understood through experience. For novices, , indeed a difficult problem.

In the future, I will also update small bugs frequently to let everyone know that whether it is a work problem or a problem with my own project, it needs to be shown to everyone so that it can be avoided.

picture

Guess you like

Origin blog.csdn.net/we2006mo/article/details/135239293