Help! Http

This is a discussion on Help! Http within the Networking/Device Communication forums, part of the General Programming Boards category; Hello, I've written a program using windows sockets to connect to a website and download a JPG. My program connects ...

  1. #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.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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.

  3. #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.

  4. #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;}
         }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    jpeg is binary file - should not it be opened as binary?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User ytaipsw's Avatar
    Join Date
    Mar 2006
    Posts
    41
    yes it should be binary, that fixed it
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  2. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  3. C# HTTP request/response
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-02-2008, 06:00 AM
  4. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 09:18 AM

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