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
|
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
int main()
{
WSADATA wsaData;
int iResult;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
struct sockaddr_in info;
ZeroMemory(&info, sizeof(info));
info.sin_family = AF_INET;
info.sin_port = htons(80);
struct in_addr addr;
struct hostent *remoteHost;
remoteHost = gethostbyname("google.com");
printf("Official name: %s\n", remoteHost->h_name);
if (remoteHost->h_addrtype == AF_INET) {
int i = 0;
while (remoteHost->h_addr_list[i] != 0) {
addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
info.sin_addr.s_addr = addr.s_addr;
printf("IP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
SOCKET s = INVALID_SOCKET;
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
iResult = connect(s, (struct sockaddr*)&info, sizeof(info));
if (iResult == SOCKET_ERROR)
{
printf("connect failed: %d\n", WSAGetLastError());
}
char mesaj1[] = "GET / HTTP/1.0\r\nHost: www.google.com\r\nUser-Agent: Browser rudimentar\r\n\r\n";
iResult = send(s, mesaj1, sizeof(mesaj1), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
}
char receiveBuffer[128];
ZeroMemory(receiveBuffer, sizeof(receiveBuffer));
do
{
iResult = recv(s, receiveBuffer, 64, 0);
printf(receiveBuffer);
ZeroMemory(receiveBuffer, sizeof(receiveBuffer));
}
while (iResult > 0);
FILE *file;
file = fopen("code.txt","a+");
fprintf(file,"%s",receiveBuffer);
fclose(file);
getchar();
WSACleanup();
return 0;
}
|