VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 340,155 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 3,993 people online right now. Registration is fast and FREE... Join Now!




Get Ping in Milliseconds

Gets the time taken to ping an IP.

Submitted By: RodgerB
Actions:
Rating:
Views: 6,345

Language: VB.NET

Last Modified: December 6, 2007
Instructions:
1) Copy and paste the snippet into a class.
2) Read below how to call the function.

Snippet


  1. ''' <summary>
  2. ''' Gets the time taken to ping an IP.
  3. ''' </summary>
  4. ''' <param name="hostNameOrAddress">IP or HostName to ping</param>
  5. ''' <returns>The time in milliseconds taken to ping the IP.</returns>
  6. ''' <remarks></remarks>
  7.  
  8. Public Shared Function GetPingMs(ByRef hostNameOrAddress As String)
  9.     Dim ping As New System.Net.NetworkInformation.Ping
  10.     Return ping.Send(hostNameOrAddress).RoundtripTime
  11. End Function
  12.  
  13. ' How to call this function (IP Address).
  14. GetPingMs("127.0.0.1")
  15.  
  16. ' How to call this function (HostName).
  17. GetPingMs("www.dreamincode.net")

Copy & Paste


Comments

vikki 2008-02-01 17:51:35

Rewritten to prevent crashes:

CODE
Public Shared Function GetPingMs(ByRef hostNameOrAddress As String) Dim ping As New System.Net.NetworkInformation.Ping Try Return ping.Send(hostNameOrAddress).RoundtripTime Catch Return "dead" End Try End Function
Else it will lock up if the host is not responding.

RodgerB 2008-02-01 20:12:30

1) It won't 'lock up', it will throw an exception when the host isn't responding. 2) With all the classes in the .NET framework, no error is caught for you, it throws an exception. It is not the function's fault, it is the developer(s) responsibility to use error handling when calling the function (hence you'd use a Try Catch block when calling the function, not inside it). Thank you for your input though!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month