Welcome Guest.   Make a donation to an author on the site September 19, 2014, 12:57:07 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
The N.A.N.Y. Challenge 2011! Download 30+ custom programs!
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Kyrathasoft Internet Connection Test Demo  (Read 3489 times)
kyrathaba
N.A.N.Y. Organizer
Moderator
*****
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« on: February 04, 2012, 09:19:00 AM »

You can download the entire C# project as a 41 Kb zipped archive. The project is quite brief, and demonstrates testing for the presence of an active (i.e., "up") internet connection.



Download link
« Last Edit: February 05, 2012, 08:33:43 AM by kyrathaba » Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

Ath
Supporting Member
**
Posts: 2,212



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #1 on: February 05, 2012, 09:27:06 AM »

Hm, looking at that code, I'm not convinced that it would really check if I have a working internet connection.
It just shows if I have a valid network connection, but doesn't reach out to an actual internet site to see if it can read data from there. And that's the only reliable way to check if the i-net is working.
I'm on a local network, and internet is handled by a Proxy server.
Logged

kyrathaba
N.A.N.Y. Organizer
Moderator
*****
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #2 on: February 05, 2012, 04:47:30 PM »

Good point. I suppose what we could say is that if this function fails, you definitely do not have an internet connection. If it passes you might have a connection.  Having experimented with this quite a bit, I'll share the complete method I have that gets invoked when one of my program attempts to discover if an update is available. This works reliably for me:

Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
  1.            if (!kyr.networkIsUp()) {
  2.                MessageBox.Show("You currently are not connected to the Internet.", "Cannot check for update");
  3.                return;
  4.            }
  5.  
  6.            string msg = string.Empty;
  7.            string pathLastSuccessfullyCheckedForUpdate = Path.Combine(specFldrAppData, "lastUpdateCheck.txt");          
  8.  
  9.            if (System.IO.File.Exists(Path.Combine(specFldrAppData + "updateURL.txt"))) {
  10.                try {
  11.                    updateToolStripMenuItem.Enabled = false;
  12.                    Cursor = Cursors.WaitCursor;
  13.                    this.Text = "Checking for update...";
  14.                    string url = string.Empty;
  15.                    using (StreamReader sr = new StreamReader(Path.Combine(specFldrAppData + "updateURL.txt"))) {
  16.                        url = sr.ReadLine();                        
  17.                    }
  18.  
  19.                    WebClient client = new WebClient();
  20.                    try {
  21.                        client.DownloadFile(url, specFldrAppData + "temp.txt");
  22.                    } catch (Exception exFailedToObtain) {
  23.                        MessageBox.Show("Unable to obtain update info from website at this time. Please try again later." +
  24.                        Environment.NewLine + Environment.NewLine + exFailedToObtain.Message, "Try Again Later");
  25.                        updateToolStripMenuItem.Enabled = true;
  26.                        Cursor = Cursors.Default;
  27.                        this.Text = appTitle;
  28.                        return;
  29.                    }
  30.                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  31.                    string downloadURL = string.Empty;
  32.                    string[] parts;                    
  33.                    using (StreamReader sr = new StreamReader(Path.Combine(specFldrAppData, "temp.txt"))) {
  34.                        parts = sr.ReadLine().Split('|');
  35.                        string newest = parts[1].Trim();                        
  36.                        parts = sr.ReadLine().Split('|');
  37.                        downloadURL = parts[1].Trim();
  38.                        pathToOnlineZippedInstaller = downloadURL;
  39.                        parts = sr.ReadLine().Split('|');
  40.                        expectedDownloadSize = Convert.ToInt32(parts[1]);
  41.                        if (String.Compare(Application.ProductVersion.ToString(), newest) < 0) {
  42.  
  43.                            if (blnLoadedPrayerHasBeenModified) {
  44.                                msg = "A loaded prayer has been modified. Discard it and download the available installer for an updated version of this application?";
  45.                                if (DialogResult.No == MessageBox.Show(msg, "Discard modified prayer and download update?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) {
  46.                                    return;
  47.                                }
  48.                            } else {
  49.                                clearMyPrayerFields();
  50.                            }
  51.  
  52.                            if (blnWorkingOnNewPrayer) {
  53.                                msg = "You're currently working on a new prayer. Discard it and download the available installer for an updated version of this application?";
  54.                                if (DialogResult.No == MessageBox.Show(msg, "Discard modified prayer and download update?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) {
  55.                                    return;
  56.                                }
  57.                            } else {
  58.                                clearMyPrayerFields();
  59.                            }
  60.  
  61.                            versionDownloading = "v" + newest;
  62.                            this.Text = "Downloading newest version " + versionDownloading;
  63.                            downloadProgress.Value = 0;                                                                                  
  64.  
  65.                            downloadFullpath = Path.Combine(pathToDesktop, Path.GetFileName(downloadURL));
  66.                            tmrDownload.Enabled = true;
  67.                            presentMyPrayersPanel();
  68.                            myPrayersInterfaceToolStripMenuItem.Checked = true;
  69.                            tmrDownload.Start();
  70.                            _downloadUpdateBegins = DateTime.Now;
  71.                            blnDownloadingUpdateInstaller = true;
  72.                            client.DownloadFileAsync(new Uri(downloadURL), downloadFullpath);
  73.                        } else {                            
  74.                            using (StreamWriter sw = new StreamWriter(pathLastSuccessfullyCheckedForUpdate)) {
  75.                                sw.WriteLine(DateTime.Now.ToString());
  76.                            }
  77.                            this.Text = appTitle;
  78.                            if (myPrayersInterfaceToolStripMenuItem.Checked) {
  79.                                this.Text += " (\"My Prayers\" interface) ";
  80.                            }
  81.                            MessageBox.Show("You are using the most recent version of the program: " +
  82.                                Application.ProductVersion.ToString(), "No update is currently available");
  83.                            this.Text = appTitle;
  84.                        }
  85.                    }
  86.                } catch (Exception exUpdate) {                                        
  87.                    updateToolStripMenuItem.Enabled = true;
  88.                    blnDownloadingUpdateInstaller = false;
  89.                    Cursor = Cursors.Default;
  90.                    string errMsg = exUpdate.Message;
  91.                    if ((exUpdate.Message.Contains("outside the bounds")) || (exUpdate.Message.Contains("remote name could not be resolved"))) {
  92.                        errMsg = "\"Prayer Minder\" was unable to connect to the download server to check for an available update at this time.";
  93.                    }
  94.                    MessageBox.Show(errMsg, "Error while seeking available program update");
  95.                }
  96.                finally {
  97.  
  98.                }
  99.            } else {
  100.                MessageBox.Show("\"Prayer Minder\" couldn't retrieve the URL to look for updates. Please contact the programmer.", "Failed to obtain update info");                
  101.            }
  102.  
  103.            updateToolStripMenuItem.Enabled = true;
  104.            Cursor = Cursors.Default;
  105.  
  106.        }
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

Ath
Supporting Member
**
Posts: 2,212



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #3 on: February 06, 2012, 01:32:21 AM »

Hm, that should work. Just try to read the file, and be prepared to catch any exception you get thrown.

Guess you never actually saw this error?:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
  1. MessageBox.Show("You currently are not connected to the Internet.", "Cannot check for update");
Logged

kyrathaba
N.A.N.Y. Organizer
Moderator
*****
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #4 on: February 07, 2012, 07:11:53 AM »

Quote
Guess you never actually saw this error?:

Formatted for C# with the GeSHI Syntax Highlighter [copy or print]

    MessageBox.Show("You currently are not connected to the Internet.", "Cannot check for update");


So far, I've only seen that when my wireless adapter was unplugged, or I deliberately turned off the router. But it should work if there's ever a temporary ISP service outage, shouldn't it?
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

Ath
Supporting Member
**
Posts: 2,212



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #5 on: February 07, 2012, 07:59:03 AM »

So far, I've only seen that when my wireless adapter was unplugged, or I deliberately turned off the router. But it should work if there's ever a temporary ISP service outage, shouldn't it?
It's in fact only a signal that the (local) network for that computer isn't working, but that's the part that is 'always' working, when behind a proxy. Just a loose cable/adapter would trigger it. smiley
Logged

app103
That scary taskbar girl
Global Moderator
*****
Posts: 5,209



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #6 on: February 07, 2012, 08:22:22 AM »

This is what I use Karen's Net Monitor for.
Logged

kyrathaba
N.A.N.Y. Organizer
Moderator
*****
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #7 on: February 07, 2012, 12:51:14 PM »

Good link, app!

Yeah, my own program's try to download a small text file from a particular DC URL. If successful, then obviously both (a) my local network access if functional and (b) DC is up (or at least that particular DM subdomain is available).
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.089s | Server load: 0.08 ]