#include <winsock.h> #include <stdio.h> #include <iostream> #include <fstream>
int main() { WSADATA wsa; SOCKET sock; struct hostent *host; struct sockaddr_in direc; char buffer[512]; int len = 0; char *mensaje = "GET /info/stuff/codeproject_w2k_bg.gif\n\n";
WSAStartup(MAKEWORD(2,2),&wsa); host=gethostbyname("www.codeproject.com");
sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
direc.sin_family=AF_INET; direc.sin_port=htons(80); direc.sin_addr = *((struct in_addr *)host->h_addr); memset(&direc.sin_zero,0,8);
connect(sock,(sockaddr *)&direc, sizeof(sockaddr));
printf("[Enviando GET...]\n"); printf("[Recibiendo...]\n"); len=send(sock,mensaje,strlen(mensaje),0); std::ofstream fs; fs.open("nombre.gif", std::ios::app); while (len = recv(sock, buffer, 512, 0)) { fs << buffer; } printf("[Archivo guardado.]\n"); system("pause"); return 0; }
|