Thread: Need advice

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Need advice

    Good afternoon people.

    I have managed to download a .gif picture from a webserver using socket programming and stored it in a binary file. The problem is that I cant view the picture, Windows Picture and Fax viewer says "Drawing failed". So I downloaded the same picture using internet explorer and found out that the files are almost the same size, one is 8558 bytes and the other is 8695. I opend the two files up in wordpad and judging by the first couple of lines the two files look alike but it's madness to try and compare two such big files manually. So I was wondering if anyone know of a good program to compare to binary files and show the difference.

    Cheers for any help.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    If i'm not mistaken, MSVC++ 6 has a file comparative feature.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In your socket program, did you open your output file stream in binary mode?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Hammer
    In your socket program, did you open your output file stream in binary mode?
    Yes.

    Here is part of my code :

    Code:
    int socketfd=0;	
    char buf[2000];
    if((m_connect(domain, path, socketfd)) == false)
    {
      exit(0);
    }
    
    fstream ofile(file.c_str(), ios::out | ios::binary);
    
    int recv_bytes;
    while((recv_bytes = recv(socketfd, buf, sizeof(buf),0)) > 0)
    {
      ofile.write(buf, recv_bytes);
    }
    
    m_close(socketfd);
    m_connect and m_close are methods I've create to connect to disconnect a socket connection.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    After a bit of search on the web did I find Beyond Compare which I very good program for comparing files and directorys.

    I figure out the difference between the two files but Im not sure why they happen.

    Here is a sample from the working .gif picture file :
    Code:
    GIF89an
    Here is from the file I downloaded into :
    Code:
    GIF89an   
    notice there it is missing three  in the file I downloaded into.


    Also, from the working file :
    Code:
    ha!F:20$~am|
    and my file :
    Code:
    ha!F:20$~a
    m|
    Notice here how my file is missing one and has a newline.

    I have no idea what to make of this. Would appreciate some advice.

    Cheers

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Anyone ?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm interested in helping you, can you post something that'll compile?

    BTW, please don't bump your threads.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Instead of viewing weird characters in a text editor, open your file in a hex editor and see what's really happening.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Hammer
    I'm interested in helping you, can you post something that'll compile?

    BTW, please don't bump your threads.
    Sorry about the bump, actions of a desperate man I guess

    Code:
    #include "stdafx.h"
    #include <afx.h>
    #include "winsock2.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	char buf[2000];
    	int socketfd;
    	WSADATA wsaData;
    
    	if (WSAStartup(MAKEWORD( 2, 2 ), &wsaData ) != 0 ) 
    	{ 
    		cout << "Wsastartup failed" << endl;
    		exit(0); 
    	}
     
    	if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion )!= 2) 
    	{
    		WSACleanup( );
    		exit(0);
    	} 
    		
    	if ((socketfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    	{
    		cout << "socket failed" << endl;
    		exit(0);
    	}
    
    	string domain = "www.google.com";
    	string path = "/images/logo.gif";
    
    	LPHOSTENT server;
    	server = gethostbyname(domain.c_str());
    
    	if (server == 0)
    	{
    		cout << "server failed" << endl;
    		exit(0);
    	}
    		
    	SOCKADDR_IN dest_addr;
    	dest_addr.sin_family      = AF_INET;
    	dest_addr.sin_addr.s_addr = *(unsigned long int*)server->h_addr_list[0];
    	dest_addr.sin_port        = htons(80);
    
    	if (connect(socketfd, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) == -1)
    	{
    		cout << "connect failed" << endl;
    		exit(0);
    	}
    	
    	string command = "GET ";
    	command.append(path);
    	command.append(" HTTP/1.1\nhost:");
    	command.append(domain);
    	command.append("\r\n\r\n");
    
    	if (send(socketfd, command.c_str(), command.length(), 0) == -1)
    	{
    		cout << "send failed" << endl;
    		exit(0);
    	}
    
    	fstream ofile("logo.gif", ios::out | ios::binary);
    
    	string buffer;
    	int recv_bytes;
    	while((recv_bytes = recv(socketfd, buf, sizeof(buf),0)) > 0)
    	{
    		ofile.write(buf, recv_bytes);
    		cout << "#";
    	}
    	cout << endl;
    
    	ofile.close();
    	closesocket(socketfd);		
    	WSACleanup();
    
    	cout << "Finished" << endl;
    	return 0;
    }
    This code should create a file logo.gif and save the data to it. For the moment I have to removed the http header info that also is writen to the file manually via notepad. Will try and implement a removeHeader() method as soon as I know this will actually work.

    Appreciate any response.
    Last edited by laasunde; 02-10-2003 at 10:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. Resume advice
    By Zughiaq in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-15-2005, 02:16 PM
  4. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  5. Partitioning advice?
    By mart_man00 in forum Tech Board
    Replies: 6
    Last Post: 01-10-2003, 10:53 PM