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 / .NET SDK / October 2003

Tip: Looking for answers? Try searching our database.

host ...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lloyd Dupont - 01 Oct 2003 00:53 GMT
In my App the user could manually enter some host computer location
for this he has the choice to enter either the IP or host name.
I get the IP address internally with the following code sample:
IPAddress ia;
try
{
   ia = IPAddress.Parse(host);
}
catch(FormatException)
{
   IPHostEntry he = Dns.GetHostByName( host );
   ia = he.AddressList[0];
}

I wonder if there is another way to do that which would avoid the try catch,
any tips ?
Jim Blizzard [MSFT] - 01 Oct 2003 01:09 GMT
Hi Lloyd,

Thanks for posting to the newsgroup.

You could potentially run this through a regular expression to see if it's
in the right format for either a machine name or an IP address and then use
the appropriate method -- Parse() or GetHostByName().  I'd still use the try
/ catch block in case there are any problems you miss.

Hope this helps,
bliz

--
Jim Blizzard, MCSD .NET
Community Developer Evangelist | http://www.microsoft.com/communities
Microsoft

Your Potential. Our Passion.

This posting is provided as is, without warranty, and confers no rights.

> In my App the user could manually enter some host computer location
> for this he has the choice to enter either the IP or host name.
[quoted text clipped - 12 lines]
> I wonder if there is another way to do that which would avoid the try catch,
> any tips ?
Lloyd Dupont - 01 Oct 2003 03:52 GMT
well you agree me ;-)
I though my code was ok, but I read on many thread recently than one should
prefer erro code to try catch as a program logic as it has really bad
performance impact.

anyway this parsing is done but a few times, so that should be ok ...

> Hi Lloyd,
>
[quoted text clipped - 34 lines]
> catch,
> > any tips ?
Michael Giagnocavo [MVP] - 01 Oct 2003 04:26 GMT
You are right about that.  I hope MS provides a non-exception way to do
Parsing sometime... :).  If you're worried about performance, then running
some basic tests can help prevent some exceptions being thrown.  If it's not
going to happen often, then don't even worry about it.
-mike
MVP

> well you agree me ;-)
> I though my code was ok, but I read on many thread recently than one should
[quoted text clipped - 43 lines]
> > catch,
> > > any tips ?
Juan Gabriel Del Cid - 01 Oct 2003 10:51 GMT
> I wonder if there is another way to do that which would avoid the try catch,
> any tips ?

You could always use the Dns.GetHostByName method, it suports both hostname
and IP address. If you pass it a name (e.g. "www.google.com"), it will
resolve the address. If you pass it an IP address (e.g. "216.239.41.99"), it
will just parse that and return it as the address.

Hope this helps,
-JG
Lloyd Dupont - 02 Oct 2003 03:59 GMT
ho, cool ....
I will use that for my apps.

anyway on pocket pc I had some bad experience with Dns.GetHostByName()

> > I wonder if there is another way to do that which would avoid the try
> catch,
[quoted text clipped - 7 lines]
> Hope this helps,
> -JG

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.