473,851 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to ping a URL from C#

I have an offline application that works online for some data
syncronization. For data syncronization I access a webservice. I want
to show whether my application is online or not by checking whether
the application is able to access the webservice.

If the webservice is accessible, the application is online. otherwise
it is offline

What is the best way to ping a url in C# ?

Thanks much

Feb 15 '07 #1
1 18528

There are a few ways to do this. First, you could create a method within
your web service that returns void and call this method first. If this
method is accessible, your web service is online.

Also, if you want to actually ping the domain on which the web service is
hosted, you could use the following code:

using System;
using System.Text;
using System.Net;
using System.Net.Netw orkInformation;
using System.Componen tModel;
using System.Threadin g;

namespace Examples.System .Net.NetworkInf ormation.PingTe st
public class PingExample
public static void Main (string[] args)
if (args.Length == 0)
throw new ArgumentExcepti on ("Ping needs a host or IP

string who = args[0];
AutoResetEvent waiter = new AutoResetEvent (false);

Ping pingSender = new Ping ();

// When the PingCompleted event is raised,
// the PingCompletedCa llback method is called.
pingSender.Ping Completed += new PingCompletedEv entHandler
(PingCompletedC allback);

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaa";
byte[] buffer = Encoding.ASCII. GetBytes (data);

// Wait 12 seconds for a reply.
int timeout = 12000;

// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);

Console.WriteLi ne ("Time to live: {0}", options.Ttl);
Console.WriteLi ne ("Don't fragment: {0}", options.DontFra gment);

// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.Send Async(who, timeout, buffer, options, waiter);

// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne ();
Console.WriteLi ne ("Ping example completed.");

private static void PingCompletedCa llback (object sender,
PingCompletedEv entArgs e)
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
Console.WriteLi ne ("Ping canceled.");

// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main
// is waiting for.
((AutoResetEven t)e.UserState). Set ();

// If an error occurred, display the exception to the user.
if (e.Error != null)
Console.WriteLi ne ("Ping failed:");
Console.WriteLi ne (e.Error.ToStri ng ());

// Let the main thread resume.
((AutoResetEven t)e.UserState). Set();

PingReply reply = e.Reply;

DisplayReply (reply);

// Let the main thread resume.
((AutoResetEven t)e.UserState). Set();

public static void DisplayReply (PingReply reply)
if (reply == null)

Console.WriteLi ne ("ping status: {0}", reply.Status);
if (reply.Status == IPStatus.Succes s)
Console.WriteLi ne ("Address: {0}", reply.Address.T oString
Console.WriteLi ne ("RoundTrip time: {0}",
reply.Roundtrip Time);
Console.WriteLi ne ("Time to live: {0}", reply.Options.T tl);
Console.WriteLi ne ("Don't fragment: {0}",
reply.Options.D ontFragment);
Console.WriteLi ne ("Buffer size: {0}", reply.Buffer.Le ngth);

Let me know if you have any additional questions.

Shaun McDonnell

"Krish" <ba****@gmail.c omwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
I have an offline application that works online for some data
syncronization. For data syncronization I access a webservice. I want
to show whether my application is online or not by checking whether
the application is able to access the webservice.

If the webservice is accessible, the application is online. otherwise
it is offline

What is the best way to ping a url in C# ?

Thanks much
Feb 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Jason Rodman | last post by:
I have downloaded every example on how to create a ping utility in .Net in both VB and C#, but have been disappointed with the results. I have YET to find an example that returns consistent results that are even close the the actual ping command line utility. For example: If i would ping the same server over and over, some pings would come back in 0 milliseconds, others would come back in 15, but never in between. It would always jump...
by: Mantorok | last post by:
Hi all I want to ping an ipaddress/host to check for a response. Is there any easy way in C#/.Net to do this? Thanks Kev
by: Eric Wenger | last post by:
I'm pretty new to C# and I'm trying to do what I believe should be a simple thing. I want to modify a program that is using the System.Net.NetworkInformation.Ping class to have some custom behavior. For example I would like to do a "TCP-ping" where I try to connect to a TCP port on a remote machine. It seems obvious to me that I should try to derive a new Ping class (TCPPing) from the existing class so that I can use its interfaces. ...
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info: http://support.microsoft.com/default.aspx?scid=kb;en-us;170591 ). The problem I have is in order to perform a discovery/ping of an entire subnet (192.168.1.* for instance) I have to do a FOR loop to itterate through all of the addresses. That it seems...
by: scotty | last post by:
I need to do a ping scan of a subnet. I can Enum through the IPs and do this, but it takes over 5 minutes as I can only create 1 process at a time. Does anyone know how I can create multiple Ping Processes so as to send out 255 pings at once? (sort of a reverse ping flood). FYI: using the " -n 1" switch with Ping will cause it to only send 1 ping. Basicly the code is:
by: Neel | last post by:
I am trying to "ping" a remote host in my C++/Redhat Linux code to check whether that host is connected or not. if (0 == system("ping -w 2 But, in both cases (connected/disconnected), system call returns 0. Can someone please show what I am doing wrong? How to check the actual result of the ping status using C++ on Redhat linux ? Thanks in advance.
by: Deepak | last post by:
I am programing a ping application which pings various centers . I used timer loop and it pings one by one. Now when i finish pinging one center it should wait for the ping_completed function to be executed and then continue pinging another certer. The ping_completed function is called on completion of ping by the os and i have no control on it .
by: imughal | last post by:
I got the perl script which does following task. This solution reads in a line from a text file that is passed in as input parameter 1 to a Perl script. This script will then ping the machine using TCP pings to the remote hosts echo port and will return up/down and the ping response time. A second optional attribute may be passed in that will set the default timeout for the ping response. Text file format: localhost...
by: Dave Marden | last post by:
I currently use this routine in vbscript to ping computers and get the status of ping to determine whether to try to backup a machine, I am trying to change it to work with vb2003.net I am wondering if anyone has a ping function they could share with me. I have done some searching on this but cannot find anything specifically for vb2003. Sub PingComputer If PingStatus(PCName) = "Success" Then
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.