# send(c,"GET /big.gif",0)

This topic is 4787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im continuing learning about winsock, bu writing a program that downloads files of the internet. It works fine on < 200kb file. when I try doing a 10mb file, it gets anout 20kb then stops. here is some code: send (c,"GET %s\r\n\r\n ",filepath,0); int t = 0; int notify = 2000; do { t = recv(c,buff,512,0); if (test) { cout << "recieved " << buff << endl; } chrecv += t; if (t!= 0) { f.Write(buff,t); } if (chrecv > notify) { cout << notify/1000 << "kb downloaded\n"; notify += 1000; } }while (t!=0); If any one has any ideas, please post hem.

##### Share on other sites
Try to add a keep-alive command:

"Connection: keep-alive"

Or with proxy:

"Proxy-Connection: keep-alive"

Other than that, i couldn't say why it shouldn't work.

##### Share on other sites
OK, thanks for the advice, but where would the "Connection: keep-alive" command go. Do I send it along with the filepath, or do I use it when connecting?

##### Share on other sites
Below the get command, simply make it the next line.

There's a heap of html parameters you send like this. Make sure to have the double carriage return (\r\n) AFTER the parameter.

Like this:

send (c,"GET %s\r\nConnection: keep-alive\r\n\r\n ",filepath,0);

##### Share on other sites
Also make sure you are using the right type for the variable that keeps file size (ie you are using INT for variable called notify), it will not make it for big files.

##### Share on other sites
cheers,I will test that now, the notify was only put in as a test, I will change it to only increase by two rather than 2000. And when It works properly I will only notify ever 100kb or something.

Oh Also, Do you know where I can look up other commands like this.

##### Share on other sites
It now gets 31kb at max, but not quite 10mb.
heres the code.

edit: the file Im trying to download is a movie player. reason: its 10mb

// clientv01.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "clientv01.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// The one and only application objectCWinApp theApp;using namespace std;CFile f;SOCKET c;char buff[512] = "";int chrecv = 0;void ParseUrl(char url[512], char servername[512], char filepath[512], char filename[100]){	int j = 0;	bool error = false;	int temp = 0;	for (int i = 0;i< 512;)	{		if (url[j] == 0)		{			error = true;			break;		}		if (url[j] == 47)		{			temp++;			if (temp == 3)			{				break;			}		}		else		{			if (temp > 1)			{				servername = url[j];				i++;			}		}		j++;			}	for (int i = 0;i< 512;i++)	{		if (url[j] == 0)		{			break;		}				filepath = url[j];		j++;	}	int count = 0;	for (int i = 0; i< 512;i++)	{		j--;		if (url[j] == 47)		{			break;		}		count++;	}	j++;	filename[0] = 99;	filename[1] = 58;	filename[2] = 92;	for (int i = 3;count>0;i++)	{		filename = url[j];		j++;		count--;	}}bool client(const char *hostname,char filepath[512], char filename[100]){	WSADATA info;	if (WSAStartup(0x101,&info)!=0)	{		cout << "ERROR seting up winsock\n";		return false;	}	c = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);	struct hostent *hp;	struct sockaddr_in sa;	if (c == INVALID_SOCKET)	{		cout << "Invalid socket\n";		return false;	}	cout << "set up socket\n";	if (inet_addr(hostname) == INADDR_NONE)	{		hp = gethostbyname(hostname);	}	else	{		unsigned int addr = inet_addr(hostname);		hp = gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);	}	if (hp == NULL)	{		cout << "Error getting host info\n";		closesocket(c);		return false;	}	cout << "Got host info\n";	sa.sin_addr.s_addr = *((unsigned long*)hp->h_addr);	sa.sin_family = AF_INET;	sa.sin_port = htons(80);    	if (connect(c,(struct sockaddr*)&sa,sizeof(sa)))	{		cout << "Error connecting\n";		return false;	}	cout << "connected\n";	f.Open(filename,CFile::modeCreate | CFile::modeWrite);	cout << "requesting file\n";	sprintf(buff,"GET %s\r\nConnection: keep-alive\r\n\r\n ",filepath);	send(c,buff,(int)strlen(buff),0);    	cout << "writting\n";	int t = 0;	int notify = 2;	do	{		t = recv(c,buff,512,0);		chrecv += t;		if (t!= 0)		{			f.Write(buff,t);		}		if (chrecv/1024 >  notify)		{			cout << chrecv/1024 << "kb downloaded\n";			notify += 2;		}	}while (t!=0); 	cout << "File downloaded, size = " << (float)chrecv/1024 << "kb\n";	cout << "located: " << filename << "\n";		f.Close();	closesocket(c);		return true;}int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){	int nRetCode = 0;	// initialize MFC and print and error on failure	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))	{		// TODO: change error code to suit your needs		_tprintf(_T("Fatal Error: MFC initialization failed\n"));		nRetCode = 1;		return nRetCode;	}	char url[512] = "", servername[512] = "",filepath[512] = "",filename[100] = "";//	cin.getline(url,512);//	ParseUrl(url,servername,filepath,filename);	ParseUrl("http://download.videolan.org/pub/videolan/vlc/0.1.8/win32/vlc-0.1.8-win32.zip",servername,filepath,filename);	client(servername,filepath,filename);	WSACleanup();	cout << "Thank you for using file downloader.\n";	cout << "Created by Neil Stevenson\n";	cout << "Press any key to quit.\n";	_getch();	return nRetCode;}

##### Share on other sites
I'm almost sure this is not the fix for it, but if I remember correctly, recv()'s destination buffer (buff) should be 1 greater than the length you pass in. So if you recv() 511 bytes, it would be saver. Of course you could also just make buff 513 bytes long.

Another tip: memset() the buffer to 0 before you call sprintf().

##### Share on other sites
cheers, like you said thats probably not the fix but I will try it anyway.

##### Share on other sites
Quote:
 Original post by crazy_andyOh Also, Do you know where I can look up other commands like this.

Assuming you're using HTTP1.1, you'll need RFC2616

##### Share on other sites
Quote:
 Original post by crazy_andycheers, like you said thats probably not the fix but I will try it anyway.

You can try HTTP Made Really Easy[http://www.jmarshall.com/easy/http/]

##### Share on other sites
cheers, I will read those, but I reallyt should study for my mock oracle exam tommorow, ah well.

##### Share on other sites
cheers, both those links were pretty usefull.

My problem was that I wasn't accounting for the headers, I was reading the header, and no more. I've solved that now.

however when I try to get a file on my website http://www.geocities.com/andrew_parlane2000/penguin.gif
I send:

GET /andrew_parlane2000/penguin.gif
HOST: www.geocities.com
Connection: close

I get no response. my recv times out after about 30 secconds, returning -1.
I know this file exists, and I got the url correct, because I copied it directly from IE. Even if it was wrong, It should have responded with http/1.1 404 File Not found.

This has happened with a few files. The servers arn't down, because I can get the files in IE.

I am not sure what I can do about it.

##### Share on other sites
I'd guess especially geocities does a few checks for parameters as they try to have their bandwidth not leeched.

Try sniffing the exact get header that IE or Firefox or whatever sends, and use those.

I'm using the following header and it works for me:

GET http://www.geocities.com/andrew_parlane2000/penguin.gif HTTP/1.0
Connection: keep-alive

Might be the HTTP bit at the end.

##### Share on other sites
I sorted that out Im using http/1.1

##### Share on other sites
Use /1.0

1.1 can do things that make writing the client a bit harder.

##### Share on other sites
do all servers support 1.0,
What sort of disadvantage, would I get from using the older version?

edit: Ok, I just sent http/1.0
and it responded, with two different files, off two different sites:

http/1.1 200 OK

or basically the same as when I sent 1.1, at least I get a response though.

[Edited by - crazy_andy on January 9, 2005 7:00:54 PM]

##### Share on other sites
OK, I havn't solved the problem yet, but here is aother question anyway?I try running this on a computer network, with a proxy, the connect statement times out. I think its the proxy, so does anyone know how I can connect through a proxy?

##### Share on other sites
You have to connect to the computer with the proxy and then ask it for the webpage using the HOST: line.

It's been a while sense i've done this, but I'll take a shot. Let's say the proxy is on 192.168.0.5, port 80, and you want google:

connect 192.168.0.5:80
send-

GET / HTTP/1.1
Connection: <whatever>

Then you will recieve the information from the proxy, rather than from the actual website. In other words, the proxy is middle-man. This is for HTTP proxy, I believe, not sure about SOCKS. If i'm wrong please go ahead in correct me, like i said, havent done this in a bit, but I hope i helped.

~zix~