Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

IPAddress class in Framework 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason - 02 Nov 2006 21:26 GMT
All,

Please note the following:

IPAddress ip = IPAddress.Parse(“10.1.176.10”);

Works fine, as expected, as does:

bool good = IPAddress.TryParse(“10.1.176.10”);

Both return the proper result. HOWEVER…

IPAddress ip = IPAddress.Parse(“10.1.176”);

Also works fine? It creates an IP address object, which when examined became
“10.1.0.176”? WTF?

Note that this also returned true:

bool good = IPAddress.TryParse(“10.1.176”);

Am I simply out of luck until version 3? Is this a known issue? Am I the one
who’s wrong, but just don’t see it? (wouldn't surprise me:-))

For the life of me I can’t understand how TryParse() could return true on an
IP address with only 3 octets?

Please advise….

Thanks,…Jason

Jason Scott Shatzkamer
Director of IT / Ecommerce
Corporate Express
954.379.5415
jason.shatzkamer@cexp.com
Cowboy (Gregory A. Beamer) - 02 Nov 2006 21:41 GMT
Looks like they missed the "never trust user input" argument. :-)

Signature

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************

> All,
>
[quoted text clipped - 35 lines]
> 954.379.5415
> jason.shatzkamer@cexp.com
Turkbear - 02 Nov 2006 22:44 GMT
>All,
>
[quoted text clipped - 32 lines]
>954.379.5415
>jason.shatzkamer@cexp.com

Perhaps the expanation is here:( http://msdn2.microsoft.com/en-gb/library/system.net.ipaddress.parse.aspx )
--------------------------------------------------------
The static Parse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and
in colon-hexadecimal notation for IPv6.

The number of parts (each part is separated by a period) in ipString determines how the IP address is constructed. A one
part address is stored directly in the network address. A two part address, convenient for specifying a class A address,
puts the leading part in the first byte and the trailing part in the right-most three bytes of the network address. A
three part address, convenient for specifying a class B address, puts the first part in the first byte, the second part
in the second byte, and the final 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


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Jason - 02 Nov 2006 23:02 GMT
Nice find...that DOES look a whole lot like my pain :-)

What I do know, however, is that if someone ever called me and I asked them
for their IP address, and they told me "10.1.176", I would say "That is not a
valid IP address."

More so if someone said "65536"...

I guess the real answer, then, is to do the following logic:

string ipString = "10.1.176";
if (IPAddress.TryParse(ipString))
{
   IPAddress ip = IPAddress.Parse(ipString);
   if (ip.ToString() != ipString)
      // Not Valid
   else
      // Valid  
}

That look about right? Seems silly, but works...

Thanks,...J.~

> >All,
> >
[quoted text clipped - 62 lines]
>
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ben Voigt - 03 Nov 2006 15:14 GMT
> Nice find...that DOES look a whole lot like my pain :-)
>
[quoted text clipped - 19 lines]
>
> That look about right? Seems silly, but works...

It'll reject leading zeros which aren't actually invalid.

Maybe IPAddress.TryParse(ipString) && ipString.Split(new Char[] {
'.' }).Length == 4?

> Thanks,...J.~
>
[quoted text clipped - 73 lines]
>>
>> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.