如何获得连接到网络的计算机的列表? [英] How can I get a list of computers connected to the network?
本文介绍了如何获得连接到网络的计算机的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
获取网络上所有计算机的列表/o DNS
我正在创建一个应用程序,您可以在其中在网络上的计算机之间发送信息.这些计算机侦听连接,并可以将信息发送到网络上的另一台计算机.我需要知道如何获取计算机连接到的网络上的主机名或IP地址的列表.
I am creating an application where you can send information between computers on a network. The computers listen for connections and can send information to another computer that is on the network. I need to know how to get a list of hostnames or IP addresses on the network the computer is connected to.
推荐答案
我不知道我从哪里得到了这段代码.最后一段代码来自我(getIPAddress())
I don't know where I got this code from anymore. The last piece of code is from me (getIPAddress())
它会读取您的IP以获取网络的基本IP.
It reads your ip to get the base ip of the network.
信用归作者所有:
static void Main(string[] args)
{
string ipBase = getIPAddress();
string [] ipParts = ipBase.Split('.');
ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";
for (int i = 1; i < 255; i++)
{
string ip = ipBase + i.ToString();
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
p.SendAsync(ip, 100, ip);
}
Console.ReadLine();
}
static void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
string ip = (string)e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
if (resolveNames)
{
string name;
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ip);
name = hostEntry.HostName;
}
catch (SocketException ex)
{
name = "?";
}
Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);
}
else
{
Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
}
lock (lockObj)
{
upCount++;
}
}
else if (e.Reply == null)
{
Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
}
}
public static string getIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
这篇关于如何获得连接到网络的计算机的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文