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