Código:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
using namespace std;
#define DEFAULT_BUFLEN 1
#define PORT 80
WSADATA WSA;
SOCKET Socket;
SOCKADDR_IN Server;
HOSTENT *Info;
FILE *MyFile;
char *IP;
int Bytes;
char Buffer[DEFAULT_BUFLEN];
int Bufflen = DEFAULT_BUFLEN;
DWORD WINAPI Descargar (void* Nada);
DWORD WINAPI Descargar (void* Nada)
{
do {
Bytes = recv(Socket, Buffer, Bufflen, 0);
if(Bytes > 0){
fputs(Buffer, MyFile);
}
}while(Bytes > 0);
fclose(MyFile);
return 0;
}
int main()
{
char *Request = "GET /img/cpp-logo.png HTTP/1.1\nhost: www.cplusplus.com\r\n\r\n";
unsigned int j = 0;
MyFile = fopen("C:\\Imagen.txt","w");
int Res;
WSAStartup(MAKEWORD(2, 2), &WSA);
Socket = socket(AF_INET, SOCK_STREAM, 0);
Info = gethostbyname("www.cplusplus.com");
IP = inet_ntoa(*(struct in_addr*)(Info -> h_addr_list[0]));
Server.sin_family = AF_INET;
Server.sin_port = htons(PORT);
Server.sin_addr.s_addr = inet_addr(IP);
connect(Socket, (struct sockaddr*)&Server, sizeof(Server));
send(Socket, Request, strlen(Request), 0);
HANDLE Hand = CreateThread(NULL, 0, Descargar, NULL, NORMAL_PRIORITY_CLASS, NULL);
cin.get();
closesocket(Socket);
return (EXIT_SUCCESS);
}
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
using namespace std;
#define DEFAULT_BUFLEN 1
#define PORT 80
WSADATA WSA;
SOCKET Socket;
SOCKADDR_IN Server;
HOSTENT *Info;
FILE *MyFile;
char *IP;
int Bytes;
char Buffer[DEFAULT_BUFLEN];
int Bufflen = DEFAULT_BUFLEN;
DWORD WINAPI Descargar (void* Nada);
DWORD WINAPI Descargar (void* Nada)
{
do {
Bytes = recv(Socket, Buffer, Bufflen, 0);
if(Bytes > 0){
fputs(Buffer, MyFile);
}
}while(Bytes > 0);
fclose(MyFile);
return 0;
}
int main()
{
char *Request = "GET /img/cpp-logo.png HTTP/1.1\nhost: www.cplusplus.com\r\n\r\n";
unsigned int j = 0;
MyFile = fopen("C:\\Imagen.txt","w");
int Res;
WSAStartup(MAKEWORD(2, 2), &WSA);
Socket = socket(AF_INET, SOCK_STREAM, 0);
Info = gethostbyname("www.cplusplus.com");
IP = inet_ntoa(*(struct in_addr*)(Info -> h_addr_list[0]));
Server.sin_family = AF_INET;
Server.sin_port = htons(PORT);
Server.sin_addr.s_addr = inet_addr(IP);
connect(Socket, (struct sockaddr*)&Server, sizeof(Server));
send(Socket, Request, strlen(Request), 0);
HANDLE Hand = CreateThread(NULL, 0, Descargar, NULL, NORMAL_PRIORITY_CLASS, NULL);
cin.get();
closesocket(Socket);
return (EXIT_SUCCESS);
}
Bueno, el problema es que la respuesta del servidor es:
Código:
HTTP/1.1 200 OK
Date: Tue, 01 Oct 2013 20:24:42 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Tue, 08 Mar 2011 09:51:17 GMT
ETag: "2b60065-2434-49df58db76f40"
Accept-Ranges: bytes
Content-Length: 9268
Connection: close
Content-Type: image/png
‰PNG
IHDRÆKkitEXtSoftwareAdobe ImageReadyqÉe<PLTEˆÍgüõÿñëÿ‹Ì…ƒ{©': ETC, ETC...(IMAGEN PNG)
Date: Tue, 01 Oct 2013 20:24:42 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Tue, 08 Mar 2011 09:51:17 GMT
ETag: "2b60065-2434-49df58db76f40"
Accept-Ranges: bytes
Content-Length: 9268
Connection: close
Content-Type: image/png
‰PNG
IHDRÆKkitEXtSoftwareAdobe ImageReadyqÉe<PLTEˆÍgüõÿñëÿ‹Ì…ƒ{©': ETC, ETC...(IMAGEN PNG)
Bueno, con el editor de texto borro todo lo que tiene que ver con HTTP, pero aun asi, al abrir con el editor de texto la imagen descargada con el navegador web, veo que no es igual la codificacion, al parecer no "reconoce" ciertos caracteres al guardarlos en el archivo de texto, y corrompe el archivo.. Como podria solucionar esto ?? :/
Gracias!