|Written by Mike James|
|Thursday, 22 October 2009|
Page 1 of 4
There are many reasons why you might want to know how much time a server is connected to the Internet. For example, if you have a website you might want to keep an eye on how much downtime it suffers. If you have a remote site connected by a router, say, then it’s worth knowing if it’s all working and what proportion of the time it is operational. It’s not a difficult job to build your own “site scan” application and the components that make it up can also be used for similar tasks.
The advantage of DIY is, of course, that you can customise the code to do exactly what you want. For example, it would be easy to add code to send an email or create some other type of alert should a website become available or unavailable.
SiteScan is a simple C# application that keeps track of when a list of servers are actually contactable via the Internet. Notice that if you can’t contact a server it doesn’t mean that it’s “down”; the problem could be on the path between your machine and the server.
The basic idea that we are going to implement is to send each of the servers in a list a ping message and see if the server responds. What makes the task slightly more difficult is that we need to ping each server at a set interval and we need to collect the data on whether they respond or not. If the server doesn’t respond to a ping, or if you are interested in testing a specific protocol such as HTTP for a web server, then you will have to replace the ping class with custom code that checks to see that the server responds using the protocol of interest.
Initialising the Grid
Start a new Windows Forms application using C# - and as the ping class was only introduced in .NET 2.0 we need at least this version.
Next we need something to hold the list of sites to be scanned. There are a range of possible ways of doing this, Listbox, Textbox etc. but if we are going to use the same component to display the results of the scan then the obvious choice is the DataGridControl, even though it can be more complicated to use.
Place an instance of the DataGridControl on the form and a button labelled “Start Scan”. Customise the DataGridView using its Task Editor which can be accessed by clicking on the small arrow that appears at the top when it is selected. Add three columns – with labels Address, Status and Time. Add column captions that are appropriate as shown:
In a full application you would need to add some simple code to allow the user to save a list of servers to test. In this case it’s simpler to hard code the list as part of an initialisation method. Notice that the user can still add extra servers by typing URLs or IP addresses directly into the grid – but there is no way of saving them for reuse. The initialisation can be included in the form’s constructor:
You can add as many URLs and IP addresses using similar instructions. Notice that while you can select which column of the grid is used via an index, as in Cells for the first column, using the column name makes the code much more readable.
It is fairly obvious that we are going to have to arrange to ping each server at a set interval. There are many ways of implementing this but the simplest is to use a Timer component. Place a Timer on the form and add the following code to the start of the Form definition:
const int interval = 5;
This sets the scanning interval to five minutes which should be sufficient for most situations. Notice that pinging a server too often can constitute an attack on the server and you could get into trouble not to mention degrading the server’s performance. In principle you should get permission to ping a public server on a regular basis but pinging a server once every 5 or ten minutes should have no detectable impact on its performance. To implement the timer interval we need to add to the end of the form’s constructor:
timer1.Enabled = false;
This disables the timer and sets its interval to five minutes – the timer works in milliseconds so we first multiply by 60 to convert to seconds and then 1000 to give milliseconds. While you are testing the program you might well want to decrease the testing interval but keep in mind the comments about over-pinging a server.
The “Start Scan” button simply has to enable the timer but rather than wait for the entire interval before scanning it’s a good idea to call the timer’s event handler directly first:
private void button1_Click(
|Last Updated ( Sunday, 14 March 2010 )|