Click here to Skip to main content
Click here to Skip to main content

How to PING Server in C#

By , 18 Sep 2010
 
It is always better to ping a server before actually calling the server from your client application. The Ping service will try to post a dummy request to the server which is used to be very small and tries to get the Response from the server. Generally if the server response is available, the server sends the response immediately.
 
Hence you can save a considerable amount of time building the Request body. You also never need to wait for so long time for Timeouts.
 
To Ping a Server, you can use:
 
public static bool IsConnectedToInternet
{
    get
    {
        Uri url = new Uri("www.abhisheksur.com");
        string pingurl = string.Format("{0}", url.Host);
        string host = pingurl;
        bool result = false;
        Ping p = new Ping();
        try
        {
            PingReply reply = p.Send(host, 3000);
            if (reply.Status == IPStatus.Success)
                return true;
        }
        catch { }
        return result;
    }
}
 
The PING sends an ICMP (Internet Control Message Protocol) echo request to the server and waits to receive the echo back. If the value of Status is success, the PingReply is successful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhishek Sur
Team Leader
India India
Member
Did you like his post?
 
Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.
 
Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook
 
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.
 
You can also mail him directly to abhi2434@yahoo.com
 
Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com
 
Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMany servers block ping response for securitymemberwilson mar23 Jan '13 - 2:43 
Note that more and more servers are set to not respond to ping requests to remove a way for hackers to map a network. So if the ping fails, the server may still be reachable by the app using HTTP.
Generalhi how to change code if no ping exit app on start, thankyo...memberMember 806616025 Feb '12 - 13:10 
hi
how to change code if no ping exit app on start,
thankyou
GeneralReason for my vote of 5 Good one abhishekmvpthatraja29 Oct '10 - 21:10 
Reason for my vote of 5
Good one abhishek
GeneralSome optimizations...memberSilic0re0913 Sep '10 - 11:22 
Uri url = new Uri("www.abhisheksur.com");
string pingurl = string.Format("{0}", url.Host);
string host = pingurl;
 
Those three lines do a lot of back-and-forth conversions, why not just use the host="www.abhisheksur.com" instead of creating a URI, creating a pingurl, and then creating a host?
 
It could be simplified to:
        string host = "www.abhisheksur.com";
        bool result = false;
        Ping p = new Ping();
        try
        {
            PingReply reply = p.Send(host, 3000);
            if (reply.Status == IPStatus.Success)
                return true;
        }
        catch { }
        return result;   //This is never anything but false, so why not just return false?
 
You're code works just fine for pinging a particular IP (www.abhisheksur.com), but really shouldn't be used to determine if the internet is available. What happens when that particular site is down, or gets turned off?
 
A better way to determine if the internet is available is to use interop:
 
using System;
using System.Runtime;
using System.Runtime.InteropServices;
 
public static class InternetConnectionStatus
{
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int Reserved);
 
    public static bool Connected()
    {
        int Descript;
        return InternetGetConnectedState(out Descript, 0);
    }
}
 
Which would be used like:
 
    bool connected = InternetConnectionStatus.Connected();

GeneralRe: Some optimizations...mvpAbhishek Sur13 Sep '10 - 12:14 
Yes actually I have done this to get only the host form a link. The example was not good enough but if the link
 
www.abhisheksur.com/forum.aspx
 
The ping server then should point to only the host, not the entire link. I used it for just that.
 
Rose | [Rose]
Abhishek Sur
Don't forget to click "Good Answer" if you like this Solution.
Visit My Website-->


www.abhisheksur.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 18 Sep 2010
Article Copyright 2010 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid