Ev güvenlik uygulaması geliştiriyorum. Yapmak istediğim bir şey, evde olup olmadığına göre otomatik olarak kapanıyor. Evdeyken ağa otomatik olarak bağlanan Kablosuz bir telefonum var.Ping veya ağdaki bir cihaza ağda C olup olmadığını söyleyin C#
Telefon, adresini DHCP aracılığıyla bağlar ve alır. Statik bir IP kullanacak şekilde yapılandırabilseydim, tercih etmemekteyim. C#/.Net'te bir cihazın MAC adresini alıp ağ üzerinde etkin olup olmadığını söyleyen herhangi bir 'Ping' veya eşdeğeri var mı?
Düzeltme: Netleştirmek için, aynı LAN üzerinde telefonu algılayabilmeyi istediğim bir PC üzerinde yazılımı çalıştırıyorum.
Düzeltme: Buraya geldiğim kod, spoulson'un yardımı sayesinde. İlgilendiğim telefonların evde olup olmadığını güvenilir bir şekilde algılar.
private bool PhonesInHouse()
{
Ping p = new Ping();
// My home network is 10.0.23.x, and the DHCP
// pool runs from 10.0.23.2 through 10.0.23.127.
int baseaddr = 10;
baseaddr <<= 8;
baseaddr += 0;
baseaddr <<= 8;
baseaddr += 23;
baseaddr <<= 8;
// baseaddr is now the equivalent of 10.0.23.0 as an int.
for(int i = 2; i<128; i++) {
// ping every address in the DHCP pool, in a separate thread so
// that we can do them all at once
IPAddress ip = new IPAddress(IPAddress.HostToNetworkOrder(baseaddr + i));
Thread t = new Thread(() =>
{ try { Ping p = new Ping(); p.Send(ip, 1000); } catch { } });
t.Start();
}
// Give all of the ping threads time to exit
Thread.Sleep(1000);
// We're going to parse the output of arp.exe to find out
// if any of the MACs we're interested in are online
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "-a";
psi.FileName = "arp.exe";
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
bool foundone = false;
using (Process pro = Process.Start(psi))
{
using (StreamReader sr = pro.StandardOutput)
{
string s = sr.ReadLine();
while (s != null)
{
if (s.Contains("Interface") ||
s.Trim() == "" ||
s.Contains("Address"))
{
s = sr.ReadLine();
continue;
}
string[] parts = s.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
// config.Phones is an array of strings, each with a MAC
// address in it.
// It's up to you to format them in the same format as
// arp.exe
foreach (string mac in config.Phones)
{
if (mac.ToLower().Trim() == parts[1].Trim().ToLower())
{
try
{
Ping ping = new Ping();
PingReply pingrep = ping.Send(parts[0].Trim());
if (pingrep.Status == IPStatus.Success)
{
foundone = true;
}
}
catch { }
break;
}
}
s = sr.ReadLine();
}
}
}
return foundone;
}
Bu soru, size yardımcı olabilir http://stackoverflow.com/questions/1148778/how-do-i-access-arp-protocol-information-through-net. MAC'ı bir IP'den almanıza yardımcı olacaktır. Veya "arp -a" kullanın ve çıktıyı ayrıştırın. –
Bunu başka şekilde yapabilir misin? Telefonunuzda olduğu gibi, ev ağında ne zaman olduğunu algılayan bir uygulama var ve daha sonra uygulama alanı içinde olduğunu bildiriyor? Genellikle, bağlantı kurulduğunda sizi bilgilendirmek için telefonu alabilirsiniz. – jvilalta
Bu, komut dosyasının son hali miydi? – cDima