LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2009, 01:17 AM   #1
pedropolian
LQ Newbie
 
Registered: Mar 2009
Posts: 6

Rep: Reputation: 0
Exclamation 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
 
Old 06-09-2009, 02:41 AM   #2
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 34
Hello. Allow me to make a suggestion even though I've only taken a quick glance over your code:

Code:
archivo.open("file.JPG", ios::out | ios::binary);
 
Old 06-11-2009, 01:06 AM   #3
pedropolian
LQ Newbie
 
Registered: Mar 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Hello noctilucent, it works!

Thanks
Pedro
 
Old 08-11-2012, 11:21 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by khaint19 View Post
What is the input link of your program???
And the reason for asking after 3 years is ?

Please note that the problem was solved
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
jpg to gif conversion damonj Linux - General 2 11-03-2008 06:13 AM
gnuplot 4.2 no png, jpg or gif terminals datasink Programming 4 04-26-2007 10:13 AM
any software on linux can joint a series of jpg to a gif? hkl8324 Linux - General 6 06-22-2005 12:14 PM
(gentoo) gnome without tiif, gif or jpg support??? ElPuello Linux - Software 1 05-31-2004 04:38 AM
How to convert macintosh pic to gif/jpg lervere Linux - General 1 07-22-2001 08:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration