Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2007

Tip: Looking for answers? Try searching our database.

how do i check the network connection status

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hayes - 04 Sep 2007 04:40 GMT
Hi all:
   I am using VC++ 2005.  When using WinXP this is with SP2 installed.

   I want to detect  network status if it's in connecting or disconnecting.
i try to use the function of "connect" in "winsock2.h" and connect with DNS
Server and Gateway to judge connection status. but the reply is always
"failed to connect".
i can't find what's the problem.
  Is there other function to implement what i want or maybe something need
mofidy in my code?
  THanks all~

my code:

CheckConnection()
{
    //----------------------
    // Initialize Winsock
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR)
        AfxMessageBox("Error at WSAStartup()");

    //----------------------
    // Create a SOCKET for connecting to server
    SOCKET ConnectSocket;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        CString error;
        error.Format("%ld", WSAGetLastError() );
        AfxMessageBox("Error at socket():"+error);
        WSACleanup();
        return  FALSE;
    }

 //----------------------
 // The sockaddr_in structure specifies the address family,
 // IP address, and port of the server to be connected to.
 sockaddr_in clientService;
 clientService.sin_family = AF_INET;
 clientService.sin_addr.s_addr = inet_addr( DNSIp );
 clientService.sin_port = htons( 27015 );

 //----------------------
 // Connect to server.
 if ( connect( ConnectSocket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
   AfxMessageBox( "Failed to connect.\n" );
   WSACleanup();
   return FALSE;
 }

 AfxMessageBox("Connected to server.\n");
 WSACleanup();
 return TRUE;

    return TRUE;
}
Ben Voigt [C++ MVP] - 05 Sep 2007 01:47 GMT
> Hi all:
>    I am using VC++ 2005.  When using WinXP this is with SP2 installed.
[quoted text clipped - 6 lines]
> "failed to connect".
> i can't find what's the problem.

What's port 27015 supposed to be?

>   Is there other function to implement what i want or maybe something need
> mofidy in my code?

IP Helper has a lot of functions that may serve your needs.  Just a call to
GetBestRoute would probably tell you if the computer has connectivity, and I
don't think it would trigger dialing out.

>   THanks all~
>
[quoted text clipped - 44 lines]
> return TRUE;
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.