C#获取指定IP的主机名
2011-06-19 09:56
435 查看
以下代码在visiual studio 2003下调试通过。
方法一:使用GetHostByAddress函数
string mystartIP = "222.206.84."; // IP地址
string myip;
int s;
string name;
for(s = 1; s <= 125; s++) //IP地址开始和结束
{
myip = mystartIP + s.ToString();
//转换IP地址
IPAddress myscanip = IPAddress.Parse(myip);
try
{
IPHostEntry myscanhost = Dns.GetHostByAddress(myscanip);
name = myscanhost.HostName.ToString();
this.listBox1.Items.Add(myip + " " + name);
}
catch
{
}
方法二:开启进程,调用nbtstat命令,通过分析该命令的执行结果获得指定IP的主机名。
public static string GetRemoteHostByNetBIOS(string clientIP)
{
string ip = clientIP;
string dirResults = "";
char[] hostResult;
string Result;
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat.exe";
psi.Arguments = "-A " + ip;
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardError = true;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
hostResult = new char[(dirResults.IndexOf("<")-dirResults.IndexOf("-/r/r/n")-8)]; //后面4个空格加4个"-/r/r/n"字符
dirResults.CopyTo(dirResults.IndexOf("-/r/r/n") + 8, hostResult, 0, dirResults.IndexOf("<") - dirResults.IndexOf("-/r/r/n") - 8);
Result = new string(hostResult);
return Result;
}
转自:http://ttgoup.blog.hexun.com/14750646_d.html
方法一:使用GetHostByAddress函数
string mystartIP = "222.206.84."; // IP地址
string myip;
int s;
string name;
for(s = 1; s <= 125; s++) //IP地址开始和结束
{
myip = mystartIP + s.ToString();
//转换IP地址
IPAddress myscanip = IPAddress.Parse(myip);
try
{
IPHostEntry myscanhost = Dns.GetHostByAddress(myscanip);
name = myscanhost.HostName.ToString();
this.listBox1.Items.Add(myip + " " + name);
}
catch
{
}
方法二:开启进程,调用nbtstat命令,通过分析该命令的执行结果获得指定IP的主机名。
public static string GetRemoteHostByNetBIOS(string clientIP)
{
string ip = clientIP;
string dirResults = "";
char[] hostResult;
string Result;
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat.exe";
psi.Arguments = "-A " + ip;
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardError = true;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
hostResult = new char[(dirResults.IndexOf("<")-dirResults.IndexOf("-/r/r/n")-8)]; //后面4个空格加4个"-/r/r/n"字符
dirResults.CopyTo(dirResults.IndexOf("-/r/r/n") + 8, hostResult, 0, dirResults.IndexOf("<") - dirResults.IndexOf("-/r/r/n") - 8);
Result = new string(hostResult);
return Result;
}
转自:http://ttgoup.blog.hexun.com/14750646_d.html
相关文章推荐
- C#获取指定IP的主机名
- C#通过ARP获取指定IP的MAC地址
- C#编程之 如何获取本地和远程主机的IP及MAC地址
- 求教C#使用(SnmpSharpNet.dll)Snmp协议,如何获取整个局域网段内的IP设备类型(例PC、路由器等)MAC和主机名?
- C#获取IP和主机名
- C#编程之 如何获取本地和远程主机的IP及MAC地址(转)
- 获取指定名称主机的IP
- C#通过ARP获取指定IP的MAC地址
- C# 根据局域网IP获取主机名称
- C#编程之 如何获取本地和远程主机的IP及MAC地址
- C# 获取客户端i,客户端主机ip
- C#编程之 如何获取本地和远程主机的IP及MAC地址
- 【转】C# 获取主机IP,Mac
- c#获取远程主机IP
- 主机上有多个网卡用java指定获取某一个网卡ip的问题
- C#获得客户端的IP,主机名 获取当前时间
- C#获取HTML文件指定DIV内容
- C#获取IP及MAC地址
- C#获取局域网ip段
- c# 通过发送arp包获取ip等信息