host ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lloyd Dupont

    #1

    host ...

    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(FormatExc eption)
    {
    IPHostEntry he = Dns.GetHostByNa me( 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]

    #2
    Re: host ...

    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.

    "Lloyd Dupont" <net.galador@ld > wrote in message
    news:OP8sO46hDH A.2984@TK2MSFTN GP11.phx.gbl...[color=blue]
    > 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(FormatExc eption)
    > {
    > IPHostEntry he = Dns.GetHostByNa me( host );
    > ia = he.AddressList[0];
    > }
    >
    >
    > I wonder if there is another way to do that which would avoid the try[/color]
    catch,[color=blue]
    > any tips ?
    >
    >[/color]


    Comment

    • Lloyd Dupont

      #3
      Re: host ...

      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 ...

      "Jim Blizzard [MSFT]" <jimblizz@onlin e.microsoft.com > wrote in message
      news:3f7a1b41$1 @news.microsoft .com...[color=blue]
      > 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[/color]
      use[color=blue]
      > the appropriate method -- Parse() or GetHostByName() . I'd still use the[/color]
      try[color=blue]
      > / 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.
      >
      > "Lloyd Dupont" <net.galador@ld > wrote in message
      > news:OP8sO46hDH A.2984@TK2MSFTN GP11.phx.gbl...[color=green]
      > > 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(FormatExc eption)
      > > {
      > > IPHostEntry he = Dns.GetHostByNa me( host );
      > > ia = he.AddressList[0];
      > > }
      > >
      > >
      > > I wonder if there is another way to do that which would avoid the try[/color]
      > catch,[color=green]
      > > any tips ?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Michael Giagnocavo [MVP]

        #4
        Re: host ...

        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

        "Lloyd Dupont" <net.galador@ld > wrote in message
        news:%23$vZ5b8h DHA.1508@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > well you agree me ;-)
        > I though my code was ok, but I read on many thread recently than one[/color]
        should[color=blue]
        > 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 ...
        >
        > "Jim Blizzard [MSFT]" <jimblizz@onlin e.microsoft.com > wrote in message
        > news:3f7a1b41$1 @news.microsoft .com...[color=green]
        > > Hi Lloyd,
        > >
        > > Thanks for posting to the newsgroup.
        > >
        > > You could potentially run this through a regular expression to see if[/color][/color]
        it's[color=blue][color=green]
        > > in the right format for either a machine name or an IP address and then[/color]
        > use[color=green]
        > > the appropriate method -- Parse() or GetHostByName() . I'd still use the[/color]
        > try[color=green]
        > > / 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.
        > >
        > > "Lloyd Dupont" <net.galador@ld > wrote in message
        > > news:OP8sO46hDH A.2984@TK2MSFTN GP11.phx.gbl...[color=darkred]
        > > > 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(FormatExc eption)
        > > > {
        > > > IPHostEntry he = Dns.GetHostByNa me( host );
        > > > ia = he.AddressList[0];
        > > > }
        > > >
        > > >
        > > > I wonder if there is another way to do that which would avoid the try[/color]
        > > catch,[color=darkred]
        > > > any tips ?
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Juan Gabriel Del Cid

          #5
          Re: host ...

          > I wonder if there is another way to do that which would avoid the try
          catch,[color=blue]
          > any tips ?[/color]

          You could always use the Dns.GetHostByNa me 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


          Comment

          • Lloyd Dupont

            #6
            Re: host ...

            ho, cool ....
            I will use that for my apps.

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

            "Juan Gabriel Del Cid" <jdelcid@atrevi do.nospam.net> wrote in message
            news:ehpSmGAiDH A.2120@TK2MSFTN GP10.phx.gbl...[color=blue][color=green]
            > > I wonder if there is another way to do that which would avoid the try[/color]
            > catch,[color=green]
            > > any tips ?[/color]
            >
            > You could always use the Dns.GetHostByNa me method, it suports both[/color]
            hostname[color=blue]
            > 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" ),[/color]
            it[color=blue]
            > will just parse that and return it as the address.
            >
            > Hope this helps,
            > -JG
            >
            >[/color]


            Comment

            Working...