|
// WinMain.cpp
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
DWORD GetHostAddress( const char* cAddr )
{
struct hostent* phe;
phe = gethostbyname( cAddr );
if( phe==NULL ) return 0;
char* p = *phe->h_addr_list;
return *( (DWORD*)p );
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WSADATA WsaData;
if( WSAStartup( 0x0101, &WsaData ) != 0 )
{
// Ошибка, выход.
MessageBox(NULL,TEXT("WSAStartup"),TEXT("Ошибка"),MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST);
return 1;
}
SOCKET Socket;
Socket = socket( AF_INET, SOCK_STREAM, 0 );
if( Socket==INVALID_SOCKET )
{
// Ошибка, выход.
MessageBox(NULL,TEXT("Socket"),TEXT("Ошибка"),MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST);
return 1;
}
int nPort = 80;
char svHostname[] = "ia700406.us.archive.org";
SOCKADDR_IN Addr;
Addr.sin_family = AF_INET;
Addr.sin_port = htons( (WORD)nPort );
Addr.sin_addr.s_addr = GetHostAddress( (const char*)svHostname );
if( connect( Socket, (LPSOCKADDR)&Addr, sizeof(SOCKADDR_IN) ) == SOCKET_ERROR )
{
// Ошибка, выход.
MessageBox(NULL,TEXT("Connect"),TEXT("Ошибка"),MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST);
return 1;
}
char svFilename[] = "https://ia700406.us.archive.org/34/items/cdc255/cdc255_64kb_mp3.zip";
char svBuffer[1024];
lstrcpyA(svBuffer, "GET " );
lstrcatA(svBuffer, svFilename);
lstrcatA(svBuffer, " HTTP/1.0\r\n" );
lstrcatA(svBuffer,
"Accept: image/gif, image/x-xbitmap, image/jpeg, \
image/pjpeg, application/vnd.ms-excel, application/msword, \
application/vnd.ms-powerpoint, */*\r\n"
);
lstrcatA(svBuffer,"Accept-Language: en-us\r\n");
lstrcatA(svBuffer,"Accept-Encoding: gzip, deflate\r\n");
lstrcatA(svBuffer,"User-Agent: Mozilla/5.0\r\n");
//lstrcat(svBuffer,"Cookie: mycookie=quackquack\r\n");
lstrcatA(svBuffer, "Host: ");
lstrcatA(svBuffer, svHostname);
lstrcatA(svBuffer, "\r\n");
lstrcatA(svBuffer, "\r\n");
if( send( Socket, svBuffer, lstrlenA(svBuffer), 0 ) == SOCKET_ERROR )
{
// Ошибка, выход.
MessageBox(NULL,TEXT("Send"),TEXT("Ошибка"),MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST);
return 1;
}
int nCount;
int nChars=0;
BOOL bDone=FALSE;
while( !bDone )
{
nCount = recv( Socket, svBuffer, 1, 0 );
if( nCount < 0 )
bDone = TRUE;
switch( *svBuffer )
{
case '\r':
break;
case '\n':
if( nChars==0 )
bDone=TRUE;
nChars=0;
break;
default:
++nChars;
break;
}
}
DWORD dwBytes;// Создать и записать новый файл.
LPCTSTR FileSave = TEXT("C:\\cdc255_64kb_mp3.zip");
HANDLE hFile=CreateFile(FileSave,
GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if( hFile!=INVALID_HANDLE_VALUE)
{
do {
nCount = recv( Socket, svBuffer, 1024, 0 );
if( nCount > 0 )
{
WriteFile( hFile, svBuffer, nCount, &dwBytes, NULL );
}
} while( nCount > 0 );
CloseHandle( hFile );
}
closesocket( Socket );
MessageBox(NULL,TEXT("Сделано"),TEXT("Info"),MB_OK|MB_ICONINFORMATION|MB_SETFOREGROUND|MB_TOPMOST);
return 0;
}
| |