Show ping round trip time
Show ping round trip time
Show ping round trip time
Show ping round trip time
Show ping round trip time
Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time
Show ping round trip time Show ping round trip time
Show ping round trip time
Go Back  Xtreme Visual Basic Talk > > > Show ping round trip time


Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2011, 06:21 AM
vb2008 vb2008 is offline
Regular
 
Join Date: Jan 2008
Posts: 88
Default Show ping round trip time


I am trying to figure out how to show the round trip time it took to ping a server. I know the following code conducts the ping and that the 1000 is for the miliseconds however I can't seem to figure out how to capture the time it took.

Code:
If My.Computer.Network.Ping("www.whatever.com",1000) Then
   MsgBox("Server pinged successfully.")
Else
   MsgBox("Ping request timed out.")
End If
Any help? Thanks
Reply With Quote
  #2  
Old 11-08-2011, 07:15 AM
PlausiblyDamp's Avatar
PlausiblyDampShow ping round trip time PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You are probably better off ignoring the VB helper method as it is hiding a lot of the ping functionality - if you use the Ping class (http://msdn.microsoft.com/en-us/libr...tion.ping.aspx) directly you can get a lot more information back.

e.g.
Code:
Dim pingSender as new Ping ()
Dim reply as PingReply = pingSender.Send("www.whatever.com",1000)
Debug.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime)
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 11-08-2011, 07:26 AM
vb2008 vb2008 is offline
Regular
 
Join Date: Jan 2008
Posts: 88
Default

Quote:
Originally Posted by PlausiblyDamp View Post
You are probably better off ignoring the VB helper method as it is hiding a lot of the ping functionality - if you use the Ping class (http://msdn.microsoft.com/en-us/libr...tion.ping.aspx) directly you can get a lot more information back.

e.g.
Code:
Dim pingSender as new Ping ()
Dim reply as PingReply = pingSender.Send("www.whatever.com",1000)
Debug.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime)
Yeah I had given up on that however I am still running into an issue.

Code:
   Dim pingSender As New Ping()
        Dim options As New PingOptions()
        Dim ping_system As String
        options.DontFragment = True
        Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        Dim buffer() As Byte = Encoding.ASCII.GetBytes(data)
        Dim timeout As Integer = 120

        For Each ping_system In SelectedServers.Items

            Dim reply As PingReply = pingSender.Send(ping_system.ToString, timeout, buffer, options)
            If reply.Status = IPStatus.Success Then

                ' Ping success, report as such
                Dim row As String() = New String() {ping_system, "Pass", reply.RoundtripTime, reply.Buffer.Length, reply.Options.Ttl}
                DataGridView2.Rows.Add(row)

            Else
                ' Ping failure, report as such
                Dim row As String() = New String() {ping_system, "Failure", reply.RoundtripTime, reply.Buffer.Length, reply.Options.Ttl}
                DataGridView2.Rows.Add(row)
                ' Console.WriteLine(myResult.Status.ToString)
            End If
        Next
The following error keeps taking place. It says that "Message=No such host is known" below however ping_system does hold the value which is the hostname so I am really confused as to why the above code is not working.
The SelectedServers ListBox does in fact have servers listed and they are good ones that I can ping from my machine. What am I missing here?

Quote:
System.Net.NetworkInformation.PingException was unhandled
Message=An exception occurred during a Ping request.
Source=System
StackTrace:
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at DRPrep.frmMain.Button2_Click(Object sender, EventArgs e) in C:\Users\lroberts\Desktop\Source Code\DR Prep\Form1.vb:line 1451
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms .UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int 32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at DRPrep.frmMain.Main() in C:\Users\lroberts\Desktop\Source Code\DR Prep\Form1.vb:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Net.Sockets.SocketException
ErrorCode=11001
Message=No such host is known
NativeErrorCode=11001
Source=System
StackTrace:
at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
InnerException:
Reply With Quote
  #4  
Old 11-08-2011, 07:33 AM
PlausiblyDamp's Avatar
PlausiblyDampShow ping round trip time PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you step through the code in a debugger what is the value of ping_system.ToString()?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 11-08-2011, 07:39 AM
vb2008 vb2008 is offline
Regular
 
Join Date: Jan 2008
Posts: 88
Default

Quote:
Originally Posted by PlausiblyDamp View Post
If you step through the code in a debugger what is the value of ping_system.ToString()?
angel.grhq.xxx.com

Which is exactly what I would expect as that is the first one in the list and I can in fact ping it from my system so I know the network can see it.

If I change this...
Code:
 Dim reply As PingReply = pingSender.Send(ping_system.ToString, timeout, buffer, options)
To this...
Code:
 Dim reply As PingReply = pingSender.Send("angel.grhq.xxx.com", timeout, buffer, options)
Then it works fine. It is only when you attempt to replace it with a string value that the error takes place which then in return prevents you from using the list. Its very strange.

It does not like the ping_system.ToString or ping_system. It is only if I give it a hard coded value there with "".
Reply With Quote
  #6  
Old 11-08-2011, 07:48 AM
PlausiblyDamp's Avatar
PlausiblyDampShow ping round trip time PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Do you have option strict turned on?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 11-08-2011, 07:50 AM
vb2008 vb2008 is offline
Regular
 
Join Date: Jan 2008
Posts: 88
Default

Quote:
Originally Posted by PlausiblyDamp View Post
Do you have option strict turned on?
No it is set to off.
Reply With Quote
  #8  
Old 11-08-2011, 08:15 AM
AtmaWeapon's Avatar
AtmaWeaponShow ping round trip time AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

When you get an error message, treat it like a puzzle and spend some time thinking about it. We aren't wizards that memorize incantations to copy/paste: we're scientists who arrive at conclusions after careful analysis.

For example, why would a ping say "no host known"? Obviously it can't find a server with the address you gave. But you say it looks OK. Could it be there's something in the string you can't see? Remember, not every character that is valid for a string is visible. I bet if you took a look at the string's Length, you'd find out it has some leading or trailing whitespace. In my experiments, both caused the Ping class to claim google.com was an unknown host.

I'd still turn Option Strict on as PlausiblyDamp is suggesting. When Option Strict is off, VB does its best to make the types of your variables play well with each other. This allows you to write code faster, but if you haven't memorized *every* conversion rule in the language specification sooner or later it will do something you didn't expect. Then you're going to lose all that time you saved learning about compiler rules no one should have to memorize. Use Option Strict. Think about your data types.

Here's how I'd write the start of your loop:
Code:
For Each selectedServer In SelectedServers.Items
    Dim serverName As String = selectedServer.ToString().Trim()
    Dim reply As PingReply = pingSender.Send(serverName, timeout, buffer, options)
    ...
It only costs a handful more characters, but having that intermediate variable eases debugging and lets you compare the before and after strins. The Trim() method removes leading and trailing whitespace characters; I bet this is the root of your problem.

Be mindful that you should catch PingException anyway. You've got a "failure" case set up but sometimes the ping fails in a way that throws the exception.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #9  
Old 11-08-2011, 08:21 AM
vb2008 vb2008 is offline
Regular
 
Join Date: Jan 2008
Posts: 88
Default

Quote:
Originally Posted by AtmaWeapon View Post
When you get an error message, treat it like a puzzle and spend some time thinking about it. We aren't wizards that memorize incantations to copy/paste: we're scientists who arrive at conclusions after careful analysis.

For example, why would a ping say "no host known"? Obviously it can't find a server with the address you gave. But you say it looks OK. Could it be there's something in the string you can't see? Remember, not every character that is valid for a string is visible. I bet if you took a look at the string's Length, you'd find out it has some leading or trailing whitespace. In my experiments, both caused the Ping class to claim google.com was an unknown host.

I'd still turn Option Strict on as PlausiblyDamp is suggesting. When Option Strict is off, VB does its best to make the types of your variables play well with each other. This allows you to write code faster, but if you haven't memorized *every* conversion rule in the language specification sooner or later it will do something you didn't expect. Then you're going to lose all that time you saved learning about compiler rules no one should have to memorize. Use Option Strict. Think about your data types.

Here's how I'd write the start of your loop:
Code:
For Each selectedServer In SelectedServers.Items
    Dim serverName As String = selectedServer.ToString().Trim()
    Dim reply As PingReply = pingSender.Send(serverName, timeout, buffer, options)
    ...
It only costs a handful more characters, but having that intermediate variable eases debugging and lets you compare the before and after strins. The Trim() method removes leading and trailing whitespace characters; I bet this is the root of your problem.

Be mindful that you should catch PingException anyway. You've got a "failure" case set up but sometimes the ping fails in a way that throws the exception.
That was it exactly. That you both for your help and the great detail on this. More knowledge gained.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Show ping round trip time
Show ping round trip time
Show ping round trip time Show ping round trip time
Show ping round trip time
Show ping round trip time
Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time Show ping round trip time
Show ping round trip time
Show ping round trip time
 
Show ping round trip time
Show ping round trip time
 
-->