C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2009, 10:02 PM   #1
Registered User
 
ytaipsw's Avatar
 
Join Date: Mar 2006
Posts: 41
Help! Http

Hello, I've written a program using windows sockets to connect to a website and download a JPG.

My program connects to the IP, recv()'s the data, and prints it to a file.

The only thing that isn't working is that something is going wrong with TAB, CR, and LF.

When I compare the actual file, with the file my program downloaded there are extra tabs and carriage returns, and the JPG doesn't work.

Does anyone know what could have gone wrong?

Thanks.
ytaipsw is offline   Reply With Quote
Old 03-10-2009, 10:15 PM   #2
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
Well in an actual JPG, there shouldn't be TAB, CR, or LF, or at least in that representation. The same bytes most likely are. You will need to parse out the http header. How are you doing this? Some code could be helpful.
carrotcake1029 is offline   Reply With Quote
Old 03-10-2009, 11:36 PM   #3
Registered User
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 217
Extra bytes at the top of the file? If that's the case then that is part of the header. It's probably a blank line which means end of header.
39ster is offline   Reply With Quote
Old 03-11-2009, 02:51 PM   #4
Registered User
 
ytaipsw's Avatar
 
Join Date: Mar 2006
Posts: 41
the begennings and ends match
its just those three characters

heres my source:

Code:
#include <winsock.h>
#include <windows.h>
#include <stdio.h>
#include <fstream>

using namespace std;
SOCKET s;WSADATA w;
bool Connect(char* IPAddress);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
    
    ofstream i;i.open("C:\\sweet.jpg");
    
    MSG messages;bool isgo=false;char buffer[100000];int err;
    isgo=Connect("64.13.232.195");
    if(isgo) {

    send(s,"GET /media/portraits/Hot_Bikini_Brunette.jpg HTTP/2.0\r\nHost: sirimo.co.uk\r\n\r\n",78,0);
    err=recv(s,buffer,sizeof(buffer),0);
    
    err=recv(s,buffer,sizeof(buffer),0);
    for(int j=0;j<err;j+=1) {
            if(buffer[j]=='\r') {i << "\n";j+=1;}
            else {i << buffer[j];}}
   //i << "\n" << "2: "<<  err << "\n";
    
    err=recv(s,buffer,sizeof(buffer),0);
    
    if(s) {closesocket(s);}
    WSACleanup();}
    
    i.close();
    return 0;}

bool Connect(char* IPAddress) {
     WSADATA wsadata;
     int error=WSAStartup(0x0202,&wsadata);
     if(error) {return false;}
     if(wsadata.wVersion!=0x0202) {WSACleanup();return false;}
     SOCKADDR_IN target;
     target.sin_family=AF_INET;
     target.sin_port=htons(80);
     target.sin_addr.s_addr=inet_addr(IPAddress);
     s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
     if(s==INVALID_SOCKET) {return false;}
     if(connect(s,(SOCKADDR *)&target,sizeof(target))==SOCKET_ERROR) {return false;}
     else {return true;}
     }
ytaipsw is offline   Reply With Quote
Old 03-11-2009, 03:31 PM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,321
jpeg is binary file - should not it be opened as binary?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-11-2009, 06:59 PM   #6
Registered User
 
ytaipsw's Avatar
 
Join Date: Mar 2006
Posts: 41
yes it should be binary, that fixed it
thank you
ytaipsw is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error stop Http Listener George2 C# Programming 1 06-04-2008 02:14 AM
my HTTP request handler code correct? George2 C# Programming 0 04-25-2008 04:01 AM
C# HTTP request/response George2 C# Programming 0 04-02-2008 06:00 AM
Writing all HTTP requests from a website to a log file goomyman C# Programming 1 07-29-2005 09:18 AM


All times are GMT -6. The time now is 04:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22