#include <Winsock2.h>
#include <iostream>
#include <conio.h>
#include "locale.h"
typedef void* Socket_t;
typedef void* Host_t;
WSADATA g_pWSAData;
bool Sockets_Init()
{
WORD wVersion = MAKEWORD( 2, 2 );
int nRet = WSAStartup( wVersion, &g_pWSAData );
return nRet ? 1 : 0;
}
void Sockets_Shutdown()
{
WSACleanup();
}
Socket_t CreateSocket()
{
return (Socket_t) socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
}
void CloseSocket( Socket_t Socket )
{
if( !Socket )
{
return;
}
SOCKET Sock = (SOCKET)Socket;
closesocket( Sock );
}
Host_t GetHostByName( const char* pName )
{
char szName[255];
strcpy( szName, pName );
if ( isalpha( szName[0] ) )
{
if ( szName[strlen( szName )-1] == '\n')
szName[strlen( szName )-1] = '\0';
return (Host_t) gethostbyname( szName );
}
else
{
return 0;
}
}
Host_t GetHostByAddress( const char* pAddress )
{
char szName[255];
strcpy( szName, pAddress );
if ( !isalpha( szName[0] ) )
{
unsigned int addr = inet_addr( szName );
return (Host_t) gethostbyaddr( (char* )&addr, 4, AF_INET );
}
else
{
return 0;
}
}
int GetHostAddr( Host_t Host, char* pAddress )
{
if( (!pAddress) || (!Host) )
{
return 0;
}
hostent* ht = (hostent*)Host;
strcpy( pAddress, inet_ntoa( *(in_addr*)ht->h_addr ) );
return strlen( pAddress );
}
unsigned int GetHostAddr( Host_t Host )
{
if( !Host )
{
return 0;
}
hostent* ht = (hostent*)Host;
return inet_addr( ht->h_addr );
}
bool ConnectToServer( Socket_t Socket, unsigned int uAddress, int nPort )
{
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = uAddress;
addr.sin_port = htons( nPort );
if( connect( (SOCKET)Socket, (SOCKADDR*) &addr, sizeof( addr ) ) == SOCKET_ERROR )
{
return 0;
}
return 1;
}
bool ConnectToServer( Socket_t Socket, const char* pAddress, int nPort )
{
return ConnectToServer( Socket, inet_addr( pAddress ), nPort );
}
int main()
{
char szAddress[255];
char buf[1024];
Sockets_Init();
Socket_t Socket = CreateSocket();
Host_t Host = GetHostByName( "files.gsc-game.ru" );
GetHostAddr( Host, szAddress );
ConnectToServer( Socket, szAddress, 80 );
strcpy( buf, "HEAD / HTTP/1.1\xD\xAHost: files.gsc-game.ru:80\xD\xAUser-Agent: bildanutiy\xD\xA\xD\xA" );
send( (SOCKET)Socket, buf, 1024, 0 );
recv( (SOCKET)Socket, buf, 1024, 0 );
std::cout << buf;
_getch();
CloseSocket( Socket );
Sockets_Shutdown();
}