Pinging a Host Asynchronously
In order to ping a host in asynchronous manner all we have to do is create an instance of EchoClient and use BeginEcho and EndEcho functions.
The whole process will consist of following step:
- First we will create an instance of EchoClient.
- We will call BeginEcho() to send a ping to the host.
- We will call EndEcho() function when we plan to stop the ping just sent. We will call this in EchoCompleted event.
The following code will ping a host in asynchronous manner. When the process of sending the ping to the host is complete, EchoCompleted event will be fired. Here we can check the response from the host and will close out ping operation by calling EndEcho().
Example
[C#]
class AsyncPing
{
//Aspose.Network.Icmp.EchoClient
private static EchoClient echoClient;
[STAThread]
static void Main(string[] args)
{
echoClient = new EchoClient("127.0.0.1");
byte[] data = new byte[] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
try
{
// assign handler to EchoClient's EchoComplete event, this event //occurs when echo(ping) message is replied
echoClient.EchoCompleted +=new EchoCompletedEventHandler(OnEchoCompleted);
Console.WriteLine("pinging 127.0.0.1 with 32B data");
echoClient.BeginEcho(new EchoMessage(data));
Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void OnEchoCompleted(object sender, EchoCompletedEventArgs e)
{
if(echoClient != null)
{
// get ping reply
EchoReplyMessage echoReplyMessage = echoClient.EndEcho(e.AsyncResult);
// display various info
Console.WriteLine("replied from " + echoClient.Host
+": bytes=" + echoReplyMessage.DataBlock.Length
+" TTL="+echoReplyMessage.TimeToLive);
}
}
}
[VB.NET]
Class AsyncPing
'Aspose.Network.Icmp.EchoClient
Private Shared echoClient As EchoClient
<STAThread()> _
Private Shared Sub Main(ByVal args As String())
echoClient = New EchoClient("127.0.0.1")
Dim data As Byte() = New Byte() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}
Try
AddHandler echoClient.EchoCompleted, AddressOf OnEchoCompleted
' assign handler to EchoClient's EchoComplete event, this event occurs when ' echo(ping) message is replied
Console.WriteLine("pinging 127.0.0.1 with 32B data")
echoClient.BeginEcho(New EchoMessage(data))
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Private Shared Sub OnEchoCompleted(ByVal sender As Object, ByVal e As EchoCompletedEventArgs)
If echoClient IsNot Nothing Then
' get ping reply
Dim echoReplyMessage As EchoReplyMessage = echoClient.EndEcho(e.AsyncResult)
' display various info
Console.WriteLine("replied from " + echoClient.Host + ": bytes=" + echoReplyMessage.DataBlock.Length + " TTL=" + echoReplyMessage.TimeToLive)
End If
End Sub
End Class
Working with IcmpClient
The IcmpClient class represents an implementation of ICMP client. It provides properties and methods to send or receive ICMP messages.
If you want to perform any ICMP operations, you should begin with creating an IcmpClient instance. It allows you to send an ICMP message to a host and receive the replies.
IcmpClient supports synchronous and asynchronous programming models.
IcmpClient provides SendCompleted event which is triggered after sending ICMP messages, and ReceiveCompleted event which is triggered after receiving the ICMP messages.
Working with EchoClient
The EchoClient class inherits from IcmpClient and provides various properties and methods which facilitate to ping a host.
If you want to ping any host in your application, the first step is to create a new instance of the EchoClient class and then use the EchoClient instance to send and receive echo messages.
EchoClient supports synchronous and asynchronous programming model.
EchoClient provides EchoCompleted event which is fired after sending Echo messages to a host.
Creating a new instance of EchoClient class
Firstly, we have to create a new instance of the EchoClient class, and specify a host address.
Example
[C#]
EchoClient echoClient = new EchoClient("127.0.0.1");
[VB.NET]
Dim echoClient As EchoClient = new EchoClient("127.0.0.1")
Contructing an Echo message with the EchoMessage
After creating an instance of the EchoClient class, an echo message can be constructed and sent to the host specified. The EchoMessage is designed for constructing and parsing the echo messages in Aspose.Network ICMP component.
Example
[C#]
EchoMessage echoMessage = new EchoMessage();
echoMessage.Identifier = echoClient.Identifier;
echoMessage.SequenceNumber = echoClient.IncreaseSequenceNumber;
echoMessage.DataBlock = new byte[] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
[VB.NET]
Dim echoMessage As EchoMessage = New EchoMessage()
echoMessage.Identifier = echoClient.Identifier
echoMessage.SequenceNumber = echoClient.IncreaseSequenceNumber
echoMessage.DataBlock = New Byte()
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
Sending Echo messages and Receiving Echo reply messages
We can now send the echo message created with the echoclient instance and get the response from the host. The EchoReplyMessage class represents the response from the host, which provides you various properties and methods to parsing the received bytes.
Example
[C#]
EchoReplyMessage replyMessage = echoClient.Echo(echoMessage);
[VB.NET]
Dim replyMessage as EchoReplyMessage
replyMessage = echoClient.Echo(echoMessage)
To run these codes, you need to set your project reference to Aspose.Network.dll, which is in the free download setup package: Aspose.Network download.
Once installed, Aspose.Network.dll can be found in Bin folder of your setup directory. If you have any questions or want to share any ideas, please visit Aspose.Network forum.