Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 9th, 2007, 5:07 AM   #1
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 254
Rep Power: 1 Jabo is on a distinguished road
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.
vb Syntax (Toggle Plain Text)
  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 is offline   Reply With Quote
Old Dec 15th, 2007, 11:27 PM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 254
Rep Power: 1 Jabo is on a distinguished road
Re: Pinger suggestions?

Well, it's a rare occasion when a new programmer does it absolutely right.
Jabo is offline   Reply With Quote
Old Jan 3rd, 2008, 3:58 AM   #3
mattireland
Hobbyist Programmer
 
mattireland's Avatar
 
Join Date: Jul 2007
Location: Wales, United Kingdom
Posts: 199
Rep Power: 2 mattireland is on a distinguished road
Send a message via MSN to mattireland Send a message via Skype™ to mattireland
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....
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old Jan 3rd, 2008, 8:39 AM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
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:
VB Syntax (Toggle Plain Text)
  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.

Last edited by Sane; Jan 3rd, 2008 at 8:58 AM.
Sane is offline   Reply With Quote
Old Jan 3rd, 2008, 10:35 AM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 254
Rep Power: 1 Jabo is on a distinguished road
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".
Jabo is offline   Reply With Quote
Old Jan 3rd, 2008, 10:56 AM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,827
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Building up a good portfolio - Suggestions? kruptof Coder's Corner Lounge 3 Jun 9th, 2007 8:39 AM
Not encoding nor zip - suggestions markbadger Other Programming Languages 2 Dec 29th, 2005 8:41 AM
Any Suggestions? dannyp Python 4 Aug 2nd, 2005 6:23 PM
Database suggestions some1 C++ 9 Mar 25th, 2005 3:02 PM
Code suggestions Mad_guy Perl 0 Feb 18th, 2005 10:31 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:34 AM.

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