C # I have not seen any good method for detecting network connection problems. It is detected by transmitting information with the Internet (or LAN server.
Let me take a look.
Code:
Private void button#click (object sender, EventArgs e)
{
String ip;
Ip = "10.1.148.1 ";
// String ip = "192.192.132.229 ";
// String strRst = dig Ping (ip );
// MessageBox. Show (strRst );
String str = CmdPingh (ip );
MessageBox. Show (str );
}
Private static string strIp Ping (string strIp) // method 1
{
Process p = new Process ();
P. StartInfo. FileName = "cmd.exe ";
P. StartInfo. UseShellExecute = false;
P. StartInfo. RedirectStandardInput = true;
P. StartInfo. RedirectStandardOutput = true;
P. StartInfo. RedirectStandardError = true;
P. StartInfo. CreateNoWindow = true;
String pingrst;
P. Start ();
P. StandardInput. WriteLine ("ping-n 1" + strIp );
P. StandardInput. WriteLine ("exit ");
String strRst = p. StandardOutput. ReadToEnd ();
If (strRst. IndexOf ("(0% loss )")! =-1)
Pingrst = "connection ";
Else if (strRst. IndexOf ("Destination host unreachable .")! =-1)
Pingrst = "unable to reach the target host ";
Else if (strRst. IndexOf ("Request timed out .")! =-1)
Pingrst = "timeout ";
Else if (strRst. IndexOf ("Unknown host ")! =-1)
Pingrst = "unable to Resolve Host ";
Else
Pingrst = strRst;
P. Close ();
Return pingrst;
}
Public static string CmdPingh (string _ strHost) // same as the preceding method, it is different in writing.
{
String m_strHost = _ strHost;
Process process = new Process ();
Process. StartInfo. FileName = "cmd.exe ";
Process. StartInfo. UseShellExecute = false;
Process. StartInfo. RedirectStandardInput = true;
Process. StartInfo. RedirectStandardOutput = true;
Process. StartInfo. RedirectStandardError = true;
Process. StartInfo. CreateNoWindow = true;
String pingrst = string. Empty;
Process. StartInfo. Arguments = "ping" + m_strHost + "-n 1 ";
Process. Start ();
Process. StandardInput. AutoFlush = true;
String temp = "ping" + m_strHost + "-n 1 ";
Process. StandardInput. WriteLine (temp );
Process. StandardInput. WriteLine ("exit ");
String strRst = process. StandardOutput. ReadToEnd ();
If (strRst. IndexOf ("(0% loss )")! =-1)
Pingrst = "connection ";
Else if (strRst. IndexOf ("Destination host unreachable .")! =-1)
Pingrst = "unable to reach the target host ";
Else if (strRst. IndexOf ("Request timed out .")! =-1)
Pingrst = "timeout ";
Else if (strRst. IndexOf ("Unknown host ")! =-1)
Pingrst = "unable to Resolve Host ";
Else
Pingrst = strRst;
Process. Close ();
Return pingrst;
}
Private void button2_Click (object sender, EventArgs e)
{
Jcip ();
}
Private void jcip () // method 2
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// But change the fragmentation behavior.
Options. DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ";
Byte [] buffer = Encoding. ASCII. GetBytes (data );
Int timeout = 120;
PingReply reply = pingSender. Send ("10.1.148.1", timeout, buffer, options );
If (reply. Status = IPStatus. Success)
{
&