Checking Internet Connection using Console Application

Raj.Trivedi
Posted by in C# category on for Beginner level | Points: 250 | Views : 4582 red flag
Rating: 5 out of 5  
 1 vote(s)

Let us learn how to check Internet Connection using Console Application

Introduction


Few days back I had discussion with a friend on a requirement to send mail using console application so he wants to check the status of Internet Connection.

Objective

  1. Checking Internet Connection in Console application


Using the code

  1. Go to Visual Studio -> Create a New Project -> Console Application 
  2. Then we will import a Namespace using system.net that will allow us to check if the data is received or not.
  3. We will create an Byte array and Create an Object of WebClient Class available in System.Net
  4. Now we will assign a URL to download the data in the form of bytes using the Download method of WebClient Class.
  5. If the received data is not null and if its length is greater than 0. Then we can say that there is an Active internet Connection or There is no connection available.

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Check_Internet_Connection
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            byte[] recieveddata = null;
            try
            {
                // Here we are checking tthe no of bytes recived 
                recieveddata = client.DownloadData("http://www.dotnetfunda.com");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message.ToString());
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.ReadLine();
            }
            if (recieveddata != null && recieveddata.Length > 0)
            {
                // If we recieve data we say connection is active
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Internet Connection Available");
                Console.ReadLine();
            }
            else
            {
                // If we dont recieve data we say connection is not active
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Internet Connection not  Available");
                Console.ReadLine();
            }
        }
    }
}
Ouput 1 :- Internet Connection Available



Now i will disable the connection and we will get Internet Connection Not available.

Output 2 :- No Internet Connection




Conclusion


Using this article as resource we can change the code as our logic if there are mails send out automatically and if while sending mail the connection fails we can capture the message and store it in LOG.



Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Posted by: Sourav.Kayal on: 7/13/2013 | Points: 25
Nice and tricky idea to check internet connection
Posted by: Shitalr on: 7/17/2013 | Points: 25
very useful

Login to post response

Comment using Facebook(Author doesn't get notification)