I am constructing a function pinging all the addresses in my network. I have set the timeout be 15 mil seconds, yet each ping takes around 4 to 6 seconds for a reply. Imagine, 4 to 6 seconds loop 254 times. It almost took you half an hour to complete a broadcast ping.
Even worse, when it reaches an address that is occupied by a device, it stucks! the program can't move on anymore. No more debugging message.
I try manually type a for-loop command in cmd for /l %i in (2,1,255) do ping 192.168.137.%i -n 1 -w 15 | find /I "TTL"
, all the process takes not more than 2 minutes.
*All those things are under a sub-thread, so it will not block the main program.
public IPAddress[] GetAllConnectedAddressInVirtualNetwork () {
List<IPAddress> ipList = new List<IPAddress> ();
string networkAddress = GetHotspotDefaultAddress (); //e.g. 192.168.137.1
string networkPrefix = networkAddress.Substring (0, networkAddress.Length - 1); //e.g. 192.168.137.
UnityEngine.Debug.Log("Start scanning in "+networkPrefix+"x");
//Scanning from x.x.x.2 to x.x.x.255
for (int i = 2; i <= 255; i++) {
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping ();
PingReply pingReply = pingSender.Send (networkPrefix + i.ToString (), pingTimeout, Encoding.Default.GetBytes(""));
if (pingReply.Status == IPStatus.Success) {
ipList.Add (pingReply.Address);
UnityEngine.Debug.Log (pingReply.Address.ToString () + " added to list.");
} else {
UnityEngine.Debug.LogError (pingReply.Status.ToString ());
}
UnityEngine.Debug.Log ("pinged to "+networkPrefix + i.ToString ());
}
return ipList.ToArray();
}