I visse situationer kan det være ganske praktisk, at kunne pinge fra ens kode. Et eksempel kunne være i forbindelse med implementation af applikationer, der
skal kunne fungere i både connectede og disconnectede scenarier. Ved at pinge relevante servere kan man konstatere, om de ønskede services er tilgængelige -
eller om applikationen skal nøjes med at køre i disconnected mode - og dermed med alternativ eller begrænset funktionalitet.
Når man kalder Send-metoden på en instans af System.Net.NetworkInformation.Ping-klassen, kan man medsende en række forskellige parametre, hvor den vigtigste
naturligvis er den host, som man ønsker at pinge, og som resultat får man et System.Net.NetworkInformation.PingReply-objekt på hvilket, man blandt andet kan
teste, hvorvidt hosten rent faktisk kunne pinges, og hvor lang tid operationen tog.
Nedenfor er der et par simple eksempler på brug af ping. Eksemplerne er synkrone, men om ønsket kan man også foretage asynkrone ping-operationer ved brug af
SendAsync-metoden.
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim host As String = "127.0.0.1"
Dim timeout As Integer = 117
If CanPingHost(host, timeout) Then
System.Windows.Forms.MessageBox.Show(host + " kunne pinges.", "Ping dingeling´- kald af CanPingHost")
Else
System.Windows.Forms.MessageBox.Show(host + " kunne ikke pinges.", "Ping dingeling´- kald af CanPingHost")
End If
Dim reply As System.Net.NetworkInformation.PingReply = GetPingReply(host, timeout)
Dim s As String = ""
s += "Address: " + reply.Address.ToString() + vbCrLf
s += "Status: " + reply.Status.ToString() + vbCrLf
s += "RoundTrip time: " + reply.RoundTripTime.ToString()
System.Windows.Forms.MessageBox.Show(s, "Ping dingeling - kald af GetPingReply")
End Sub
Public Function CanPingHost(ByVal host As String, ByVal timeout As Integer) As Boolean
Dim data As String = "12345678901234567890123456789012"
Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
Dim sender As System.Net.NetworkInformation.Ping = New System.Net.NetworkInformation.Ping()
Dim options As System.Net.NetworkInformation.PingOptions = New System.Net.NetworkInformation.PingOptions()
options.DontFragment = True
Dim reply As System.Net.NetworkInformation.PingReply = sender.Send(host, buffer, timeout, options)
If reply.Status = System.Net.NetworkInformation.IPStatus.Success Then
Return True
Else
Return False
End If
End Function
Public Function GetPingReply(ByVal host As String, ByVal timeout As Integer) As System.Net.NetworkInformation.PingReply
Dim data As String = "12345678901234567890123456789012"
Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
Dim sender As System.Net.NetworkInformation.Ping = New System.Net.NetworkInformation.Ping()
Dim options As System.Net.NetworkInformation.PingOptions = New System.Net.NetworkInformation.PingOptions()
options.DontFragment = True
Dim reply As System.Net.NetworkInformation.PingReply = sender.Send(host, buffer, timeout, options)
Return reply
End Function
private void button1_Click(object sender, System.EventArgs e)
{
string host = "127.0.0.1";
int timeout = 117;
if (CanPingHost(host, timeout))
{
System.Windows.Forms.MessageBox.Show(host + " kunne pinges.", "Ping dingeling´- kald af CanPingHost");
}
else
{
System.Windows.Forms.MessageBox.Show(host + " kunne ikke pinges.", "Ping dingeling´- kald af CanPingHost");
}
System.Net.NetworkInformation.PingReply reply = GetPingReply(host, timeout);
string s = "";
s += "Address: " + reply.Address.ToString () + "\n\n";
s += "Status: " + reply.Status.ToString() + "\n\n";
s += "RoundTrip time: " + reply.RoundTripTime.ToString();
System.Windows.Forms.MessageBox.Show(s, "Ping dingeling - kald af GetPingReply");
}
public bool CanPingHost(string host, int timeout)
{
string data = "12345678901234567890123456789012";
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
System.Net.NetworkInformation.Ping sender = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
System.Net.NetworkInformation.PingReply reply = sender.Send(host, buffer, timeout, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
public System.Net.NetworkInformation.PingReply GetPingReply(string host, int timeout)
{
string data = "12345678901234567890123456789012";
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
System.Net.NetworkInformation.Ping sender = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
System.Net.NetworkInformation.PingReply reply = sender.Send(host, buffer, timeout, options);
return reply;
}
|