Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取连接到网络的计算机列表?_C#_.net_Networking - Fatal编程技术网

C# 如何获取连接到网络的计算机列表?

C# 如何获取连接到网络的计算机列表?,c#,.net,networking,C#,.net,Networking,可能重复: 我正在创建一个应用程序,您可以在网络上的计算机之间发送信息。计算机侦听连接,并可以将信息发送到网络上的另一台计算机。我需要知道如何获取计算机所连接网络上的主机名或IP地址列表。我不知道从何处获得此代码。 最后一段代码来自我(getIPAddress()) 它读取您的ip以获取网络的基本ip 学分归作者所有: static void Main(string[] args) { string ipBase = getIPAddress(); st

可能重复:


我正在创建一个应用程序,您可以在网络上的计算机之间发送信息。计算机侦听连接,并可以将信息发送到网络上的另一台计算机。我需要知道如何获取计算机所连接网络上的主机名或IP地址列表。

我不知道从何处获得此代码。 最后一段代码来自我(getIPAddress())

它读取您的ip以获取网络的基本ip

学分归作者所有:

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;
    }
static void Main(字符串[]args)
{
字符串ipBase=getIPAddress();
字符串[]ipParts=ipBase.Split('.');
ipBase=ipParts[0]+“+ipParts[1]+”+ipParts[2]+”;
对于(int i=1;i<255;i++)
{
字符串ip=ipBase+i.ToString();
Ping p=新Ping();
p、 PingCompleted+=新的PingCompletedEventHandler(p_PingCompleted);
p、 SendAsync(ip,100,ip);
}
Console.ReadLine();
}
静态无效p_PingCompleted(对象发送方,PingCompletedEventArgs e)
{
字符串ip=(字符串)e.UserState;
if(e.Reply!=null&&e.Reply.Status==IPStatus.Success)
{
如果(解析名称)
{
字符串名;
尝试
{
IPHostEntry=Dns.GetHostEntry(ip);
name=hostEntry.HostName;
}
捕获(SocketException例外)
{
name=“?”;
}
WriteLine({0}({1})已启动:({2}ms)”,ip,name,e.Reply.RoundtripTime);
}
其他的
{
WriteLine({0}已启动:({1}ms)”,ip,e.Reply.RoundtripTime);
}
锁(lockObj)
{
upCount++;
}
}
else if(e.Reply==null)
{
WriteLine(“ping{0}失败。(空回复对象?),ip);
}
}
公共静态字符串getIPAddress()
{
IPHostEntry宿主;
字符串localIP=“”;
host=Dns.GetHostEntry(Dns.GetHostName());
foreach(主机地址列表中的ip地址)
{
if(ip.AddressFamily==AddressFamily.InterNetwork)
{
localIP=ip.ToString();
}
}
返回本地点;
}

请显示一些源代码。。。你试过什么?你在一个领域吗?看看这个:或者你能不能只ping-b?我不在域中。。。当我在命令提示符中输入“ping-b”时,它会显示“坏选项-b”。谢谢!这是一个非常好的实现,但是如果计算机没有连接到网络(或者没有连接到互联网或者直接连接到互联网),有没有办法让它抛出错误?我想你会在这里找到你想要的:谢谢。。。我会利用这个…:)