Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# - Getting error on Host to IP conversion if I don't have connectivity - Help! :-)

Posted on 2008-11-05
3
Medium Priority
?
353 Views
Last Modified: 2013-12-17
Greetings Experts,

I have the following code:

                IPAddress[] addresslist = Dns.GetHostAddresses("www.google.com");
                foreach (IPAddress theaddress in addresslist)
                {
                    MessageBox.Show(theaddress.ToString());
                }

Which works GREAT.... as long as I actually have network connectivity, but when I don't have network connectivity, as I would suspect, I get an error on the following line:

 IPAddress[] addresslist = Dns.GetHostAddresses("www.google.com");

Which makes sense to me, because the app wouldn't have access to dns without network connectivity...

But.. my question is... how do I handle the error? what I would like to do, in the event of an error would be to:
            theaddress = "1.1.1.1";

So, the Question is, how do I handle the error, and as a result of the error, set theaddress to "1.1.1.1"?

Thanks!
0
Comment
Question by:MiSheps
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
RishadanPort earned 375 total points
ID: 22890515
try{
   IPAddress[] addresslist = Dns.GetHostAddresses("www.google.com");
}
catch(Exception e){
   //set addresslist to whatever you want here
}
0
 

Author Comment

by:MiSheps
ID: 22890835
Nope, didn't work
0
 

Author Comment

by:MiSheps
ID: 22891016
scratch that, I just didn't understand that "try" was part of the code, figured it out though, and your solution works well, with a small bit added.

Good help, thanks for the info!

Here is what I ended up with:

                bool SkipServerIPConvert;
 
                // Convert IP for "Other" Server
                // #############################
                SkipServerIPConvert == false;
                try
                {
                    IPAddress[] addresslist = Dns.GetHostAddresses(MyGlobalVariables.OtherServer);
                }
                catch (Exception e)
                {
                    MyGlobalVariables.OtherServer = "1.1.1.1";
                    SkipServerIPConvert = true;
                }
                if (SkipServerIPConvert == false)
                {
                    IPAddress[] addresslist = Dns.GetHostAddresses(MyGlobalVariables.OtherServer);
                    foreach (IPAddress theaddress in addresslist)
                    {
                        MyGlobalVariables.OtherServer = theaddress.ToString();
                    }
                }
                // #############################

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question