This is mine A.h file
I want to make it as an Base class
<pre lang="c++">class A
{
public :
struct addrinfo addr_;
unsigned short port_;
CString hostname_;
CRITICAL_SECTION sect_;
HANDLE threadHandle_;
HANDLE threadHandle1_;
bool connected_;
SOCKET sock_;
public:
int ConnectToMachine(); };
This is mine A.cpp
class B:public A
{
int ConnectToMachine()
{
int conResult,iResult;
struct addrinfo *result = NULL,*ptr = NULL;
u_long iMode = 0;
DWORD nTimeout = 5000; int port=22;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
}
ZeroMemory( &addr_, sizeof(addr_) );
addr_.ai_family = AF_UNSPEC;
addr_.ai_socktype = SOCK_STREAM;
addr_.ai_protocol = IPPROTO_TCP;
conResult = getaddrinfo("192.168.15.168", (PCSTR)22, &addr_, &result);
if ( conResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", conResult);
WSACleanup();
return 1;
}
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
sock_ = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (sock_ == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
conResult = ioctlsocket(sock_, FIONBIO, &iMode);
if (conResult != NO_ERROR)
printf("ioctlsocket failed with error: %ld\n", conResult);
conResult = setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
if (conResult != NO_ERROR)
{
printf("\nSetsocopt fail with error :%d\n",WSAGetLastError());
return 0;
}
conResult = connect(sock_, ptr->ai_addr, (int)ptr->ai_addrlen);
if (conResult == SOCKET_ERROR) {
closesocket(sock_);
sock_ = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (sock_ == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
DWORD dwThreadId;
threadHandle1_=CreateThread(NULL,0,startReceive,this,0,&dwThreadId);
}
}
int main()
{
//i want to call the connect to machine function of B class,how to do this,simple creating the object of B class doesn't work
}