Help with sockets to download image files (JPG, GIF) in C/C++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Help with sockets to download image files (JPG, GIF) in C/C++
Hello I have written a program to download files from a server. This program creates the file where will be written the data from the buffer named "Buffer" with:
archivo.open("file.JPG");
so it's only for JPG, and if I want to download a txt, I change it to:
archivo.open("file.txt");
A txt can be downloaded ok, but my problem it's when I download a JPG or a GIF, the file it's not ok and the image isn't displayed as the original.
Here is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <winsock.h>
#pragma comment(lib,''ws2_32.lib'')
#include <conio.h>
#include <fstream>
#include <iostream>
#define LEN_BUFF 512
using namespace std; // Esta linea sirve para poder usar la libreria iostream
int main(int argc, char* argv[])
{
ofstream archivo;
SOCKET sockfd;
int Recv;
WSADATA wsaData;
struct hostent *hp;; // Manejará el nombre del servidor destino
unsigned long addr = 0;
struct sockaddr_in client;
char Buffer[LEN_BUFF];
memset(Buffer, '\0', sizeof(Buffer));
int wsaret=WSAStartup(0x101,&wsaData);
if (wsaret!=0){
printf("%s: Ha ocurrido un error en WSAStartup()\n",argv[0]);
return 0;
}
// Valores de la estructura
memset((char *) &client, 0, sizeof(client));
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=htons((u_short)80);
// Se requiere comprobar si el nombre del servidore está en formato X.X.X.X o DNS.
if(inet_addr(argv[1])==INADDR_NONE){
hp=gethostbyname(argv[1]); // NO está en formato X.X.X.X
}
else{
addr=inet_addr(argv[1]); // SI está en formato X.X.X.X
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}
if(hp==NULL){
printf("%s: Host %s desconocido\n",argv[0],argv[1]);
return 0;
} // Al final se quiere trabajar con host
// Si el host antes estaba con nombre DNS se hace gethostbyname
// Si el host antes estaba con IP se hace gethostbyaddr
memcpy((char *) &client.sin_addr, hp->h_addr, hp->h_length);
//Comienza la conexión
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET){
printf("%s: Ha ocurrido un error en socket()\n",argv[0]);
return 0;
}
if (connect(sockfd, (struct sockaddr *) &client, sizeof(client)) < 0) {
printf("%s: Ha ocurrido un error en connect()\n",argv[0]);
return 0;
}
memset(Buffer, '\0', sizeof(Buffer));
sprintf(Buffer,"GET %s\r\n\r\n",argv[2]);
send(sockfd,Buffer,strlen(Buffer),0);
archivo.open("file.JPG");
if(archivo.fail()){
cerr << "Error al abrir el archivo" << endl;
exit(1);
}
while(Recv=recv(sockfd,Buffer,sizeof(Buffer),0))
{
archivo.write(Buffer,Recv);
}
archivo.close();
cout << "new message: " << Buffer << " -- Total: " << strlen(Buffer) << " bytes\r\n";
closesocket(sockfd);
WSACleanup();
return 0;
}
If someone can help me to improve my code I will appreciate it.
Thanks in advance,
Pedro
Last edited by pedropolian; 06-09-2009 at 01:20 AM.
Reason: the title should be modified
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.