.NET में इंटरनेट कनेक्टिविटी की जांच करने का सबसे तेज़ और सबसे प्रभावी तरीका क्या है?
Windows NLM API
इसके लिए सबसे अच्छा होना चाहिए। stackoverflow.com/questions/5405895/…
.NET में इंटरनेट कनेक्टिविटी की जांच करने का सबसे तेज़ और सबसे प्रभावी तरीका क्या है?
Windows NLM API
इसके लिए सबसे अच्छा होना चाहिए। stackoverflow.com/questions/5405895/…
जवाबों:
कुछ इस तरह से काम करना चाहिए।
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (client.OpenRead("http://google.com/generate_204"))
return true;
}
catch
{
return false;
}
}
I cannot image a world where www.google.com does not return some HTML
चीन में उदाहरण के लिए ...
अगर कोई इंटरनेट कनेक्शन है या नहीं, तो आप पूरी तरह से जांच कर सकते हैं कि कोई तरीका नहीं है (मुझे लगता है कि आप इंटरनेट तक पहुंच का मतलब है)।
हालाँकि, आप ऐसे संसाधनों का अनुरोध कर सकते हैं, जो वस्तुतः ऑफ़लाइन नहीं हैं, जैसे google.com या कुछ इसी तरह का। मुझे लगता है कि यह कुशल होगा।
try {
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
return (reply.Status == IPStatus.Success);
}
catch (Exception) {
return false;
}
8.8.8.8
या 8.8.4.4
) मेरे लिए ठीक काम करता है।
Beware - many schools and offices block the ping protocol.
यदि आप इस पद्धति का उपयोग किसी ऐसे अनुप्रयोग के लिए कर रहे हैं जो क्लाइंट द्वारा उपयोग किया जाएगा तो मैं इंटरनेट की जाँच करने की इस पद्धति के खिलाफ सलाह दूंगा
जाँच करने के बजाय, बस क्रिया (वेब अनुरोध, मेल, ftp, आदि) करें और असफल होने के अनुरोध के लिए तैयार रहें, जो आपको वैसे भी करना है, भले ही आपका चेक सफल रहा हो।
निम्नलिखित को धयान मे रखते हुए:
1 - check, and it is OK
2 - start to perform action
3 - network goes down
4 - action fails
5 - lot of good your check did
यदि नेटवर्क नीचे है तो आपकी कार्रवाई केवल एक पिंग के रूप में तेजी से विफल हो जाएगी, आदि।
1 - start to perform action
2 - if the net is down(or goes down) the action will fail
NetworkInterface.GetIsNetworkAvailable
बहुत अविश्वसनीय है। बस कुछ VMware या अन्य LAN कनेक्शन है और यह गलत परिणाम देगा। Dns.GetHostEntry
विधि के बारे में भी मैं सिर्फ इस बात को लेकर चिंतित था कि क्या परीक्षण URL उस वातावरण में अवरुद्ध हो सकता है जहां मेरा आवेदन तैनात होने जा रहा है।
इसलिए मुझे पता चला कि एक और तरीका है InternetGetConnectedState
। मेरा कोड है
[System.Runtime.InteropServices.DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
public static bool CheckNet()
{
int desc;
return InternetGetConnectedState(out desc, 0);
}
Google द्वारा इंटरनेट कनेक्शन के लिए एक परीक्षण:
new Ping().Send("www.google.com.mx").Status == IPStatus.Success
मैं ऐसे लोगों से असहमत हूं जो कह रहे हैं: "कार्य करने से पहले कनेक्टिविटी की जाँच करने में क्या बात है, क्योंकि जाँच के तुरंत बाद कनेक्शन खो सकता है"। निश्चित रूप से डेवलपर्स द्वारा किए गए कई प्रोग्रामिंग कार्यों में अनिश्चितता की एक डिग्री है, लेकिन अनिश्चितता को स्वीकृति के स्तर तक कम करना चुनौती का हिस्सा है।
मैं हाल ही में इस समस्या को लेकर एक एप्लिकेशन बना रहा था जिसमें एक मैपिंग फीचर शामिल था जो ऑन-लाइन टाइल सर्वर से जुड़ा था। यह कार्यक्षमता अक्षम की जानी थी जहां इंटरनेट कनेक्टिविटी की कमी नोट की गई थी।
इस पृष्ठ पर कुछ प्रतिक्रियाएं बहुत अच्छी थीं, लेकिन फिर भी बहुत सारे प्रदर्शन मुद्दों जैसे कि फांसी, मुख्य रूप से कनेक्टिविटी की अनुपस्थिति के मामले में हुई।
यहाँ समाधान है कि मैं इन जवाबों और मेरे सहयोगियों में से कुछ की मदद से, का उपयोग कर समाप्त हुआ:
// Insert this where check is required, in my case program start
ThreadPool.QueueUserWorkItem(CheckInternetConnectivity);
}
void CheckInternetConnectivity(object state)
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
using (WebClient webClient = new WebClient())
{
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
webClient.Proxy = null;
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
webClient.OpenReadAsync(new Uri("<url of choice here>"));
}
}
}
volatile bool internetAvailable = false; // boolean used elsewhere in code
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
internetAvailable = true;
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
// UI changes made here
}));
}
}
मैंने ऊपर सूचीबद्ध सभी विकल्पों को देखा है और इंटरनेट को रोकने के लिए एकमात्र व्यावहारिक विकल्प उपलब्ध है या नहीं "पिंग" विकल्प है। नेटवर्क की उपलब्धता का पता लगाने में कक्षा का आयात [DllImport("Wininet.dll")]
और System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
या कोई अन्य भिन्नता NetworkInterface
अच्छी तरह से काम नहीं करती है। ये तरीके केवल यह जांचते हैं कि नेटवर्क केबल प्लग किया गया है या नहीं।
"पिंग विकल्प"
if
(कनेक्शन उपलब्ध है) रिटर्न true
if
(कनेक्शन उपलब्ध नहीं है और नेटवर्क केबल प्लग इन है) रिटर्न false
if
(नेटवर्क केबल प्लग इन नहीं है) Throws an exception
NetworkInterface
if
(इंटरनेट उपलब्ध है) रिटर्न True
if
(इंटरनेट उपलब्ध नहीं है और नेटवर्क केबल प्लग इन है) रिटर्न True
if
(नेटवर्क केबल प्लग इन नहीं है) रिटर्न false
[DllImport ("Wininet.dll")]
if
(इंटरनेट उपलब्ध है) रिटर्न True
if
(इंटरनेट उपलब्ध नहीं है और नेटवर्क केबल प्लग इन है) रिटर्न True
if
(नेटवर्क केबल प्लग इन नहीं है) रिटर्न false
तो के मामले में [DllImport("Wininet.dll")]
और NetworkInterface
जानने यदि इंटरनेट कनेक्शन उपलब्ध है का कोई तरीका नहीं है।
आपके कोड को जाँचने और चलाने के बीच नेटवर्क की समस्या को हल नहीं करता है लेकिन यह काफी विश्वसनीय है
public static bool IsAvailableNetworkActive()
{
// only recognizes changes related to Internet adapters
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
// however, this will include all adapters -- filter by opstatus and activity
NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
return (from face in interfaces
where face.OperationalStatus == OperationalStatus.Up
where (face.NetworkInterfaceType != NetworkInterfaceType.Tunnel) && (face.NetworkInterfaceType != NetworkInterfaceType.Loopback)
select face.GetIPv4Statistics()).Any(statistics => (statistics.BytesReceived > 0) && (statistics.BytesSent > 0));
}
return false;
}
Pinging google.com एक DNS रिज़ॉल्यूशन निर्भरता का परिचय देता है। Ops..8. away..8 पिंग करना ठीक है लेकिन गूगल मुझसे कई गुना दूर है। मुझे बस इतना करना है कि इंटरनेट पर मेरे पास की चीज़ को पिंग करना है।
मैं पिंग की TTL सुविधा का उपयोग पिंग हॉप # 1, फिर हॉप # 2, आदि के लिए कर सकता हूं, जब तक कि मुझे उस चीज से उत्तर न मिल जाए, जो एक स्थाई पते पर है; यदि वह नोड एक नियमित पते पर है तो यह इंटरनेट पर है। हम में से ज्यादातर के लिए, हॉप # 1 हमारा स्थानीय गेटवे / राउटर होगा, और हॉप # 2 हमारे फाइबर कनेक्शन के दूसरी तरफ या जो भी हो, पहला बिंदु होगा।
यह कोड मेरे लिए काम करता है, और इस थ्रेड में कुछ अन्य सुझावों की तुलना में तेज प्रतिक्रिया करता है क्योंकि यह इंटरनेट पर मेरे लिए जो कुछ भी है, वह पिंग कर रहा है।
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Diagnostics;
internal static bool ConnectedToInternet()
{
const int maxHops = 30;
const string someFarAwayIpAddress = "8.8.8.8";
// Keep pinging further along the line from here to google
// until we find a response that is from a routable address
for (int ttl = 1; ttl <= maxHops; ttl++)
{
Ping pinger = new Ping();
PingOptions options = new PingOptions(ttl, true);
byte[] buffer = new byte[32];
PingReply reply = null;
try
{
reply = pinger.Send(someFarAwayIpAddress, 10000, buffer, options);
}
catch (System.Net.NetworkInformation.PingException pingex)
{
Debug.Print("Ping exception (probably due to no network connection or recent change in network conditions), hence not connected to internet. Message: " + pingex.Message);
return false;
}
System.Diagnostics.Debug.Print("Hop #" + ttl.ToString() + " is " + (reply.Address == null ? "null" : reply.Address.ToString()) + ", " + reply.Status.ToString());
if (reply.Status != IPStatus.TtlExpired && reply.Status != IPStatus.Success)
{
Debug.Print("Hop #" + ttl.ToString() + " is " + reply.Status.ToString() + ", hence we are not connected.");
return false;
}
if (IsRoutableAddress(reply.Address))
{
System.Diagnostics.Debug.Print("That's routable so you must be connected to the internet.");
return true;
}
}
return false;
}
private static bool IsRoutableAddress(IPAddress addr)
{
if (addr == null)
{
return false;
}
else if (addr.AddressFamily == AddressFamily.InterNetworkV6)
{
return !addr.IsIPv6LinkLocal && !addr.IsIPv6SiteLocal;
}
else // IPv4
{
byte[] bytes = addr.GetAddressBytes();
if (bytes[0] == 10)
{ // Class A network
return false;
}
else if (bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31)
{ // Class B network
return false;
}
else if (bytes[0] == 192 && bytes[1] == 168)
{ // Class C network
return false;
}
else
{ // None of the above, so must be routable
return true;
}
}
}
यहां बताया गया है कि इसे Android में कैसे लागू किया जाता है।
अवधारणा के प्रमाण के रूप में, मैंने इस कोड का अनुवाद C #:
var request = (HttpWebRequest)WebRequest.Create("http://g.cn/generate_204");
request.UserAgent = "Android";
request.KeepAlive = false;
request.Timeout = 1500;
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.ContentLength == 0 && response.StatusCode == HttpStatusCode.NoContent)
{
//Connection to internet available
}
else
{
//Connection to internet not available
}
}
private bool ping()
{
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply reply = pingSender.Send(address);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
एक अन्य विकल्प नेटवर्क सूची प्रबंधक एपीआई है जो विस्टा और विंडोज 7. एमएसडीएन लेख के लिए उपलब्ध है । लेख में कोड नमूने डाउनलोड करने के लिए एक लिंक है जो आपको ऐसा करने की अनुमति देता है:
AppNetworkListUser nlmUser = new AppNetworkListUser();
Console.WriteLine("Is the machine connected to internet? " + nlmUser.NLM.IsConnectedToInternet.ToString());
COM टैब से नेटवर्क लिस्ट 1.0 टाइप लाइब्रेरी का संदर्भ जोड़ना सुनिश्चित करें ... जो कि NETWORKLIST के रूप में दिखाई देगा।
अपवाद को पकड़कर परीक्षण कनेक्शन से बचने की कोशिश करें। क्योंकि हम वास्तव में उम्मीद करते हैं कि कभी-कभी हम नेटवर्क कनेक्शन खो सकते हैं।
if (NetworkInterface.GetIsNetworkAvailable() &&
new Ping().Send(new IPAddress(new byte[] { 8, 8, 8, 8 }),2000).Status == IPStatus.Success)
//is online
else
//is offline
यदि आप नेटवर्क / कनेक्शन परिवर्तन होने पर उपयोगकर्ता को सूचित करना चाहते हैं / कार्रवाई करना चाहते हैं।
एनएलएम एपीआई का उपयोग करें:
मुझे व्यक्तिगत रूप से एंटोन का जवाब और moffeltje सबसे अच्छा लगता है, लेकिन मैंने VMWare और अन्य लोगों द्वारा स्थापित वर्चुअल नेटवर्क को बाहर करने के लिए एक चेक जोड़ा।
public static bool IsAvailableNetworkActive()
{
// only recognizes changes related to Internet adapters
if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) return false;
// however, this will include all adapters -- filter by opstatus and activity
NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
return (from face in interfaces
where face.OperationalStatus == OperationalStatus.Up
where (face.NetworkInterfaceType != NetworkInterfaceType.Tunnel) && (face.NetworkInterfaceType != NetworkInterfaceType.Loopback)
where (!(face.Name.ToLower().Contains("virtual") || face.Description.ToLower().Contains("virtual")))
select face.GetIPv4Statistics()).Any(statistics => (statistics.BytesReceived > 0) && (statistics.BytesSent > 0));
}
bool bb = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
if (bb == true)
MessageBox.Show("Internet connections are available");
else
MessageBox.Show("Internet connections are not available");
bb
तब भी सही होगा जब नेटवर्क इंटरनेट से कनेक्ट नहीं है।
बहु पिरोया संस्करण पिंग:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
namespace OnlineCheck
{
class Program
{
static bool isOnline = false;
static void Main(string[] args)
{
List<string> ipList = new List<string> {
"1.1.1.1", // Bad ip
"2.2.2.2",
"4.2.2.2",
"8.8.8.8",
"9.9.9.9",
"208.67.222.222",
"139.130.4.5"
};
int timeOut = 1000 * 5; // Seconds
List<Thread> threadList = new List<Thread>();
foreach (string ip in ipList)
{
Thread threadTest = new Thread(() => IsOnline(ip));
threadList.Add(threadTest);
threadTest.Start();
}
Stopwatch stopwatch = Stopwatch.StartNew();
while (!isOnline && stopwatch.ElapsedMilliseconds <= timeOut)
{
Thread.Sleep(10); // Cooldown the CPU
}
foreach (Thread thread in threadList)
{
thread.Abort(); // We love threads, don't we?
}
Console.WriteLine("Am I online: " + isOnline.ToYesNo());
Console.ReadKey();
}
static bool Ping(string host, int timeout = 3000, int buffer = 32)
{
bool result = false;
try
{
Ping ping = new Ping();
byte[] byteBuffer = new byte[buffer];
PingOptions options = new PingOptions();
PingReply reply = ping.Send(host, timeout, byteBuffer, options);
result = (reply.Status == IPStatus.Success);
}
catch (Exception ex)
{
}
return result;
}
static void IsOnline(string host)
{
isOnline = Ping(host) || isOnline;
}
}
public static class BooleanExtensions
{
public static string ToYesNo(this bool value)
{
return value ? "Yes" : "No";
}
}
}
मुझे नहीं लगता कि यह असंभव है, बस सीधा नहीं है।
मैंने कुछ इस तरह बनाया है, और हाँ यह सही नहीं है, लेकिन पहला चरण आवश्यक है: यह जांचने के लिए कि क्या कोई नेटवर्क कनेक्टिविटी है या नहीं। Windows Api एक महान काम नहीं करता है, इसलिए बेहतर काम क्यों नहीं किया जाता है?
bool NetworkIsAvailable()
{
var all = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (var item in all)
{
if (item.NetworkInterfaceType == NetworkInterfaceType.Loopback)
continue;
if (item.Name.ToLower().Contains("virtual") || item.Description.ToLower().Contains("virtual"))
continue; //Exclude virtual networks set up by VMWare and others
if (item.OperationalStatus == OperationalStatus.Up)
{
return true;
}
}
return false;
}
यह बहुत आसान है, लेकिन यह वास्तव में चेक की गुणवत्ता में सुधार करने में मदद करता है, खासकर जब आप विभिन्न प्रॉक्सी कॉन्फ़िगरेशन की जांच करना चाहते हैं।
इसलिए:
public static bool Isconnected = false;
public static bool CheckForInternetConnection()
{
try
{
Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success)
{
return true;
}
else if (reply.Status == IPStatus.TimedOut)
{
return Isconnected;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}
public static void CheckConnection()
{
if (CheckForInternetConnection())
{
Isconnected = true;
}
else
{
Isconnected = false;
}
}
नेटवर्क स्थिति और इंटरनेट कनेक्शन की निगरानी के लिए NetworkMonitor का उपयोग करें ।
नमूना:
namespace AmRoNetworkMonitor.Demo
{
using System;
internal class Program
{
private static void Main()
{
NetworkMonitor.StateChanged += NetworkMonitor_StateChanged;
NetworkMonitor.StartMonitor();
Console.WriteLine("Press any key to stop monitoring.");
Console.ReadKey();
NetworkMonitor.StopMonitor();
Console.WriteLine("Press any key to close program.");
Console.ReadKey();
}
private static void NetworkMonitor_StateChanged(object sender, StateChangeEventArgs e)
{
Console.WriteLine(e.IsAvailable ? "Is Available" : "Is Not Available");
}
}
}
परिचय
कुछ परिदृश्यों में आपको यह जांचने की आवश्यकता है कि विंडोज़ अनुप्रयोगों में सी # कोड का उपयोग करके इंटरनेट उपलब्ध है या नहीं। विंडोज़ फ़ॉर्म्स में इंटरनेट का उपयोग करके फ़ाइल डाउनलोड करना या अपलोड करना या डेटाबेस से कुछ डेटा प्राप्त करना हो सकता है, जो दूरस्थ स्थान पर है, इन स्थितियों में इंटरनेट की जाँच अनिवार्य है।
कोड से C # का उपयोग करके इंटरनेट उपलब्धता की जांच करने के कुछ तरीके हैं। इस तरह के सभी तरीकों को उनकी सीमाओं सहित यहां समझाया गया है।
'Wininet' API का उपयोग स्थानीय सिस्टम की जाँच के लिए किया जा सकता है जिसमें सक्रिय इंटरनेट कनेक्शन है या नहीं। इसके लिए उपयोग किया जाने वाला नामस्थान 'System.Runtime.InteropServices' है और DllImport का उपयोग करके dll 'wininet.dll' आयात करेगा। इसके बाद एक ब्यूटेन वैरिएबल बनाएं जिसमें एक्सटर्नल स्टेटिक एक फंक्शन नाम InternetGetConnectedState के साथ दो मापदंडों के विवरण और आरक्षितवैल्यू के रूप में दिखाया गया है।
नोट: एक्सटर्नल संशोधक का उपयोग एक ऐसी विधि को घोषित करने के लिए किया जाता है जिसे बाहरी रूप से लागू किया जाता है। जब आप अनवांटेड कोड में कॉल करने के लिए इंटरोप सेवाओं का उपयोग कर रहे हैं तो एक्सट्रीम संशोधक का एक सामान्य उपयोग DllImport विशेषता के साथ है। इस स्थिति में, विधि को भी स्थैतिक घोषित किया जाना चाहिए।
अगला बूलियन के रूप में 'IsInternetAvailable' नाम से एक विधि बनाएं। इस पद्धति में उपरोक्त फ़ंक्शन का उपयोग किया जाएगा जो स्थानीय प्रणाली की इंटरनेट स्थिति लौटाता है
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int description, int reservedValue);
public static bool IsInternetAvailable()
{
try
{
int description;
return InternetGetConnectedState(out description, 0);
}
catch (Exception ex)
{
return false;
}
}
यदि नेटवर्क कनेक्शन उपलब्ध है, तो निम्न उदाहरण GetIsNetworkAvailable विधि का उपयोग करता है।
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
System.Windows.MessageBox.Show("This computer is connected to the internet");
}
else
{
System.Windows.MessageBox.Show("This computer is not connected to the internet");
}
रिमार्क्स (प्रति MSDN के रूप में): एक नेटवर्क कनेक्शन उपलब्ध माना जाता है यदि कोई नेटवर्क इंटरफ़ेस "ऊपर" चिह्नित है और लूपबैक या टनल इंटरफ़ेस नहीं है।
ऐसे कई मामले हैं जिनमें एक उपकरण या कंप्यूटर एक उपयोगी नेटवर्क से जुड़ा नहीं है, लेकिन फिर भी उपलब्ध माना जाता है और GetIsNetworkAvailable सच में वापस आ जाएगा। उदाहरण के लिए, यदि एप्लिकेशन चलाने वाला डिवाइस एक वायरलेस नेटवर्क से जुड़ा होता है, जिसमें प्रॉक्सी की आवश्यकता होती है, लेकिन प्रॉक्सी सेट नहीं है, तो GetIsNetworkAvailable सच वापस आ जाएगा। जब GetIsNetworkAvailable सही लौटेगा, तो एक अन्य उदाहरण यह है कि यदि एप्लिकेशन ऐसे कंप्यूटर पर चल रहा है जो हब या राउटर से जुड़ा है, जहां हब या राउटर ने अपस्ट्रीम कनेक्शन खो दिया है।
पिंग और पिंगरपेली कक्षाएं एक एप्लिकेशन को यह निर्धारित करने की अनुमति देती हैं कि मेजबान से उत्तर प्राप्त करके एक दूरस्थ कंप्यूटर नेटवर्क पर पहुंच योग्य है या नहीं। ये कक्षाएं System.Net.NetworkInformation नामस्थान में उपलब्ध हैं। निम्न उदाहरण दिखाता है कि होस्ट को कैसे पिंग करना है।
protected bool CheckConnectivity(string ipAddress)
{
bool connectionExists = false;
try
{
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
if (!string.IsNullOrEmpty(ipAddress))
{
System.Net.NetworkInformation.PingReply reply = pingSender.Send(ipAddress);
connectionExists = reply.Status ==
System.Net.NetworkInformation.IPStatus.Success ? true : false;
}
}
catch (PingException ex)
{
Logger.LogException(ex.Message, ex);
}
return connectionExists;
}
रिमार्क्स (प्रति MSDN के अनुसार): दूरस्थ कंप्यूटर उपलब्ध है या नहीं, यह पता लगाने के लिए अनुप्रयोग पिंग वर्ग का उपयोग करते हैं। नेटवर्क टोपोलॉजी यह निर्धारित कर सकती है कि क्या पिंग किसी दूरस्थ होस्ट से सफलतापूर्वक संपर्क कर सकता है। परदे के पीछे, नेटवर्क एड्रेस ट्रांसलेशन (NAT) उपकरण या फायरवॉल की उपस्थिति और विन्यास पिंग को सफल होने से रोक सकता है। एक सफल पिंग केवल इंगित करता है कि रिमोट होस्ट नेटवर्क पर पहुंच सकता है; दूरस्थ होस्ट पर उच्च स्तरीय सेवाओं (जैसे वेब सर्वर) की उपस्थिति की गारंटी नहीं है।
टिप्पणियाँ / सुझाव आमंत्रित हैं। हैप्पी कोडिंग ......!
मेरे आवेदन के लिए हम भी छोटे फ़ाइल डाउनलोड करके परीक्षण करते हैं।
string remoteUri = "https://www.microsoft.com/favicon.ico"
WebClient myWebClient = new WebClient();
try
{
byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);
if(myDataBuffer.length > 0) // Or add more validate. eg. checksum
{
return true;
}
}
catch
{
return false;
}
इसके अलावा। कुछ ISP फ़ाइल को कैश करने के लिए मध्य सर्वर का उपयोग कर सकते हैं। यादृच्छिक अप्रयुक्त पैरामीटर जोड़ें जैसे। https://www.microsoft.com/favicon.ico?req=random_number कैशिंग को रोका जा सकता है।
मैं अपने 3G राउटर / मॉडेम पर उन तरीकों को जारी कर रहा हूं, क्योंकि अगर इंटरनेट डिस्कनेक्ट हो गया है तो राउटर पृष्ठ को उसके रिस्पॉन्स पेज पर रीडायरेक्ट कर देता है, इसलिए आपको अभी भी स्टीम मिलता है और आपका कोड लगता है कि इंटरनेट है। सेब (या अन्य) में एक हॉट-स्पॉट-डिडक्शन पेज होता है जो हमेशा एक निश्चित प्रतिक्रिया देता है। निम्नलिखित नमूना "सफलता" प्रतिक्रिया देता है। तो आपको पूरा यकीन है कि आप इंटरनेट से जुड़ सकते हैं और वास्तविक प्रतिक्रिया पा सकते हैं!
public static bool CheckForInternetConnection()
{
try
{
using (var webClient = new WebClient())
using (var stream = webClient.OpenRead("http://captive.apple.com/hotspot-detect.html"))
{
if (stream != null)
{
//return true;
stream.ReadTimeout = 1000;
using (var reader = new StreamReader(stream, Encoding.UTF8, false))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line == "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>")
{
return true;
}
Console.WriteLine(line);
}
}
}
return false;
}
}
catch
{
}
return false;
}
मेरे पास इंटरनेट कनेक्शन के लिए तीन परीक्षण हैं।
System.Net
औरSystem.Net.Sockets
टेस्ट 1
public bool IsOnlineTest1()
{
try
{
IPHostEntry dummy = Dns.GetHostEntry("https://www.google.com");
return true;
}
catch (SocketException ex)
{
return false;
}
}
परीक्षण २
public bool IsOnlineTest2()
{
try
{
IPHostEntry dummy = Dns.GetHostEntry("https://www.google.com");
return true;
}
catch (SocketException ex)
{
return false;
}
}
परीक्षण ३
public bool IsOnlineTest3()
{
System.Net.WebRequest req = System.Net.WebRequest.Create("https://www.google.com");
System.Net.WebResponse resp = default(System.Net.WebResponse);
try
{
resp = req.GetResponse();
resp.Close();
req = null;
return true;
}
catch (Exception ex)
{
req = null;
return false;
}
}
परीक्षण कर रहे हैं
यदि आप एक बनाते Dictionary
हैं String
और Boolean
कहते हैं CheckList
, तो आप प्रत्येक परीक्षण के परिणामों को जोड़ सकते हैं CheckList
।
अब, लूप KeyValuePair
का उपयोग करके प्रत्येक के माध्यम से पुनरावृत्ति करें for...each
।
अगर CheckList
एक होता है Value
की true
, तो आप को पता एक इंटरनेट कनेक्शन नहीं है।
public static bool HasConnection()
{
try
{
System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("www.google.com");
return true;
}
catch
{
return false;
}
}
यह काम करता है