Parsi Coders

نسخه‌ی کامل: سورس کد چک کردن وضعیت اتصال به اینترنت
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
Check if an Internet Connection is available


کد پی‌اچ‌پی:
''' <summary>
''' 
Pings a variation of websites defined in a array and checks for connectivity.
''' </summary>
''' 
<returns>True if a ping succeededFalse if otherwise.</returns>
''' <remarks></remarks>
Public Function isInternetAvailable() As Boolean
    ' 
The arrays of sites we want to check for connectivity.
    
' If all fail to ping, the function will return false.
    Dim sitesToCheck() As String = {"www.google.com", _
    "www.microsoft.com", "www.dreamincode.net"}

    Dim failedPings As Integer = 0

    ' 
Loop through all hostnames and ping them
    
For Each str As String In sitesToCheck
        
Try
            
My.Computer.Network.Ping(str)
        Catch 
ex As Exception
            failedPings 
+= 1
        End 
Try
    
Next

    
' Couldn't ping any of the hostnames.
    If 
failedPings sitesToCheck.Length Then Return False
    
Return True
End 
Function

' Example Usage
MessageBox.Show(isInternetAvailable())