-
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.
-
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.
-
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.
-
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;}
}
-
jpeg is binary file - should not it be opened as binary?
-
yes it should be binary, that fixed it
thank you