using System;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
namespace LocalIP
{
class LanSearch
{ /// <summary> /// 取 MAC 地址 /// </summary> /// <param name="DestIP">目标 IP</param> /// <param name="SrcIP">源 IP</param> /// <param name="pMacAddr">MAC 地址</param> /// <param name="PhyAddrLen">MAC 地址的长度</param> /// <returns></returns>
///
[DllImport("iphlpapi.dll", ExactSpelling = true)]
private static unsafe extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
/// <summary> /// 在线程中扫描 /// </summary>
private static void LanSearchThreadMethod()
{
int i = Convert.ToUInt16(Thread.CurrentThread.Name);
Console.Write(".");
string strIP = "172.18.72." + i.ToString();
//IPHostEntry ip = null;
IPAddress ip = null;
try
{
//ip = Dns.GetHostEntry(strIP);
ip = IPAddress.Parse(strIP);
}
catch
{
//Console.WriteLine("请勿输入非法 IP 地址");
return;
}
byte[] b = new byte[6];
int len = b.Length;
//int r = SendARP(BitConverter.ToInt32(ip.AddressList[0].G etAddressBytes(), 0), 0, b, ref len);
int r = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, b, ref len);
int num = BitConverter.ToInt32(b, 0);
string mac = BitConverter.ToString(b, 0, 6);
if (num != 0)
{
//有效 MAC //Console.WriteLine("\r\n{0}--{1}--{2}", ip.AddressList[ 0].ToString(), ip.HostName, mac);
Console.WriteLine("\r\n{0}--{1}", ip.ToString(), mac);
}
}
/// <summary> /// 程序主入口 /// </summary> /// <param name="args"></param>
[STAThread]
static void Main(string[] args)
{
Thread[] thread = new Thread[255];
ThreadStart threadMethod;
for (int i = 0; i < 255; i++)
{
threadMethod = new ThreadStart(LanSearchThreadMethod);
thread[i] = new Thread(threadMethod);
thread[i].Name = i.ToString();
thread[i].Start();
if (!thread[i].Join(100))
{
thread[i].Abort();
}
}
Console.WriteLine("\r\n 按任意键返回");
Console.ReadLine();
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。