dipps
Goto Top

C Sharp Ping ausführen und bearbeiten

C# Ping ausführen und bearbeiten

Hallo,
ich möchte mit meinem C# Programm einen Ping ausführen und dann mit dem ergebniss erfolgreich bzw nicht erfolgreich(Zielhost nicht gefunden) weiter arbeiten wie kann ich das realisieren das ich ping mache und erkenne ob ziel gefunden oder nicht?

Content-Key: 101064

Url: https://administrator.de/contentid/101064

Printed on: April 24, 2024 at 17:04 o'clock

Member: pulse
pulse Nov 05, 2008 at 10:04:29 (UTC)
Goto Top
Member: Dipps
Dipps Nov 05, 2008 at 11:08:06 (UTC)
Goto Top
Danke mein Code geht jetzt so
...
private void Pruefen_Tick(object sender, eventArgs e)
{

if(ip_s1.Text != "" && ip_s1.Text !=" ")  
{
Ping pingSender = new Ping();
PingOptions options= new PingOptions();

options.DontFragment = true;
string data="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";  
byte buffer = Encoding.ASCII.GetBytes(data);
int Timeout = 1000;
PingReplay replay= pingSender.Send(ip_s1.Text,timeout,buffer,options);
if(replay.Status == IPStatus.Success)
{
led1.color= color.Lime;
}
else
{
led1.color= color.red;
}
}
else
{
led1.color= color.gray;
}

if(ip_s2.Text != "" && ip_s2.Text !=" ")  
{
Ping pingSender = new Ping();
PingOptions options= new PingOptions();

options.DontFragment = true;
string data="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";  
byte buffer = Encoding.ASCII.GetBytes(data);
int Timeout = 1000;
PingReplay replay= pingSender.Send(ip_s2.Text,timeout,buffer,options);
if(replay.Status == IPStatus.Success)
{
led2.color= color.Lime;
}
else
{
led2.color= color.red;
}
}
else
{
led2.color= color.gray;
}

... bis 11


}

das ist ja aber keine saubere lösung
deshalb wollte ich es so machen

...
private void Pruefen_Tick(object sender, eventArgs e)
{
for(int i=1;i<=11;i++)
{
if(ip_si.Text != "" && ip_si.Text !=" ")  
{
Ping pingSender = new Ping();
PingOptions options= new PingOptions();

options.DontFragment = true;
string data="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";  
byte buffer = Encoding.ASCII.GetBytes(data);
int Timeout = 1000;
PingReplay replay= pingSender.Send(ip_si.Text,timeout,buffer,options);
if(replay.Status == IPStatus.Success)
{
ledi.color= color.Lime;
}
else
{
ledi.color= color.red;
}
}
else
{
ledi.color= color.gray;
}
}
}

so das er mir immer die aktuelle zahl schreiben soll bloß schreibt er wirklich das i und nicht die zahl wie kann ich das machen das er mir in den namen die i zahl schreibt?
Member: -Ohforf
-Ohforf Nov 05, 2008 at 13:08:29 (UTC)
Goto Top
Welche i Zahl meinst du denn?
Die Laufvariable aus dem Schleifenbefehl?!

Oder willst du die Latenz ausgegeben bekommen?!

Du musst da schon etwas genauer werden,
sonst kann ich dir nicht helfen.

Gruß,
Niklas
Member: Dipps
Dipps Nov 05, 2008 at 13:58:43 (UTC)
Goto Top
Naja meine textfelder heißen
ip_s1,ip_s2,ip_s3,...ip_s11

nun will ich das i aus der schleife benutzen um das jeweilige textfeld anzusprechen sprich bei i=1 -> ip_1 bei i=5 ip_s5
Member: -Ohforf
-Ohforf Nov 05, 2008 at 15:25:22 (UTC)
Goto Top
Würde ich über ein Array lösen...

In etwa so:

string strServer;

private void populateServerList()
{
strServer = new string[11];

strServer = ip_1;
strServer[1] = ip_2;
strServer[2] = ip_3;


//... fortführen
}

und dann als schleife:

int intLength = strServer.Length;

for (int i = 0; i >= intLength; i++)
{
Ping pingSender = new Ping();
PingOptions options= new PingOptions();

options.DontFragment = true;
string data="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";  
byte buffer = Encoding.ASCII.GetBytes(data);
int Timeout = 1000;
PingReplay replay= pingSender.Send(strServer[i],timeout,buffer,options);

//...
}

Der springende Punkt hierbei ist:
pingSender.Send(strServer[i],timeout,buffer,options);

Evtl. gibt es hierbei eine IndexOutOfRange Exception,
da der erste Eintrag im strServer-Array den Index 0
und der letzte dem entsprechend den Index 10 hat.

Gruß,
Niklas
Member: Dipps
Dipps Nov 05, 2008 at 15:43:07 (UTC)
Goto Top
und wie mache ich das bei den led?

da habe ich ja auch led1.ColorOff = Color. ....
led2.coloroff = Color. ....
...
Member: -Ohforf
-Ohforf Nov 08, 2008 at 13:11:33 (UTC)
Goto Top
Zitat von @Dipps:
und wie mache ich das bei den led?


Da fällt mir spontan nichts überzeugendes ein,
ausser vielleicht ein led-Array.

Mit Objektarrays habe ich aber nur einmal gearbeitet.

Eventuell solltest du dir überlegen auf eine Tabellenansicht
mit dem DataGridView zurückzugreifen.
Member: Dipps
Dipps Nov 11, 2008 at 07:19:15 (UTC)
Goto Top
Habe ich schon versucht klappt aber nicht

System.Drawing.Color ledm;
ledm = System.Darwing.Color[12];
led[1] = led1.ColorOff;
led[2] = led2.ColorOff;
led[3] = led3.ColorOff;
...
led[10] = led10.ColorOff;
led[11] = led11.ColorOff;
led1 ... led11 sind vom Objekt Steve.GraphicControls typ bloß mit dem typen geht es auch nicht.