1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <windows.h>
#include <iostream>
#include <fstream>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "mswsock.lib")
int main()
{
WSADATA wsa;
SOCKET hSocket;
sockaddr_in address;
char szSite[128] = "\0", szIP[16] = "\0";
// -----------------------------------------------------------------
std::cout << "Enter an IP of a site:\n> ";
std::cin >> szIP;
std::cout << "Enter the page:\n> ";
std::cin >> szSite;
system("cls");
// -----------------------------------------------------------------
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(szIP);
address.sin_port = htons(80);
// -----------------------------------------------------------------
WSAStartup(0x0202, &wsa);
hSocket = socket(AF_INET, SOCK_STREAM, 6);
if(connect(hSocket, (sockaddr*)&address, sizeof(address)) == SOCKET_ERROR)
{
// Error
// std::cerr << "An error #1\n";
closesocket(hSocket);
WSACleanup();
return -1;
}
// -----------------------------------------------------------------
std::ofstream ofFile("C:\\cpp\\HtmlCodeOfTheSite.html");
if(ofFile.good())
{
int nCnt = 0;
char szSnd[2048] = "\0";
strcpy(szSnd, "GET ");
strcat(szSnd, szSite);
strcat(szSnd, " HTTP/1.0\r\n\r\n");
if(send(hSocket, szSnd, strlen(szSnd), NULL) == SOCKET_ERROR)
{
// Error
// std::cerr << "An error #2\n";
closesocket(hSocket);
ofFile.close();
WSACleanup();
return -1;
}
do
{
if((nCnt = recv(hSocket, szSnd, sizeof(szSnd), NULL)) == SOCKET_ERROR)
{
// Error
// std::cerr << "An error #3\n";
closesocket(hSocket);
ofFile.close();
WSACleanup();
return -1;
}
ofFile.write(szSnd, nCnt);
} while(nCnt > NULL);
ofFile.close();
}
closesocket(hSocket);
WSACleanup();
return 0;
}
|