获取通过局域网连接的所有电脑的ipp地址 [英] get ipp address of all pcs connected via lan
本文介绍了获取通过局域网连接的所有电脑的ipp地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我是Haseeb,
我想在通过LAN连接的列表框中显示所有PC的IP地址.
我有一段代码,但是无法在我的计算机实验室中显示连接到LAN的PC的IP地址
Hello everyone!
I''m Haseeb,
I want to display the IPAddress of all the PC''s in a listbox that are connected via LAN.
I have a piece of code but it couldn''t display the IPAddress of PC''s connected to LAN in my computer lab
hostname = Dns.GetHostName();
IPHostEntry IPentry = Dns.GetHostEntry(hostname);
IPAddress[] addr = IPentry.AddressList;
string addres = addr[0].ToString();
foreach (IPAddress myip in IPentry.AddressList)
{
listBox1.Items.Add(myip.ToString());
}
listBox1.Items.Remove(addres);
请帮助我获取IPAddresses
我将非常感谢您.
Please help me in getting the IPAddresses
I''ll be highly thankful to you.
推荐答案
我记得使用Windows API方法,获取可以解析为IP地址的主机名.但这仅适用于我认为域中的计算机.
在这里
I remeber using windows API method , getting host names which you can resolve to IP addresses. but that will only work for computers in domain I think.
here it is
[DllImport("Netapi32.dll")]
private static extern int NetServerEnum(
IntPtr servername,
uint level,
out IntPtr bufptr,
int prefmaxlen,
out int entriesread,
out int totalentries,
uint servertype,
IntPtr domain,
IntPtr resume_handle);
以及使用它的这段代码
and this code using it
public static string[] EnumComputers()
{
IntPtr pInfo;
int entriesRead = 0;
int totalEntries = 0;
int result = NetServerEnum(
IntPtr.Zero,
LEVEL_SERVER_INFO_100,
out pInfo,
MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries,
SV_TYPE_WORKSTATION | SV_TYPE_SERVER,
IntPtr.Zero,
IntPtr.Zero);
if (result != 0)
{
throw new ApplicationException("NetApi Error = " + String.Format("0x{0,0:X}", result));
}
string[] computers = new string[entriesRead];
IntPtr pos = pInfo;
for (int ii = 0; ii < entriesRead; ii++)
{
SERVER_INFO_100 info = (SERVER_INFO_100)Marshal.PtrToStructure(pos, typeof(SERVER_INFO_100));
computers[ii] = info.sv100_name;
pos = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(SERVER_INFO_100)));
}
NetApiBufferFree(pInfo);
return computers;
}
尝试看看IPGlobalProperties类和GetActiveTcpConnections
IPGlobalProperties
这为您提供了与您自己的计算机的所有活动Tcp连接的概述.
Try look at IPGlobalProperties class and GetActiveTcpConnections
IPGlobalProperties
This gives you a overview of all active Tcp connection to your own computer.
这篇关于获取通过局域网连接的所有电脑的ipp地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文