Jump to content
  • Advertisement
Sign in to follow this  
crazy_andy

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 object

CWinApp 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 this post


Link to post
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().

Good luck finding the answer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!