Testing Network Connectivity

Last post 01-05-2007 7:38 by Anonymous. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 06-24-2002 9:18

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,940

    Testing Network Connectivity

    Credit: Neil Cowburn

    C# Version:


    using System;
    using System.Net;

    public class Net
    {
    protected Net() {}

    public static bool IsWebAccessible() // Call this class as follows: bool bResponse = Net.IsWebAccessible;
    {
    HttpWebRequest hwrRequest;
    HttpWebResponse hwrResponse;

    string strUrl = @"http://www.microsoft.com/";
    bool bConnected = false;

    try
    {
    hwrRequest = (HttpWebRequest)WebRequest.Create(strUrl);
    hwrResponse = (HttpWebResponse)hwrRequest.GetResponse();

    if(hwrResponse.StatusCode == HttpStatusCode.OK)
    {
    bConnected = true;
    }
    hwrResponse.GetResponseStream().Close();
    }
    catch(WebException we)
    {
    bConnected = false;
    }
    catch(Exception ex)
    {
    bConnected = false;
    }
    finally
    {
    hwrRequest = null;
    hwrResponse = null;
    }

    return bConnected;
    }
    }


    VB .NET Version


    Imports System
    Imports System.Net

    Public Class Net

    Protected Sub New()
    End Sub

    Public Shared Function IsWebAccessible() As Boolean

    Dim hwrRequest As HttpWebRequest
    Dim hwrResponse As HttpWebResponse

    Dim strUrl As String = "http://www.microsoft.com/"
    Dim bConnected As Boolean = False

    Try
    hwrRequest = CType(WebRequest.Create(strUrl), HttpWebRequest)
    hwrResponse = CType(hwrRequest.GetResponse(), HttpWebResponse)

    If hwrResponse.StatusCode = HttpStatusCode.OK Then
    bConnected = True
    End If
    hwrResponse.GetResponseStream().Close

    Catch we As WebException
    bConnected = False
    Catch ex As Exception
    bConnected = False
    Finally
    hwrRequest = Nothing
    hwrResponse = Nothing
    End Try

    Return bConnected

    End Function

    End Class


  • 10-16-2002 13:20 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,940

    Re: Testing Network Connectivity

    Posted modifications (hwrResponse.GetResponseStream().Close()) to prevent lock up on multiple calls. (Credit Neelima Godugu)

  • 04-06-2004 22:46 In reply to

    Re: Testing Network Connectivity

    As I set
    hwrRequest.Timeout = 5;

    but for the unknown host it still takes about 50s to return false.
    Why's that?
  • 06-21-2004 19:46 In reply to

    Re: Testing Network Connectivity

    quote:
    Originally posted by alho

    As I set
    hwrRequest.Timeout = 5;

    but for the unknown host it still takes about 50s to return false.
    Why's that?



    I dont know for sure, but I do know that if you do something like:
    Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000);
    Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);

    those timeout values apply only AFTER the connection has been established. AFAIK there is no way to adjust the timeout when trying to establish an initial connection attempt.
  • 01-05-2007 7:38 In reply to

    Re: Testing Network Connectivity

    I have read an article about how to test the network connectivity but i don't remember the url. But i thinks this method is more efficient to test the network available.


    try
    {
    string hostname = Dns.GetHostName();
    IPHostEntry thishost = Dns.GetHostByName(hostname);
    string ipaddress = thishost.AddressList[0].ToString();
    ret = !ipaddress.Equals(IPAddress.Parse("127.0.0.1").ToString());
    }
    catch
    {
    throw new Exception("No network connection.");
    }


    Have fun.
Page 1 of 1 (5 items)