how do i check the network connection status

G

Guest

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;
}
 
B

Ben Voigt [C++ MVP]

Hayes said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top