C # Summary of determining whether the network connection is normal

Source: Internet
Author: User

I recently worked on a c # winform project and encountered a problem in determining whether the network is connected normally. Later, we found the following methods for your reference.
1. method 1
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Windows. Forms;
Using System. Net. Sockets;
Using System. Threading;
Namespace WindowsFormsApplication1
{
Public partial class Demo: Form
{
Public Demo ()
{
InitializeComponent ();
}
// Judge
Private void btpanduan_Click (object sender, EventArgs e)
{
// 210.192.120.228 163 Netease
String ip = this.txt ip. Text. ToString ();
Int port = Convert. ToInt32 (this.txt port. Text. ToString ());
Bool a = panduan (ip, port); // 135 indicates the service port number of the local machine.
If (a = true)
{
MessageBox. Show ("the network connection is normal! ");
}
Else
{
MessageBox. Show ("this network connection is not smooth! ");
}
}

// Asynchronous call
 
// Method of judgment
Public bool panduan (string ip, int port)
{
Try
{
TcpClient client = new TcpClient (ip, port );
If (client. Connected)
{
Return true;
}
Else
{
Return false;
}
}
Catch
{
Return false;
}
}
}
}
 
2. Use the c # ping class
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Net;
Namespace WindowsFormsApplication1
{
Public partial class Demo3: Form
{
Public Demo3 ()
{
InitializeComponent ();
}
System. Net. NetworkInformation. Ping ping = new System. Net. NetworkInformation. Ping ();
System. Net. NetworkInformation. PingReply res;
// Check the network connection
Private void btcheck_Click (object sender, EventArgs e)
{

String url = this.txt url. Text. ToString ();
Bool a = check (url );
If (a = true)
{
MessageBox. Show ("connection successful! "," Prompt information ");

}
Else
{
MessageBox. Show ("connection failed! "," Prompt information ");
}
}

Public bool check (string url)
{

Try
{
Res = ping. Send (url );
If (res. Status = System. Net. NetworkInformation. IPStatus. Success)
{
Return true;
}
Else
{
Return false;
}
}
Catch {

Return false;
}
}
}
}
 


From the wanglei_smartfish Column

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.