Simple Networking Tasks in C#

March 27, 2009 | By

Developing applications in C# that access resource on the internet is easy. This post shows four useful functions: checking if the PC is connected to a network, domain name to IP address lookup, ping a host and download a file.

 

Network Availability

You can check if there are any network connections using NetworkInterface.GetIsNetworkAvailable static method, found in the System.Net.NetworkInformation assembly. MSDN: “A network connection is considered to be available if any network interface is marked “up” and is not a loopback or tunnel interface.” Here is how you could check for a network connection:

 

Host Name Lookup

DNS Lookup

To lookup a host name’s IP address or vice versa you could use the Dns.GetHostEntry static method in the System.Net assembly. It returns an IPHostEntry object that contains the host name and all the aliases and IP addesses associated with it. Here is an example that populates a ListView with all the IP addresses of a host name entered in a TextBox:

 

Ping a Host

Ping

Pinging a host can be done using Send or SendAsync methods of the Ping object found in the System.Net.NetworkInformation assembly. Send pings the host and waits for a reply before returning to calling method. And returns a PingReply object that contains the ICMP echo’s result and the status. SendAsync on the other hand pings the host asynchronously and returns immediately to the calling method. When SendAsync is finished the PingCompleted event occurs and the PingCompletedEventArgs parameter contains the details of the ping. The example below populates a ListView with the results of three pings to a host:

 

Download a File

DownloadFile

Downloading a file using the System.Net.WebClient can also be done synchronously and asynchronously. The DownloadFile method blocks while downloading the file. The asynchronous method DownloadFileAsync starts the download and when finished the DownloadFileCompleted event occurs.

The source code below contains all the examples above in one Visual Studio 2008 project.

 

Download Source Code

 

Resume writing is the most important step in presenting your skills and getting the job you want with https://devmyresume.com/. It is a process that allows a candidate to highlight the achievements and experience that will attract the attention of any employer and ensure the realization of obtaining hisher dream career.

Filed in: Mono 2.0, WinForms | Tags: , ,

Comments (15)

  1. Great information, thanks a lot.

  2. Dany

    Thanks alot for your Great information.
    How can I download source code in one Visual Studio 2008 project.

  3. Sorry about that, I deactivated the wrong plugin. Everything is working fine now.

  4. Dany

    Thanks alottttttttttttttttttttttttttttttt

  5. You’re welcome Dany. Glad I could help.

  6. temp

    hi

    2 NIC (both have Internet). how can we tell System.Net.WebClient to use a specific Network Card ?

  7. Hi temp,

    You can have more than one NIC in a computer but you can only set the gateway of one of them, and this is the one that can reach the Internet.

  8. Temp

    Can i use the same coding in .NET for ping’g different IP’s

  9. @Temp, yes you can. Just remove the for loop in the example (used to ping the same host 3 times) and enclose the whole try block with a for loop that goes through an array of IP addresses or domain names.

  10. Dorababu

    Good one but I had a small doubt, if i enter text like aaaa or some thing i am getting some Ip Address what does it mean

  11. @Dorababu, this must be from your network setting because it should display the message: Could not resolve name.

  12. asanthosh

    it is really nice

  13. Pavel Bega

    It would be nice to add
    ping.Dispose()
    to the
    pingButton_Click().

  14. Siducha

    I’m not being able to ping and lookup IP address but I can do it to websites. Can any body explain me why?

  15. Siducha, if you want to display the hostname associated with IP address add the following line inside the try block in the second listing:

    lookupListView.Items.Add(hostEntry.HostName);