IPAddress. [Try] Parsing 192.168 - 192.0.0.168

I have the following script:

IPAddress ip; IPAddress.TryParse("192.168", out ip); if(ip == null){//do something with IP} 

I would expect the parsing to fail, instead it is parsed as "192.0.0.168". What am I missing here? (IPAddress.Parse works the same)

+3
c # networking ip-address
Sep 24
source share
3 answers

From the Parse documentation:

The number of parts (each part is divided by period) in ipString determines how the IP address is created. The address of one part is stored directly in the network address. A two-part address, convenient for specifying a Class A address, places the leading part in the first byte and the ending part in three three bytes of the network address. A three-part address, convenient for specifying a class B address, places the first part in the first byte, the second part in the second byte, and the end part in the right-most two bytes of the network address. For example:

 Number of parts and example ipString | IPv4 address for IPAddress 1 -- "65536" | 0.0.255.255 2 -- "20.2" | 20.0.0.2 2 -- "20.65535" | 20.0.255.255 3 -- "128.1.2" | 128.1.0.2 
+7
Sep 24
source share

The documentation, which contains similar examples for you, is pretty clear:

The number of parts (each part is separated by a period) in ipString determines how the IP address will be built. The address of one part is stored directly in the network address. A two-part address, convenient for specifying a Class A address, puts the leading role in the first byte and the end part in the right three bytes of the network address. A three-part address, convenient for specifying class B, places the first part in the first byte, the second part in the second byte, and the final part in the rightmost two bytes of the network address.

 Number of parts and example ipString IPv4 address for IPAddress ==================================================================== 1 -- "65536" 0.0.255.255 2 -- "20.2" 20.0.0.2 2 -- "20.65535" 20.0.255.255 3 -- "128.1.2" 128.1.0.2 
+6
Sep 24
source share

Enter http://192.168 in the address bar of the browser. What's happening?

This is the expected behavior.

+4
Sep 24
source share



All Articles