我正在尝试这样做C#。我需要找到在我的网络中处于活动状态的所有IP地址,并将它们显示在列表中。我可以在网络中ping所有可用的(1 ... 255)IP地址。但我想让这个过程更快。查找网络中的所有IP地址
回答
请参阅This link了解异步客户端Socket以了解如何更快地ping通。
编辑:快速片段如何做到这一点:
private static void StartClient() {
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client,"This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
感谢NewAmbition。但我正在寻找一种替代方法来检测所有 –
为什么你在寻找替代品? (即使那样,你从来没有在你的问题中说过) - Pinging可以让你知道IP是否已经启动。 – TheGeekZn
如果没有像主域名注册的域名服务器/域名服务器这样的参考中心点,您将不得不联系所有候选地址并查看是否有任何域名,ping就是这样做的方法。 –
http://www.advanced-ip-scanner.com/服用。这个工具可以用来查看你的网络上有哪些ip活动。 PC使用什么类型的网络硬件。
其实我正在寻找一个解决方案,涉及编程 –
public static void NetPing()
{
Ping pingSender = new Ping();
foreach (string adr in stringAddressList)
{
IPAddress address = IPAddress.Parse(adr);
PingReply reply = pingSender.Send (address);
if (reply.Status == IPStatus.Success)
{
//Computer is active
}
else
{
//Computer is down
}
}
}
此代码在大约1秒内扫描我的网络255个D级分段。我在VB.net中编写它,并将它转换为C#(如果有任何错误,请致歉)。将其粘贴到控制台项目中并运行。根据需要修改。
注:该代码是不生产做好准备,需要在特别的情况下计算的改进(试图实现与BlockingCollection
代替TaskFactory
)。
如果结果不稳定,修改ttl(生存时间)和超时。
运行代码会给这样的结果:
Pinging 255 destinations of D-class in 192.168.1.*
Active IP: 192.168.1.100
Active IP: 192.168.1.1
Finished in 00:00:00.7226731. Found 2 active IP-addresses.
C#代码:
using System.Net.NetworkInformation;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using System;
static class Module1
{
private static List<Ping> pingers = new List<Ping>();
private static int instances = 0;
private static object @lock = new object();
private static int result = 0;
private static int timeOut = 250;
private static int ttl = 5;
public static void Main()
{
string baseIP = "192.168.1.";
Console.WriteLine("Pinging 255 destinations of D-class in {0}*", baseIP);
CreatePingers(255);
PingOptions po = new PingOptions(ttl, true);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] data = enc.GetBytes("abababababababababababababababab");
SpinWait wait = new SpinWait();
int cnt = 1;
Stopwatch watch = Stopwatch.StartNew();
foreach (Ping p in pingers) {
lock (@lock) {
instances += 1;
}
p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
cnt += 1;
}
while (instances > 0) {
wait.SpinOnce();
}
watch.Stop();
DestroyPingers();
Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result);
Console.ReadKey();
}
public static void Ping_completed(object s, PingCompletedEventArgs e)
{
lock (@lock) {
instances -= 1;
}
if (e.Reply.Status == IPStatus.Success) {
Console.WriteLine(string.Concat("Active IP: ", e.Reply.Address.ToString()));
result += 1;
} else {
//Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
}
}
private static void CreatePingers(int cnt)
{
for (int i = 1; i <= cnt; i++) {
Ping p = new Ping();
p.PingCompleted += Ping_completed;
pingers.Add(p);
}
}
private static void DestroyPingers()
{
foreach (Ping p in pingers) {
p.PingCompleted -= Ping_completed;
p.Dispose();
}
pingers.Clear();
}
}
和VB.net代码:
Imports System.Net.NetworkInformation
Imports System.Threading
Module Module1
Private pingers As New List(Of Ping)
Private instances As Integer = 0
Private lock As New Object
Private result As Integer = 0
Private timeOut As Integer = 250
Private ttl As Integer = 5
Sub Main()
Dim baseIP As String = "192.168.1."
Dim classD As Integer = 1
Console.WriteLine("Pinging 255 destinations of D-class in {0}*", baseIP)
CreatePingers(255)
Dim po As New PingOptions(ttl, True)
Dim enc As New System.Text.ASCIIEncoding
Dim data As Byte() = enc.GetBytes("abababababababababababababababab")
Dim wait As New SpinWait
Dim cnt As Integer = 1
Dim watch As Stopwatch = Stopwatch.StartNew
For Each p As Ping In pingers
SyncLock lock
instances += 1
End SyncLock
p.SendAsync(String.Concat(baseIP, cnt.ToString()), timeOut, data, po)
cnt += 1
Next
Do While instances > 0
wait.SpinOnce()
Loop
watch.Stop()
DestroyPingers()
Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result)
Console.ReadKey()
End Sub
Sub Ping_completed(s As Object, e As PingCompletedEventArgs)
SyncLock lock
instances -= 1
End SyncLock
If e.Reply.Status = IPStatus.Success Then
Console.WriteLine(String.Concat("Active IP: ", e.Reply.Address.ToString()))
result += 1
Else
'Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
End If
End Sub
Private Sub CreatePingers(cnt As Integer)
For i As Integer = 1 To cnt
Dim p As New Ping
AddHandler p.PingCompleted, AddressOf Ping_completed
pingers.Add(p)
Next
End Sub
Private Sub DestroyPingers()
For Each p As Ping In pingers
RemoveHandler p.PingCompleted, AddressOf Ping_completed
p.Dispose()
Next
pingers.Clear()
End Sub
End Module
ARP路由,您可以简单地发送所有地址的ARP请求,稍等一会,然后查看主机的ARP表格
这可能有助于
所以你只限于解决方案连接到他的子网的主机? –
这似乎是他想要做的事情,而且它也应该很快。 –
他的问题不清楚,这个解决方案提供的不止是ping –
- 1. 一个保留的IP地址:“所有0的网络地址”?
- 2. 从网络设备的IP地址列表中查找打印机IP地址
- 3. 寻找访客网络IP地址
- 4. 查找网络地址
- 5. 在Linux中列出网络中的所有活动IP地址
- 6. 在我的网络中查找IP地址
- 7. 如何在网络中查找未使用的IP地址?
- 8. 查找所有本地网络广播地址
- 9. 如何获取Java中所有网络设备的IP地址
- 10. 如何查找本地网络IP地址
- 11. 跨IP网络的IP地址
- 12. 如何在网络上查找特定桌面的IP地址?
- 13. 从网络上的IP地址查找特定计算机
- 14. 查找IP地址
- 15. 获取IP地址的所有主机名的网络
- 16. 在Python中的IP地址网络3.3
- 17. 获取网络上的所有IP和MAC地址
- 18. 在FreeBSD上枚举IP地址的所有网络接口
- 19. 本地网络HTTPS的IP地址
- 20. 如何在没有IP地址的网络上找到设备
- 21. 查找默认网络打印机IP地址
- 22. 使用Python查找网络(外部)IP地址
- 23. 通过ping网关IP地址查找
- 24. 按IP地址查找网卡
- 25. 在Centos中查找所有访问者的IP地址
- 26. 在Android中获取网络IP地址
- 27. 如何从iPhone中找到网络服务的IP地址
- 28. 计算网络的IP地址
- 29. 网络计算机的IP地址
- 30. 修改IP和MAC-地址的网络
如果你想更快,平同时所有IP:'Ping.SendAsync'。 – igrimpe
我刚刚运行了pinging命令,就像这个 'for/l%n在(1,1,255)中执行ping 192.168.10。%x' with process.start()然后再读取arp表项查找每个ip地址在网络中。但是这非常慢。需要做到这一点。 –
@MehbubeArman,网络中有多少IP地址,什么是“足够快” –