How to Ping with C Sharp?

21.03.2025
553
How to Ping with C Sharp?

C# Ping Code

I will explain how to ping any computer to any site with c sharp. We will use System.Net.NetworkInformation and System.Net library in c sharp for this event. With these libraries, we can easily ping a site or computer we want.

If we look at our codes that we will use in the btn_click event, we do not forget to add System.Net.NetworkInformation and System.Net namespace to our project.

protected void Btn_Click(object sender, EventArgs e)

{ 
Ping p = new Ping(); PingReply sonuc = p.Send(Txtip.Text);

  if (sonuc.Status == IPStatus.Success)

  { 
    lblsonuc.Text += sonuc.Address.ToString() + "";

    lblsonuc.Text += sonuc.RoundtripTime.ToString() + "";

    lblsonuc.Text += sonuc.Options.Ttl.ToString() + ""; 
  }

  else if (sonuc.Status == IPStatus.TimedOut) 
  { 
    lblsonuc.Text = ("Time out."); 
  } 
}
C#

In this way, we have solved the pinging thing in a simple sense. I use it to check whether my servers are online or not. Of course, I changed it a bit. The page “post-back” every 5 seconds, i.e. refresh, allows me to get updated information about our servers. It worked very successfully.

I will share an example of a console application for a wider example:

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();

// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}
C#

MAKE A COMMENT
COMMENTS - 0 COMMENTS

No comments yet.

Bu web sitesi, bilgisayarınıza bilgi depolamak amacıyla bazı tanımlama bilgilerini kullanabilir.
Bu bilgilerin bir kısmı sitenin çalışmasında esas rolü üstlenirken bir kısmı ise kullanıcı deneyimlerinin iyileştirilmesine ve geliştirilmesine yardımcı olur.
Sitemize ilk girişinizde vermiş olduğunuz çerez onayı ile bu tanımlama bilgilerinin yerleştirilmesine izin vermiş olursunuz.
Çerez bilgilerinizi güncellemek için ekranın sol alt köşesinde bulunan mavi kurabiye logosuna tıklamanız yeterli. Kişisel Verilerin Korunması,
Gizlilik Politikası ve Çerez (Cookie) Kullanımı İlkeleri hakkında detaylı bilgi için KVKK&GDPR sayfamızı inceleyiniz.
| omersahin.com.tr |
Copyright | 2007-2025