It is actually quite simple. ping a few machines under cmd.exe and run the arp-a command to check whether this method is simpler and more effective than the multi-thread round-robin scanning method.
// Ping the host of the network segment cyclically.
// Use the following functions to obtain the list of IP addresses with responses in the LAN.
Public static ArrayList GetAllLocalMachines ()
{
Process p = new Process ();
P. StartInfo. FileName = "cmd.exe ";
P. StartInfo. UseShellExecute = false;
P. StartInfo. RedirectStandardInput = true;
P. StartInfo. RedirectStandardOutput = true;
P. StartInfo. RedirectStandardError = true;
P. StartInfo. CreateNoWindow = true;
P. Start ();
P. StandardInput. WriteLine ("arp-");
P. StandardInput. WriteLine ("exit ");
ArrayList list = new ArrayList ();
StreamReader reader = p. StandardOutput;
String IPHead = Dns. GetHostByName (Dns. GetHostName (). AddressList [0]. ToString (). Substring (0, 3 );
For (string line = reader. ReadLine (); line! = Null; line = reader. ReadLine ())
{
Line = line. Trim ();
If (line. StartsWith (IPHead) & (line. IndexOf ("dynamic ")! =-1 ))
{
String IP = line. Substring (0, 15). Trim ();
String Mac = line. Substring (line. IndexOf ("-")-2, 0x11). Trim ();
LocalMachine localMachine = new LocalMachine ();
LocalMachine. MachineIP = IP;
LocalMachine. MachineMAC = Mac;
LocalMachine. MachineName = "";
List. Add (localMachine );
}
}
Return list;
}