473,765 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3239
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****@newsgro ups.nospamwrote in message
news:eo******** ******@TK2MSFTN GP04.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 BackgroundWorke r 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 GetNextIpAddres s() As String
Get
SyncLock GetType(IpProvi der)
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_COUN T - 1) As BackgroundWorke r

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

For i As Integer = 0 To THREAD_COUNT - 1
Dim bw As New BackgroundWorke r
bw.WorkerReport sProgress = True
bw.WorkerSuppor tsCancellation = True

AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChan ged, AddressOf bw_ProgressChan ged
AddHandler bw.RunWorkerCom pleted, AddressOf
bw_RunWorkerCom pleted
bw.RunWorkerAsy nc(ipp)

bws(i) = bw
Next
End Sub

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As
DoWorkEventArgs )
Dim bw As BackgroundWorke r = CType(sender, BackgroundWorke r)
e.Result = PingIp(e.Argume nt, bw, e)
End Sub

Private Sub bw_ProgressChan ged(ByVal sender As Object, ByVal e As
ProgressChanged EventArgs)
ListBox1.Items. Add(e.UserState )
End Sub

Private Sub bw_RunWorkerCom pleted(ByVal sender As Object, ByVal e As
RunWorkerComple tedEventArgs)
If (e.Error IsNot Nothing) Then
ListBox1.Items. Add(e.Error.Mes sage)
ElseIf e.Cancelled Then

Else

End If
End Sub

Private Function PingIp(ByVal ipp As IpProvider, ByVal bw As
BackgroundWorke r, ByVal e As DoWorkEventArgs ) As Boolean
While True
If bw.Cancellation Pending Then
e.Cancel = True
Return False
Else
Dim ipaddress = ipp.GetNextIpAd dress()
If ipaddress Is Nothing Then Exit While
' insert code to ping the address, here we're using Sleep()
to simulate a delay
Thread.Sleep(10 00)

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

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
For i As Integer = 0 To THREAD_COUNT - 1
bws(i).CancelAs ync()
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
19980
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 calculations now i can do them on paper but thats about it just starting to learn c sharp
0
7783
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: http://support.microsoft.com/default.aspx?scid=kb;en-us;170591 ). The problem I have is in order to perform a discovery/ping of an entire subnet (192.168.1.* for instance) I have to do a FOR loop to itterate through all of the addresses. That it seems...
0
1497
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 Processes so as to send out 255 pings at once? (sort of a reverse ping flood). FYI: using the " -n 1" switch with Ping will cause it to only send 1 ping. Basicly the code is:
3
1862
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 same subnet, otherwise false. I have got this so far: Private Function IsSameSubnet(ByVal IPSource As String, ByVal IPDest As String, ByVal IPSubnet As String) As Boolean
1
1786
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
5137
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 it only tells me that "instance" is used before it has been assigned a value. Can anyone tell me what to do to get my subnet location info? Public Property SubnetLocation() As String Get Dim instance As ActiveDirectorySubnet
1
1515
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
3459
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 wondering if anyone has a ping function they could share with me. I have done some searching on this but cannot find anything specifically for vb2003. Sub PingComputer If PingStatus(PCName) = "Success" Then
1
2144
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 workstations (my 2 member servers and my single DC) and servers. The network IP is 10.10.10.0/24 I have a second VLAN, 10.10.20.0/24 where I have another member server. I joined this member server to the domain, added a subnet in AD Sites and Services...
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.