Android Robot

DnnForge - NewsArticles

14
When I redesigned my home page (www.mcwherter.net) last year, I went through a rough time finding content to put on the master page (I still think it lacks). Because of the lack of content I thought it may be cool so show some statistics of my internal network where www.mcwherter.net runs. When the page loads it will check to see if all my devices on my network up and running. Currently I’m checking 2 servers, a TiVo, a Vonage phone router and my laptop (great way to see if I’m at home or not)
To check the devices, I just ping them with a bit of C# code. The System.Net.NetworkInformation name space contains a ping utility that works great.

    1         private bool ServerUp(string name)

    2         {

    3             Ping ping = new Ping();

    4             bool tmpRtn;

    5 

    6             try

    7             {

    8                 // try to ping the server, time out after 2 seconds

    9                 if (ping.Send(name, 2000).Status == IPStatus.Success)

   10                 {

   11                     tmpRtn = true;

   12                 }

   13                 else

   14                 {

   15                     tmpRtn = false;

   16                 }

   17             }

   18             catch

   19             {

   20                 tmpRtn = false;

   21             }

   22 

   23             // return

   24             return tmpRtn;

   25         }


The System.Net.NetworkInformation namespace does some really cool things. I have found the objects that deal with the network adapters to be fun to play with. I think my favorite thing about the System.Net.NetworkInformation namepase is the ApplicationNetworkAvailabilityChanged delegate. This is really cool, simply put; this will fire when you unplug your CAT5 cable from your network adapter
I created a simple app that shows the Ping and the ApplicationNetworkAvailabilityChanged features as discussed above. You can download the code here NetworkInfoCode

    1 using System;

    2 using System.Collections.Generic;

    3 using System.ComponentModel;

    4 using System.Data;

    5 using System.Drawing;

    6 using System.Text;

    7 using System.Windows.Forms;

    8 using System.Net.NetworkInformation;

    9 

   10 namespace WindowsApplication2

   11 {

   12     public partial class frmMain : Form

   13     {

   14         public frmMain()

   15         {

   16             InitializeComponent();

   17         }

   18 

   19         private void frmMain_Load(object sender, EventArgs e)

   20         {

   21             // wire up the event

   22             NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(ApplicationNetworkAvailabilityChanged);

   23         }

   24 

   25         void ApplicationNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs networkAvailabilityEventArgs)

   26         {

   27             // check to see if the interface is avilable

   28             string adpStatus = NetworkInterface.GetIsNetworkAvailable() ? "Connected" : "Disconnected";

   29             lblAdpStatus.Text = adpStatus;

   30         }

   31 

   32         private void btnPing_Click(object sender, EventArgs e)

   33         {

   34             Ping ping = new Ping();

   35 

   36             int timeout = 120;

   37             PingReply reply = ping.Send("www.msu.edu", timeout);

   38 

   39             lstPingStatus.Items.Clear();

   40             if (reply.Status == IPStatus.Success)

   41             {

   42                 lstPingStatus.Items.Add(string.Format("Address: {0}", reply.Address.ToString()));

   43                 lstPingStatus.Items.Add(string.Format("RoundTrip time: {0}", reply.RoundtripTime));

   44                 lstPingStatus.Items.Add(string.Format("TTL: {0}", reply.Options.Ttl));

   45                 lstPingStatus.Items.Add(string.Format("Buffer Size: {0}", reply.Buffer.Length));

   46                 lstPingStatus.Items.Add(string.Format("Don't fragment: {0}", reply.Options.DontFragment));

   47             }

   48             else

   49             {

   50                 lstPingStatus.Items.Add(string.Format("Could not ping host {0}", reply.Status.ToString()));

   51             }

   52         }

   53     }

   54 }


Click here to download a working sample of the network info namespace.