CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2010
    Posts
    74

    internet connection always shows connected

    hi ,
    I am working on windows mobile and I have following piece of code for finding internet connection is available or not but it is not working always show connected . where am i wrong? please check and give any solutions.


    in network.cpp

    struct in_addr BIN_IPV4_ADDR_LOOPBACK = {127,0,0,1};

    struct in6_addr BIN_IPV6_ADDR_LOOPBACK = {0x0 ,0x0,
    0x0 ,0x0,
    0x0,0x0,
    0x0, 0x0,
    0x0, 0x0,
    0x0 ,0x0,
    0x0 ,0x0,
    0x0 ,0x1};


    CNetwork::CNetwork(void)
    {
    }

    CNetwork::~CNetwork(void)
    {
    }

    BOOL CNetwork::IsPresent()
    {
    WSADATA WSAData ;
    WSAStartup(MAKEWORD(2,2),&WSAData);
    BOOL bFoundLocalAddr = FALSE;
    char szAddrASCII[MAX_LOCAL_NAME_LEN];
    ADDRINFO AddrHints, *pAI, *pAddrInfo;

    //
    // Get the local host's name in ASCII text.
    //
    if(gethostname(szAddrASCII, MAX_LOCAL_NAME_LEN - 1))
    {
    // Print(TEXT("Error getting local host name, error = %d"), WSAGetLastError());
    AfxMessageBox(L"Error getting local host name, error " );

    return FALSE;
    }

    //
    // To obtain a list of all the local
    // addresses, resolve the local name with getaddrinfo for all

    memset(&AddrHints, 0, sizeof(AddrHints));
    AddrHints.ai_family = PF_UNSPEC;//caller will accept any protocol family
    AddrHints.ai_flags = AI_PASSIVE; //the caller intends to use the returned socket address struc.
    //in a call to the bind (Windows Sockets) function


    int err ;
    if((err=getaddrinfo(szAddrASCII, "10", &AddrHints, &pAddrInfo))!=0)
    {
    // Print(TEXT("getaddrinfo(%hs) error %d"), szAddrASCII, WSAGetLastError());
    AfxMessageBox(L"getaddrinfo error %d " );
    CString s ;

    s.Format(L"error is %d", err);

    return FALSE;
    }
    //
    // Search the addresses returned.
    // If any of them match the loopback address, then
    // are not connected to an outside network.
    //
    // Note: This will not tell you how many networks you
    // are connected to. If one or more networks are present,
    // then the loopback addresses will not be included in the
    // list returned from getaddrinfo.
    //
    bFoundLocalAddr = TRUE;
    for(pAI = pAddrInfo; pAI != NULL && bFoundLocalAddr; pAI = pAI->ai_next)
    {
    if(pAI->ai_family == PF_INET)
    {
    if(memcmp(&(((SOCKADDR_IN *)(pAI->ai_addr))->sin_addr), &BIN_IPV4_ADDR_LOOPBACK, sizeof(BIN_IPV4_ADDR_LOOPBACK)) == 0)

    bFoundLocalAddr = FALSE;
    }
    else if(pAI->ai_family == PF_INET6)
    {
    if(memcmp(&(((SOCKADDR_IN6 *)(pAI->ai_addr))->sin6_addr), &BIN_IPV6_ADDR_LOOPBACK, sizeof(BIN_IPV6_ADDR_LOOPBACK)) == 0)
    bFoundLocalAddr = FALSE;
    }
    }

    freeaddrinfo(pAddrInfo);

    return bFoundLocalAddr;
    }


    thanks,
    rohit

  2. #2
    Join Date
    Mar 2010
    Posts
    74

    Re: internet connection always shows connected

    sorry this post is not for this type of query.I post it innetwork section.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured