BOOL Connect(char *hostname)
{
WSADATA wsaData;
WORD version;
int error;
SOCKET client;
version = MAKEWORD( 2, 0 );
error = WSAStartup( version, &wsaData );
if ( error != 0 )
{
// error occured
return FALSE;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
WSACleanup();
return FALSE;
}
client = socket( AF_INET, SOCK_STREAM, 0 );
struct hostent *host;
host = gethostbyname( hostname );
struct sockaddr_in sin;
memset( &sin, 0, sizeof sin );
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = ((struct in_addr *)(host->h_addr))->s_addr;
sin.sin_port = htons(135);
int nRet2 = -1;
int nLen2 = sizeof(nRet2);
if(getsockopt(client, SOL_SOCKET, SO_SNDTIMEO, (char*)&nRet2, (int*)&nLen2) == 0)
printf("send timeout is %d\n", nRet2);
else
printf("error %d\n", WSAGetLastError());
int optval = 1000;
if(setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, (char*)&optval, sizeof(int)) !=0)
{
printf("error %d", WSAGetLastError());
}
if(getsockopt(client, SOL_SOCKET, SO_SNDTIMEO, (char*)&nRet2, (int*)&nLen2) == 0)
printf("send timeout is %d\n", nRet2);
else
printf("error %d\n", WSAGetLastError());
if ( connect( client, (struct sockaddr *)&sin, sizeof sin ) == SOCKET_ERROR )
{
printf( "Failed to connect.\n" );
closesocket(client);
WSACleanup();
return FALSE;
}
else
printf("success\n");
closesocket(client);
WSACleanup();
return TRUE;
}
תודה מראש,
כפיר