Kingdz is changed to cainiao. A daily C # Small instance-C # determines whether the network is connected.

Source: Internet
Author: User

Today, writeCodeThere are few examples.ArticleHaha, but I still want to write it. Is it a cainiao? The change is complete. Hey. Today I found a friend's article interesting. Let's show you how

I finally know why China Mobile is better than China Unicom!
1. Right-click on the desktop and choose new-Text Document;
2. Open "New Text Document", enter "move", and save the disk and exit;
3. Open "New Text Document" again. What do you see?
4. Is the word "mobile" just entered?

Let's change it over:

1. Right-click on the desktop and choose new-Text Document;
2. Open "New Text Document", enter "Unicom", and save the disk and exit;
3. Open "New Text Document" again. What do you see?
4. Is the word "Unicom" just entered missing? Instead, it looks like a burning cell phone battery?

I only know why China Unicom is not so powerful today, haha. Okay. Let's get started with our small instance.

How can I use C # To determine whether the network is connected? There are n methods. We will try to explain them as completely as possible.

The first method is the simplest, but not accurate.

Private voidBtnisconnect_click (ObjectSender,EventargsE ){If(Systeminformation. Network ){MessageBox. Show ("Connected, but not sure whether it is Internet or LAN");}}

This method is also true under the LAN

 

The second method is to Ping An address, so we will adopt Baidu. If Baidu is down, wow, it will not blame anyone. Haha

First, reference the namespace

 
UsingSystem. net. networkinformation;

Then, let's simply implement it. This kind of implementation is not perfect, and we are gradually improving it,

Private voidBtnping_click (ObjectSender,EventargsE ){PingPing =NewPing();PingreplyPR = ping. Send ("Baidu.com");If(PR. Status = ipstatus. Success)MessageBox. Show ("Networking status");ElseMessageBox. Show ("Your network cable has fallen");}
 
Ping. Send ("Baidu.com"); remember that sending has eight overload methods. You can adjust them by yourself.
 
Let's get a Google IP address first.
 
 
 
 
 
Start now.
Here we mainly prepare our data based on the send parameter. Some people also refer to this as a heartbeat packet, which is to constantly send requests and determine whether the network is connected.
 
PingPing =NewPing();PingoptionsPoptions =NewPingoptions(); Poptions. dontfragment =True;StringData =String. Empty;Byte[] Buffer =Encoding. ASCII. getbytes (data );

Prepare data in place and start sending

 

IntTimeout = 1200;PingreplyReply = ping. Send (IPaddress. Parse ("74.125.71.104"), Timeout, buffer, poptions );If(Reply. Status = ipstatus. Success)MessageBox. Show ("Networking status");ElseMessageBox. Show ("Your network cable has fallen");

Haha, okay. Test it.

 
 
 
Hey, this is much more stable.
 
Of course, another way is to call the system API,
[Dllimport("Wininet")]Private extern static boolInternetgetconnectedstate (Out intConnectiondescription,IntReservedvalue );
 
The usage address of the above function is given.
 
Http://msdn.microsoft.com/en-us/library/windows/desktop/aa384702%28v=vs.85%29.aspx
 
IntI = 0;If(Internetgetconnectedstate (OutI, 0 ))MessageBox. Show ("Networking status");ElseMessageBox. Show ("Your network cable has fallen");
 
 
 
Haha. Someone in the previous instance asked about the verification code judgment method. Hey hey, there's a suspense. I'll give you an example to see the effect.
 
 
Code: isinternet.rar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.