获取客户端机器的IP地址

发布于 2024-10-27 08:49:04 字数 1863 浏览 1 评论 0原文

我们实验室有 10 台计算机,我们将所有计算机设置为连接到 LAN,以便我们 可以共享文件,我的电脑作为主电脑,我只想获取所有 连接到主计算机(即我的计算机)的计算机的 IP 地址并列出它们 我的代码是,

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "cmd ";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Arguments = "/C net view";
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    String output = p.StandardOutput.ReadToEnd();
    char[] delimiters = new char[] { '\n', '\\' };
    string[] s = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    string hostName = Dns.GetHostName();
    IPHostEntry IPHost = Dns.GetHostEntry(hostName);
    Console.WriteLine(IPHost.HostName); // Output name of web host
    IPAddress[] address = IPHost.AddressList; // get list of IP address
    // Console.WriteLine("List IP {0} :", IPHost.HostName);
    if (address.Length > 0)
    {
        for (int i = 0; i < address.Length; i++)
        {
            Console.WriteLine(address[i]);
        }
    }


    p.WaitForExit();
    int z = s.Length - 5;

    string[] str1 = new string[z];
   // int i = 0;
    char[] saperator = { ' ' };
    for (int j = 3; j < s.Length - 2; j++)
    {
        //Console.WriteLine(s[i]);
       // str1[i] = (s[j].ToString()).Split(saperator)[0];
       // Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
    }
    //Console.WriteLine(output);

    s = output.Split(new string[] { "\n,\\" }, StringSplitOptions.None);

    //Console.WriteLine(s[i]);
    //Console.WriteLine(output);
   // Console.WriteLine("IP Address : {1} ", i, AddressList[i].ToString());
    Console.ReadLine();

但我得到了我的机器的IP地址,我想要实验室中10台机器的IP地址。

we have 10 computers at lab, we set-up all computers connected to LAN so we
can share files, my computer serves as the main computer, i just want to get all
IP address of the computers connected to main computer(that is my computer) and list them
my code is

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "cmd ";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Arguments = "/C net view";
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    String output = p.StandardOutput.ReadToEnd();
    char[] delimiters = new char[] { '\n', '\\' };
    string[] s = output.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    string hostName = Dns.GetHostName();
    IPHostEntry IPHost = Dns.GetHostEntry(hostName);
    Console.WriteLine(IPHost.HostName); // Output name of web host
    IPAddress[] address = IPHost.AddressList; // get list of IP address
    // Console.WriteLine("List IP {0} :", IPHost.HostName);
    if (address.Length > 0)
    {
        for (int i = 0; i < address.Length; i++)
        {
            Console.WriteLine(address[i]);
        }
    }


    p.WaitForExit();
    int z = s.Length - 5;

    string[] str1 = new string[z];
   // int i = 0;
    char[] saperator = { ' ' };
    for (int j = 3; j < s.Length - 2; j++)
    {
        //Console.WriteLine(s[i]);
       // str1[i] = (s[j].ToString()).Split(saperator)[0];
       // Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
    }
    //Console.WriteLine(output);

    s = output.Split(new string[] { "\n,\\" }, StringSplitOptions.None);

    //Console.WriteLine(s[i]);
    //Console.WriteLine(output);
   // Console.WriteLine("IP Address : {1} ", i, AddressList[i].ToString());
    Console.ReadLine();

but i get my machine's ip address ,i want to 10 machine's ip address in lab.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-11-03 08:49:05

不传递主机名,而是传递net view的结果。

foreach (string hostName in hostNames)
{
    //string hostName = Dns.GetHostName();
    IPHostEntry entry = Dns.GetHostEntry(hostName);
    Console.WriteLine(entry.HostName); // output name of web host
    IPAddress[] addresses = entry.AddressList; // get list of IP addresses
    foreach (var address in addresses)
    {
        Console.WriteLine(address);
    }
}

Instead of passing the host name, pass the result of net view.

foreach (string hostName in hostNames)
{
    //string hostName = Dns.GetHostName();
    IPHostEntry entry = Dns.GetHostEntry(hostName);
    Console.WriteLine(entry.HostName); // output name of web host
    IPAddress[] addresses = entry.AddressList; // get list of IP addresses
    foreach (var address in addresses)
    {
        Console.WriteLine(address);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文