DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Kyrathasoft Internet Connection Test Demo  (Read 20,400 times)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Kyrathasoft Internet Connection Test Demo
« on: February 04, 2012, 03:19 PM »
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.

[attach]
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

« Last Edit: February 05, 2012, 02:33 PM by kyrathaba »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Kyrathasoft Internet Connection Test Demo
« Reply #1 on: February 05, 2012, 03:27 PM »
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.
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Kyrathasoft Internet Connection Test Demo
« Reply #2 on: February 05, 2012, 10:47 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:

Code: (csharp)
            if (!kyr.networkIsUp()) {
                MessageBox.Show("You currently are not connected to the Internet.", "Cannot check for update");
                return;
            }

            string msg = string.Empty;
            string pathLastSuccessfullyCheckedForUpdate = Path.Combine(specFldrAppData, "lastUpdateCheck.txt");           

            if (System.IO.File.Exists(Path.Combine(specFldrAppData + "updateURL.txt"))) {
                try {
                    updateToolStripMenuItem.Enabled = false;
                    Cursor = Cursors.WaitCursor;
                    this.Text = "Checking for update...";
                    string url = string.Empty;
                    using (StreamReader sr = new StreamReader(Path.Combine(specFldrAppData + "updateURL.txt"))) {
                        url = sr.ReadLine();                        
                    }
                    
                    WebClient client = new WebClient();
                    try {
                        client.DownloadFile(url, specFldrAppData + "temp.txt");
                    } catch (Exception exFailedToObtain) {
                        MessageBox.Show("Unable to obtain update info from website at this time. Please try again later." +
                        Environment.NewLine + Environment.NewLine + exFailedToObtain.Message, "Try Again Later");
                        updateToolStripMenuItem.Enabled = true;
                        Cursor = Cursors.Default;
                        this.Text = appTitle;
                        return;
                    }
                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                    string downloadURL = string.Empty;
                    string[] parts;                    
                    using (StreamReader sr = new StreamReader(Path.Combine(specFldrAppData, "temp.txt"))) {
                        parts = sr.ReadLine().Split('|');
                        string newest = parts[1].Trim();                        
                        parts = sr.ReadLine().Split('|');
                        downloadURL = parts[1].Trim();
                        pathToOnlineZippedInstaller = downloadURL;
                        parts = sr.ReadLine().Split('|');
                        expectedDownloadSize = Convert.ToInt32(parts[1]);
                        if (String.Compare(Application.ProductVersion.ToString(), newest) < 0) {

                            if (blnLoadedPrayerHasBeenModified) {
                                msg = "A loaded prayer has been modified. Discard it and download the available installer for an updated version of this application?";
                                if (DialogResult.No == MessageBox.Show(msg, "Discard modified prayer and download update?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) {
                                    return;
                                }
                            } else {
                                clearMyPrayerFields();
                            }

                            if (blnWorkingOnNewPrayer) {
                                msg = "You're currently working on a new prayer. Discard it and download the available installer for an updated version of this application?";
                                if (DialogResult.No == MessageBox.Show(msg, "Discard modified prayer and download update?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) {
                                    return;
                                }
                            } else {
                                clearMyPrayerFields();
                            }

                            versionDownloading = "v" + newest;
                            this.Text = "Downloading newest version " + versionDownloading;
                            downloadProgress.Value = 0;                                                                                   

                            downloadFullpath = Path.Combine(pathToDesktop, Path.GetFileName(downloadURL));
                            tmrDownload.Enabled = true;
                            presentMyPrayersPanel();
                            myPrayersInterfaceToolStripMenuItem.Checked = true;
                            tmrDownload.Start();
                            _downloadUpdateBegins = DateTime.Now;
                            blnDownloadingUpdateInstaller = true;
                            client.DownloadFileAsync(new Uri(downloadURL), downloadFullpath);
                        } else {                            
                            using (StreamWriter sw = new StreamWriter(pathLastSuccessfullyCheckedForUpdate)) {
                                sw.WriteLine(DateTime.Now.ToString());
                            }
                            this.Text = appTitle;
                            if (myPrayersInterfaceToolStripMenuItem.Checked) {
                                this.Text += " (\"My Prayers\" interface) ";
                            }
                            MessageBox.Show("You are using the most recent version of the program: " +
                                Application.ProductVersion.ToString(), "No update is currently available");
                            this.Text = appTitle;
                        }
                    }
                } catch (Exception exUpdate) {                                        
                    updateToolStripMenuItem.Enabled = true;
                    blnDownloadingUpdateInstaller = false;
                    Cursor = Cursors.Default;
                    string errMsg = exUpdate.Message;
                    if ((exUpdate.Message.Contains("outside the bounds")) || (exUpdate.Message.Contains("remote name could not be resolved"))) {
                        errMsg = "\"Prayer Minder\" was unable to connect to the download server to check for an available update at this time.";
                    }
                    MessageBox.Show(errMsg, "Error while seeking available program update");
                }
                finally {

                }
            } else {
                MessageBox.Show("\"Prayer Minder\" couldn't retrieve the URL to look for updates. Please contact the programmer.", "Failed to obtain update info");                
            }

            updateToolStripMenuItem.Enabled = true;
            Cursor = Cursors.Default;

        }
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Kyrathasoft Internet Connection Test Demo
« Reply #3 on: February 06, 2012, 07:32 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?:
Code: (csharp)
MessageBox.Show("You currently are not connected to the Internet.", "Cannot check for update");
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Kyrathasoft Internet Connection Test Demo
« Reply #4 on: February 07, 2012, 01:11 PM »
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?
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Kyrathasoft Internet Connection Test Demo
« Reply #5 on: February 07, 2012, 01:59 PM »
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. :)
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,888
    • Participated in NANY Challenge Six Times
    • Coding Snacks
    • Gathering Badge
    • Cody Award
    • Volunteers/Staff
Re: Kyrathasoft Internet Connection Test Demo
« Reply #6 on: February 07, 2012, 02:22 PM »
This is what I use Karen's Net Monitor for.

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Kyrathasoft Internet Connection Test Demo
« Reply #7 on: February 07, 2012, 06:51 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).
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Original address: https://www.donationcoder.com/forum/index.php?topic=29819.0