473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Ping subnet

I want to ping every IP on a subnet (255 IP's) and display in a table what
IP's are active. I'm currently using a For loop but this takes forever
because it pauses to test each Ping. So my guess is that I need to run 255
functions each in their own thread. Would there be an easy way to do this,
maybe create the functions dynamically within a loop? Any help with the
syntax would be great. I haven't done much threaded programming.
Jul 27 '06 #1
2 3216
Do you need this functionality as part of an existing app or are you trying to
write something that does this? There are already apps out there that do this
very quickly, like nmap.

"Ryan" <Ty****@newsgroups.nospamwrote in message
news:eo**************@TK2MSFTNGP04.phx.gbl...
>I want to ping every IP on a subnet (255 IP's) and display in a table what IP's
are active. I'm currently using a For loop but this takes forever because it
pauses to test each Ping. So my guess is that I need to run 255 functions each
in their own thread. Would there be an easy way to do this, maybe create the
functions dynamically within a loop? Any help with the syntax would be great.
I haven't done much threaded programming.

Jul 27 '06 #2
Hi Ryan,

For such scenario, I recommend use several BackgroundWorker instances to do
the ping and update UI to report IP address's status.

First, we need to create a class to provide the next IP address that needs
to ping:

Public Class IpProvider
Private _index As Integer = 0
Public ReadOnly Property GetNextIpAddress() As String
Get
SyncLock GetType(IpProvider)
If _index = 255 Then Return Nothing
_index = _index + 1
Return "192.168.1." + _index.ToString()
End SyncLock
End Get
End Property
End Class

Here I'm using a hardcoded subnet for demostrating purpose. You may need to
use some properties to set the specified subnet.

Add a ListBox to your Form which will display each IP address's status. Add
two buttons, the first one will be used to start the ping threads, the
second one will be used to cancel the ping threads. Here we will use 5
threads to do the ping, each thread will use the IpProvider to get the next
ip address to ping.

Const THREAD_COUNT As Integer = 5
Private bws(THREAD_COUNT - 1) As BackgroundWorker

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ipp As New IpProvider

For i As Integer = 0 To THREAD_COUNT - 1
Dim bw As New BackgroundWorker
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True

AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf
bw_RunWorkerCompleted
bw.RunWorkerAsync(ipp)

bws(i) = bw
Next
End Sub

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As
DoWorkEventArgs)
Dim bw As BackgroundWorker = CType(sender, BackgroundWorker)
e.Result = PingIp(e.Argument, bw, e)
End Sub

Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As
ProgressChangedEventArgs)
ListBox1.Items.Add(e.UserState)
End Sub

Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As
RunWorkerCompletedEventArgs)
If (e.Error IsNot Nothing) Then
ListBox1.Items.Add(e.Error.Message)
ElseIf e.Cancelled Then

Else

End If
End Sub

Private Function PingIp(ByVal ipp As IpProvider, ByVal bw As
BackgroundWorker, ByVal e As DoWorkEventArgs) As Boolean
While True
If bw.CancellationPending Then
e.Cancel = True
Return False
Else
Dim ipaddress = ipp.GetNextIpAddress()
If ipaddress Is Nothing Then Exit While
' insert code to ping the address, here we're using Sleep()
to simulate a delay
Thread.Sleep(1000)

' then we report the result, here we don't care the
progress percentage
bw.ReportProgress(0, ipaddress + " is alive")
End If
End While
Return True
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
For i As Integer = 0 To THREAD_COUNT - 1
bws(i).CancelAsync()
Next
End Sub

Hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ohynes | last post by:
hey newbie here wondering if there is anyone out there who can help me im trying to make a c# subnet calulator using the IPAddress class i cant seem to figure out how to do all the subnet...
0
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info:...
0
by: scotty | last post by:
I need to do a ping scan of a subnet. I can Enum through the IPs and do this, but it takes over 5 minutes as I can only create 1 process at a time. Does anyone know how I can create multiple Ping...
3
by: JamesB | last post by:
Hmm, this doesn't seem to work. I am probably tackling it completely the wrong way, but basically I want a function to receive two IP's and a subnet mask and return True if IP1 and 2 are on the...
1
by: Terry Olsen | last post by:
Is there an "elegant" way to determine if an IP address is on my subnet? Or do I just compare the first 2 or 3 octets?
4
by: Morten Fagermoen | last post by:
Hi! I try to get the subnet location info using the code below found at http://msdn2.microsoft.com/en-us/library/system.directoryservices.activedirectory.activedirectorysubnet.location.aspx. But...
1
by: adonis | last post by:
hello. how can i ping with a specific NIC.when my computer has more than one adapter? in IPv4. thanks.
6
by: Dave Marden | last post by:
I currently use this routine in vbscript to ping computers and get the status of ping to determine whether to try to backup a machine, I am trying to change it to work with vb2003.net I am...
1
by: =?Utf-8?B?Q2FybG9z?= | last post by:
Guys, I have a little problem here. I have an AD domain with one DC and 2 other member servers and when I go to My Network Places and browse the network, I can seamless see a list of all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.