Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Pinger suggestions? (http://www.programmingforums.org/showthread.php?t=14716)

Jabo Dec 9th, 2007 5:07 AM

Pinger suggestions?
 
I've wrote a little program to ping servers and report the results. I'd be interested in ways I could have done it better if you don't mind.
:

  1. Dim entry(1), line(3) As String
  2.         Dim sr As StreamReader = New StreamReader(path)
  3.         Dim pingres(3) As String
  4.  
  5.         errstring = ""
  6.  
  7.         Try
  8.             Do While sr.EndOfStream = False
  9.                 entry = sr.ReadLine.Trim.Split(Chr(9))
  10.                 Dim Ping As Ping = New Ping
  11.                 Dim PingReply As PingReply
  12.                 PingReply = Ping.Send(entry(0), 2000)
  13.                 tssActivity.Text = "Pinging: " & entry(1) & ": " & entry(0)
  14.                 tssActivity.Invalidate()
  15.                 line(0) = entry(1) & ": "
  16.                 line(1) = entry(0)
  17.                 If PingReply.Status = IPStatus.Success Then
  18.                     line(2) = PingReply.RoundtripTime.ToString
  19.                 Else
  20.                     line(2) = PingReply.Status.ToString
  21.                     errstring += line(0).PadRight(20) & line(1).PadRight(20) & "Ping: " & line(2).PadRight(30) & Chr(13) & Chr(10)
  22.                 End If
  23.                 line(3) = line(0).PadRight(20) & line(1).PadRight(20) & "Ping: " & line(2).PadRight(30) & Chr(13) & Chr(10)
  24.                 txtResult.Text += line(3)
  25.                 Ping.Dispose()
  26.                 txtResult.Invalidate()
  27.                 Me.Refresh()
  28.             Loop
  29.             If Not errstring = "" Then
  30.                 PrntDoc.Print()
  31.             End If
  32.         Catch ex As Exception
  33.             txtResult.Text += ex.Message & Chr(13) & Chr(10)
  34.         End Try
  35.         sr.Close()


Jabo Dec 15th, 2007 11:27 PM

Re: Pinger suggestions?
 
Well, it's a rare occasion when a new programmer does it absolutely right.

mattireland Jan 3rd, 2008 3:58 AM

Re: Pinger suggestions?
 
To me, another relatively new programmer (only been doing it about 2 years) it looks fine. I'd always try to comment my code a bit more though.

I'll see if some of the more experienced guys have comments though....

Sane Jan 3rd, 2008 8:39 AM

Re: Pinger suggestions?
 
You want comments of course.

With reference to functionality, I couldn't say; I've never written a pinger before. If you wanted to get nifty and write very little code, just use the ping system command:
:

ping -n 1 -w 2000 google.ca

Which could be used in Visual Basic like so:
:

  1.     Dim PingTimeOut as String
  2.     Dim PingIPAddr as String
  3.  
  4.     PingTimeOut = "2000"
  5.     PingIPAddr = "google.ca"
  6.  
  7.     Shell "ping -n 1 -w " & PingTimeOut & " " & PingIPAddr


This way's a lot simpler because it handles all exceptions for you. If the ping doesn't work, ping.exe handles that for you.



And if you want to get nit-picky, I'd turn Chr(13) & Chr(10) into a constant, CRLF (if the constant does not already exist), and reference that, instead of having that annoying code repeating 3-4 times. Especially since CRLF is often something you want to change.

Then turn your PadRight 30's and 20's into some sort of constant MarginRight variable, and base your calculations off that.

Finally, your ping timeout of 2000ms could be user defined as a textCtrl or slider in the Graphical Interface, or referenced as another constant, PingTimeout.

Jabo Jan 3rd, 2008 10:35 AM

Re: Pinger suggestions?
 
Shell opens a command window? I'd rather keep the open windows to a minimum, seems to be less things to cause problems since there will be more than one person accessing the computer this will be running on. Also, some of these people are not very technically astute, even after "training".

Sane Jan 3rd, 2008 10:56 AM

Re: Pinger suggestions?
 
There should be a way to pipe the output of ping.exe right into a variable you can even use, without any other windows opening.

Look for popen or something.


All times are GMT -5. The time now is 8:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC