Is 0.0.0.0 a valid IP address?

后端 未结 10 1235
遇见更好的自我
遇见更好的自我 2020-11-29 04:14

Is 0.0.0.0 a valid IP address? I want my program to be able to store it as an indication that no address is in use, but this won\'t work if it\'s actually valid.

10条回答
  •  暖寄归人
    2020-11-29 04:50

    Lets look at the Question being asked here by the OP.

    Is 0.0.0.0 a valid IP address?

    Yes. This is technically a valid IP address, and the other answers describe many various uses of it (I am not going to repost wikipedia links here ... or maybe I am).

    As such I believe paxdiablo's answer above is the most correct, but lets look at the context of your question as well.

    I want my program to be able to store it as an indication that no address is in use, but this won't work if it's actually valid.

    This entirely depends on your use case. Given that this is a programmers forum, lets consider that perspective.

    If your software is storing actual internet addresses - server locations, visitors to your website, replication/mirror or backup sites, web service or database servers etc. - then this will be perfectly valid. No machine on the internet will ever have this address assigned, nor will it ever resolve to a valid connection.

    If on the other hand you are writing firewall or router firmware, then this address does take on special meaning; default route, accept any IP source/destination, block all IP source/destination, fall-trough catch-all, etc. as outlined by everyone else. However, let me point out that if you are coding on this level you should have a good enough understanding of network protocols so as to not need to ask this question in the first place.

    I am therefore going to assume that most people viewing this question fall into the first category, and suggest that this is a perfectly valid way of storing a null, empty or missing IP address, if there is some reason that an actual null value cannot be used. Even if you neglect validation checking and your software does try to connect to this IP address, it will simply not be able to make a connection.

提交回复
热议问题