Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Send ping to web server - computer and get status using system.net vb.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1041_source.zip

In this simple article you will learn that how we can ping a web server or computer and how we can get reply from them using system.net in VB and C# .net.

 

Ping and pingreply class:
These classes are available in system.net.NetworkInformation. These classes are used to send ping to sever or computer and receive reply from the server.
Constructor:
Ping class has empty constructors.
C#
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
VB
Dim As System.Net.NetworkInformation.Ping = New System.Net.NetworkInformation.Ping
Send Ping:
To send ping to the machine or a web server, ping class provide us a function named send. This function has 8 overloaded methods. One of the eight methods takes a url of website or ip of a machine and send ping to the machine. This method returns an object of class pingreply. This class stores the information and status sent back from the machine.
To check the status we have a property named status this property return value of ip status type. This status tells that the ping was successful or not
To demonstrate make a window application. Drag text box and one button on form.
Now write the following code on Button click event:
C#
   private void btn_send_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty)
            {
                System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
                System.Net.NetworkInformation.PingReply prep;
                string url = textBox1.Text;
                prep = p.Send(url);
                if (prep.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    string address = prep.Address.ToString();
                    string time = prep.RoundtripTime.ToString();
                    MessageBox.Show("ping successfull" + "machine address :" + address + "Round trip time" + time);
                }
                else
                {
                    string status = prep.Status.ToString();
                    MessageBox.Show("not successfull" + status);
                }
            }
            else
                MessageBox.Show("enter value");
        }
 
VB
Private Sub btn_send_Click(ByVal sender As ObjectByVal As EventArgs)
        
If (textBox1.Text <> string.Empty) Then
            Dim 
As System.Net.NetworkInformation.Ping = New System.Net.NetworkInformation.Ping
            
Dim prep As System.Net.NetworkInformation.PingReply
            
Dim url As String = textBox1.Text
            prep 
p.Send(url)
            
If (prep.Status System.Net.NetworkInformation.IPStatus.Success) Then
                Dim 
address As String = prep.Address.ToString
                
Dim time As String = prep.RoundtripTime.ToString
                MessageBox.Show((
"ping successfull" + ("machine address :"  _
                                + (address + (
"Round trip time" + time)))))
            
Else
                Dim 
status As String = prep.Status.ToString
                MessageBox.Show((
"not successfull" + status))
            
End If
        Else
            
MessageBox.Show("enter value")
        
End If
    End Sub
This is simple code to ping a machine n get reply from machine.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Ping Application";              
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Ping Application" 
    End Sub
This simple article tells that how we can ping a web server or computer and how we can get reply from them using system.net in VB and C# .net.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Animate Image on Frame using Drawing.ImageAnimator class vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net