C# Ping

I was recently tasked with putting together a ping and traceroute feature in an application and like I always do I googled to see if someone had already worked out how to do this.

I found a few people who had posted ICMP libraries and so I used one of them and tested and everything seemed to work properly. However I was getting reports that it was not working on Vista. However the person could use the built in ping and tracert utilities with no issue.

At first I thought it might be related to permissions or firewall but none of those ended up resolving the issue.

So I fired up wireshark and looked at what was different at the packet level between what I was doing and what the built in windows ping was doing.

The code I was using was returning an incorrect checksum on the ICMP reply. So I searched a bit to learn about the checksum and ended up finding somehow that as of .NET 2.0 framework there is a Ping class built into the framework!!

So I am posting this in hopes of saving someone a bunch of time with the same issue I had. Also the code I had to put together initially was much more complex. This I think you will see is very straight forward.

Download PingUtility.cs (3.14 kb)

Example Output:

Pinging with 32 bytes of data:

Reply from bytes=32 time=11ms TTL=128
Reply from bytes=32 time=3ms TTL=128
Reply from bytes=32 time=3ms TTL=128
Reply from bytes=32 time=3ms TTL=128

Ping statistics for
	Packets: Sent = 4, Received = 4, Lost = 0
Approximate round trip times in milli-seconds:
	Minimum = 3ms, Maximum = 11ms
public static string Ping()
    using (Ping pingSender = new Ping())
        PingOptions pingOptions = null;
        StringBuilder pingResults = null;
        PingReply pingReply = null;
        IPAddress ipAddress = null;
        int numberOfPings = 4;
        int pingTimeout = 1000;
        int byteSize = 32;
        byte[] buffer = new byte[byteSize];
        int sentPings = 0;
        int receivedPings = 0;
        int lostPings = 0;
        long minPingResponse = 0;
        long maxPingResponse = 0;
        string ipAddressString = "";
        pingOptions = new PingOptions();
        //pingOptions.DontFragment = true;
        //pingOptions.Ttl = 128;
        ipAddress = IPAddress.Parse(ipAddressString);
        pingResults = new StringBuilder();
        pingResults.AppendLine(string.Format("Pinging {0} with {1} bytes of data:", ipAddress, byteSize));
        for (int i = 0; i < numberOfPings; i++)
            pingReply = pingSender.Send(ipAddress, pingTimeout, buffer, pingOptions);
            if (pingReply.Status == IPStatus.Success)
                pingResults.AppendLine(string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}", ipAddress, byteSize, pingReply.RoundtripTime, pingReply.Options.Ttl));
                if (minPingResponse == 0)
                    minPingResponse = pingReply.RoundtripTime;
                    maxPingResponse = minPingResponse;
                else if (pingReply.RoundtripTime < minPingResponse)
                    minPingResponse = pingReply.RoundtripTime;
                else if (pingReply.RoundtripTime > maxPingResponse)
                    maxPingResponse = pingReply.RoundtripTime;
        pingResults.AppendLine(string.Format("Ping statistics for {0}:", ipAddress));
        pingResults.AppendLine(string.Format("\tPackets: Sent = {0}, Received = {1}, Lost = {2}", sentPings, receivedPings, lostPings));
        pingResults.AppendLine("Approximate round trip times in milli-seconds:");
        pingResults.AppendLine(string.Format("\tMinimum = {0}ms, Maximum = {1}ms", minPingResponse, maxPingResponse));
        return pingResults.ToString();

BlueQuartz not generating usage information for sites

For those that might be having the same problem here is what I found.

Going to Usage Information and clicking to generate web, mail or ftp stats resulted in no stats available for period. I looked for a recent site created and tried and stats worked. When I went and looked at what the difference was between the site that worked and the one did not I found there was a permission difference on the logs folder.

Site that worked
drwxr-s---  3 nobody site26  4096 Nov 13 03:28 logs

Site that did not work
drwxr-s--x   4 SITE66-logs site66  4096 Dec 11 12:30 logs

To fix this I issued this command to reset on all sites the permission on the logs folder

find /home -type d -name 'logs' -mindepth 4 -print | xargs chmod 02751

Now all my sites are able to generate stats again.


ASP.NET - Ajax - Expression - LINQ - SQL Server - Webcasts

Found some great sites that have links to all kinds of webcasts covering ASP.NET, Ajax, Expression, LINQ, SQL Server and more. If you want to learn about any of these topics the sites listed below are a great place to start.

ASP.NET Website - Great website with lots of resources but found this quick link to all of the webcasts that have been done both past and present.

DNRTv - A must use website. I love the quality of the webcasts done on this site - http://www.dnrtv.com/ 

They also have a podcast .NET Rocks I listen to every week.


.NET 2.0 SP2 Installation - Were to find it

I have seen many people referring to .NET 2.0 SP2 to fix some of the issues I have been dealing with. I finally figured out that the SP2 for the 2.0 framework is not available as of now anyways as a individual download but rather you must download .NET 3.5 SP1 to get the installation.

So I downloaded 3.5 SP1 and sure enough in my Add/Remove program list now show installed 2.0 SP2.

I also found a link for a bootstrap that you can download and get SP2 by itself but have not tried this myself.

UPDATE April 23rd, 2009

Microsoft has now released .NET 2.0 SP2 as its own install.


Design Patterns - Writing better software

Writing applications that simply work is for the most part pretty easy. However writing software that is well designed, scalable and also readable takes a bit more work. Using and understanding design patterns can help you write better software. Here is a few links I found related to the topic. I was going to write all about this but found someone that did almost exactly what I would have done so just going to post a link to the topic.



Object Oriented Design Patterns

While I have been programming now for about 3 years using the Object Oriented programming environment of C# I have not really spent any time looking at patterns. I am guessing in my natural progression of learning programming this seems to be the next logical step.

I am on vacation currently and this gave me the perfect opportunity to spend some time reading which I don't usually do much of. In fact I can say that I have never read a technical publication fully. Well as of today that is no longer the case. I read "Design Patterns Explained" by Alan Shallow and James Trott entirely. I have to say that this is the very first publication I have read that explained things the way I learn. In fact the author explains the same process he went through in understanding patterns and his natural progression in learning software. I highly recommend this book if you are just learning OO Design patterns.

Next I went and did some searches on the Design patterns that I had been reading about but related to my programming language and came across a great page on codeproject that shows an entire program and how each aspect relates to various patterns.


Design Patterns Implementation in a Storage Explorer Application

By Breman Sinaga

URL: http://www.codeproject.com/KB/architecture/sinagastorageexplorer.aspx

Breman does an incredible job of showing how some of the software patterns might be implemented in a real world application and really this is bringing together a much better understanding of how to apply some of these patterns. I am starting to think about how I am possibly already using some of these patterns without knowing it before and how some of the things solved in our applications might have been better structured had we implemented some of the various patterns.

I read some more today and found a link to a page that gives an outline at how to study patterns and information on creating a study group. I think it might be cool to see if Seattle has a local study group related to patterns so putting this as a reminder.




Good reference resource Wikipedia: http://en.wikipedia.org/wiki/Design_Patterns

Spawning multiple .NET delegates really slow

I recently worked on a project that had a listener service running that took TCP requests and made external calls to a remote WebService. The listener service could have up to 300 concurrent requests.

In testing the service it was showing a significant delay in executing requests above 3-4 concurrent clients. The first bottleneck had to do with the number of allowed HTTP connections to a remote machine from our service. It turns out that by default only 2 concurrent HTTP connections are allowed by default.

Here is an MSDN article that explains the settings that need to be set to allow more connections to be made.


So setting something like this what required.

      <add address = "http://www.contoso.com" maxconnection = "10" />
      <add address = "*" maxconnection = "2" />

While this showed a little improvement the problems were not over. It turns out that the ThreadPool by default only starts up with 4 idle threads. As you exceed that limit the ThreadPool checks every 500ms to see if more threads are needed and only spawns up one thread every 500ms. So you can see that it will take a long time to get spawned a large number of concurrent requests.

Solution to this is to set the minimum threads in the threadpool.

ThreadPool.SetMinThreads - http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setminthreads.aspx

So we set the Min to the minimum number of threads we wanted sitting idle. Ok now performance was very good up to the number of threads we set as our minimum. But wait things are not over. We moved the code to another machine and the performance degraged. What could be wrong? It worked on my machine just fine!!

So we looked at what was different about the machines. Turned out my machine was running .NET 2.0 and the other machine was running .NET 2.0 SP1. I guess in .NET 2.0 SP1 a new bug was introduced that if threads are requested to quickly it will revert back to slowly invoking new threads.

Solution is to put a delay after each new call to a delegate for a few milliseconds using something like Thread.Sleep(50);. I had to put about 50ms delay on the machine in question. However I found that this bug was to be fixed in .NET 2.0 SP2. A bit of searching I found that .NET 2.0 SP2 had actually been released but not by itself. You have to download .NET 3.5 SP1 which also includes .NET 2.0 SP2. After installing this on the machine no delay was required any longer and performance was very good.

Hope this helps someone