/// <summary> /// Sends discovery packets to all servers on the local network /// </summary> public void DiscoverServers() { // Discover local peers :D myPeer.DiscoverLocalPeers(NetSettings.DEFAULT_SERVER_PORT); myPeer.DiscoverLocalPeers(NetSettings.DEFAULT_SERVER_PORT + 1); myPeer.DiscoverLocalPeers(NetSettings.DEFAULT_SERVER_PORT + 2); myPeer.DiscoverLocalPeers(NetSettings.DEFAULT_SERVER_PORT + 3); try { // Get the IP range to ping IPSegment segment = new IPSegment(NetUtils.GetGateway(myAddress).ToString(), NetUtils.GetSubnetMask(myAddress).ToString()); // Get an enumerable result IEnumerable <uint> hosts = segment.Hosts(); // Iterate and ping each one foreach (uint host in hosts) { myPeer.DiscoverKnownPeer(host.ToIpString(), NetSettings.DEFAULT_SERVER_PORT); } } catch (Exception e) { Logger.Write("Failed to ping local servers"); MessageBox.Show("Failed to discover local servers, please make sure you don't have any weird networking", "Whoops", MessageBoxButtons.OK); } }
private void GetHosts() { ips.Clear(); //Clear Hosts List if (IpHelpers.isValidIP(tbIPStart.Text) == true && IpHelpers.isValidIP(tbIPEnd.Text) && cbUseSubnet.Checked == false) { string ipStart = tbIPStart.Text; string ipEnd = tbIPEnd.Text; string ipNet = IPFirstPart(ipStart); int ipStartSub = Convert.ToInt32(IPLastPart(ipStart)); int ipEndSub = Convert.ToInt32(ipEnd); if (ipStartSub <= ipEndSub) { for (int i = ipStartSub; i <= ipEndSub; i++) { IPAddress newIP = IPAddress.Parse(ipNet + i.ToString()); Host newHost = new Host(); newHost.IP_Address = newIP; newHost.CountChecks = 0; newHost.RunningCheck = false; ips.Add(newHost); } } } if (IpHelpers.isValidIP(tbIPStart.Text) == true && cbUseSubnet.Checked == true) { foreach (var host in ipSeg.Hosts()) { if (host >= IpHelpers.ParseIp(firstIP) && host <= IpHelpers.ParseIp(lastIP)) { //Prevent ip .0 and .255 in List if (Convert.ToInt32(IPLastPart(host.ToIpString())) > 0 && Convert.ToInt32(IPLastPart(host.ToIpString())) < 255) { IPAddress newIP = IPAddress.Parse(host.ToIpString()); Host newHost = new Host(); newHost.IP_Address = newIP; newHost.CountChecks = 0; newHost.RunningCheck = false; ips.Add(newHost); } } } } }