C # detect network connections (mainly local area networks)

Source: Internet
Author: User

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)
{
&

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.