Hallo, also ich bin noch recht unvertraut mit der Sprache C#
Benötige für mein Programm folgendes:
Auf Buttonklick soll das Programm eine IP-Adresse (Internet)
anpingen. Sollte er keine Antwort bekommen (Internet ausgeschaltet),
soll in einem "Label" die Fehlermeldung als Text erscheinen -> Keine Verbingung erscheinen.
Kann mir jemand bitte helfen?
Schöne Grüße und vielen dank.
👶-> :]-> 8o-> 🙂
Such mal in der MSDN nach ping, denn dafür gibts eine fertige Methode...
Nein, funktioniert auch problemlos in Forms-Anwendungen, habe es ja selbst schon verwendet...
Sry, stimmt ...
Habe grad ein paar Codezeilen gefunden, geht irgendwie nicht.
Naja ich schau mal was ich hinbekomme
👶-> :]-> 8o-> 🙂
ne klappt alles nicht, hab davon zu wenig ahnung.
PS: Was willlst du mir mit dem GUI Link sagen?
👶-> :]-> 8o-> 🙂
Hallo RPlaner,
also erstmal: Ich konnte ja bisher nur raten, warum du der Meinung bist, dass es in Windows.Forms.Anwendungen nicht geht. Siehe [Hinweis] Wie poste ich richtig? Punkt 5. Eine mögliche Vermutung ist, dass du dieser Meinung bist, es ginge nicht, weil dein GUI blockiert. Und wie man dieses Problem löst, steht in dem Thread.
Wenn gute Fragen stellst, wirst du gute Antworte bekommen. 🙂 Bei unverständlichen Fragen, gibt es unverständliche Antworten. 🙂
herbivore
Ok 😁
Also ich bin wiegesagt einsteiger.
Ich benötige folgendes Befehl:
Bei Klick auf einen Button soll eine IP angepingt werden,
sollte dies erfolgreich sein, soll garnichts passieren.
Sollte jedoch keine Antwort kommen,
so soll in einem Labelfeld eine Fehlermeldung erscheinen.
Ich bräuchte einen Code-Ansatz,
weiß nicht wie ich anfangen soll.
Dankeschön
👶-> :]-> 8o-> 🙂
Auszug aus der MSDN (1. Eintrag bei Ping):
using System;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main (string[] args)
{
if (args.Length == 0)
throw new ArgumentException ("Ping needs a host or IP Address.");
string who = args[0];
AutoResetEvent waiter = new AutoResetEvent (false);
Ping pingSender = new Ping ();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
// Wait 12 seconds for a reply.
int timeout = 12000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);
Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(who, timeout, buffer, options, waiter);
// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}
public static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set ();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
DisplayReply (reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
public static void DisplayReply (PingReply reply)
{
if (reply == null)
return;
Console.WriteLine ("ping status: {0}", reply.Status);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}
Jopp danke, das habe ich schon getestet.
Bekomme jedoch den Fehler
"Fehler 2 Der Typ- oder Namespacename "PingReply" konnte nicht gefunden werden. (Fehlt eine using-Direktive oder ein Assemblyverweis?)"
Habe nichts am Code geändert ...
👶-> :]-> 8o-> 🙂
Hast du den Code 1:1 kopiert?
Wenn ja stimmt wohl an deiner IDE/SDK was nicht, denn der sollte problemlos funktionieren und tut es bei mir auch...
Hallo RPlaner,
und:
Fehlt eine using-Direktive oder ein Assemblyverweis?
Welches using (= Namespace) und welche Assembly (=DLL) findest du in der :rtfm: Doku in der Beschreibung der PingReply-Klasse.
herbivore
In dem C#-Network Tutorial hier findest Du auch ein Ping Bsp:
http://www.codeplanet.eu/modules/tutoria....php?storyid=11