English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 24 April 2008, 19:30   #1
serk118
 
Posts: n/a
Floppy disk HttpGet_c_code

Hi ppl

I`m new to this forum & got a problem with httpget.c which wont download
the file from my or other sites & i`m not a socket programmer but rite now i need only this httpget.c to


email : serk118@hotmail.com
my msn : serk118@hotmail.com

thanks....

//httpget.c
//---------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <exec/types.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <time.h>
//#include <clib/socket_protos.h>
#include <sys/types.h>

struct Library * SocketBase=NULL;

extern LONG CloseSocket(LONG d);
extern ULONG inet_addr(CONST_STRPTR);
extern LONG SocketBaseTagList(struct TagItem *tagList);
//typedef struct filehandle FILE;
/* general system / socket includes */
#include <sys/types.h>
#include <sys/socket.h>

/* internet socket stuff */
#include <netinet/in.h>

/* host name resolvation/database stuff */
#include <netdb.h>

//for perrors
#include <amitcp/socketbasetags.h>

//test sites
//#define TEMPLATE "http://www.google.com/intl/en_ALL/images/logo.gif"
#define TEMPLATE "http://amigaworld.net/themes/default/images/logo-top.gif"


//#define SAVE_OUT //if you want file to be save in ram:
#define USAGE "URL/A"

int httpget(char * url);
int do_connect(char * name, int port);
void netperror(char * banner);

int Send_ALL(int sd,char *txt);



int main()
{
int rv = 20; /* default exit value */
char tmp[200];

strcpy(tmp,TEMPLATE);


if ((SocketBase = OpenLibrary("bsdsocket.library", 4)) != NULL)
{

//main call
rv = httpget(TEMPLATE);
printf("\nrv=%d\n",rv);
printf("Usage: httpget " USAGE "\n");

if(SocketBase) CloseLibrary(SocketBase);
}
else
{
printf("\nConnection FAILED due to no TCP/SOCK");
rv=20;//def
}



return(rv);
}


/*
* Let's define some useful routines for this application.
*/

int printUrlFault(void)
{
Printf("Url has to have format: [http://]host.domain.net[ort]/path\n");
return 20;
}



//And finally to our `main' routines.

int httpget(char * url)
{
char buf[1024]; /*stack usage of program should fit in 4000 bytes of memory*/
char * p, * u; /* ^^^ in current program 1k local buffer is sufficient */
int i, port, sd,size=0,x=0;


#ifdef SAVE_OUT
BPTR fp;

if((fp=Open("ram:ttest",MODE_NEWFILE))==NULL)
{
printf("\nfile error");
}
#endif

/*
* A URL may start w/ http:// or without it in this application.
*/


if (memcmp(url, "http://", 7) == 0)
url+= 7;

if ((p = strchr(url, '/')) == NULL)
return printUrlFault();

if (*p == ':')
{
/*
* user want's to access custom port/service.
*/
u = strchr(p, '/');

if (u == NULL)
return printUrlFault();

if (u == p)
{
printf("No port/service defined\n");
return 20;
}
/*
* But now, I'm too lazy to write full `getservbyname' query here,
* so only numeric port information will do
*/

if ((port = atoi(p+1)) == 0)
{
printf("Given port not numeric\n");
return 20;
}
}
else
{
u = p;
port = 80; /* standard http -service port number */
}


*p = '\0';



if ((sd = do_connect(url, port)) < 0)
return 20;

*p = '/';
/*
* I could have done a hack to write "GET " at position u - 4 and done
* a single send() call to peer. I don't know a hostname that fits in 3
* bytes and get positive answer from name server. Anyway, that would
* have been too `kludgy' way to solve the problem in this example.
*/


i = strlen(u);
u[i++] = '\n'; ///* here it is safe to replace terminating NUL w/ '\n';

// Printf("\nSending `%s'\n",u);

//Send_ALL(sd,"HEAD "); if enable this than i get different output
Send_ALL(sd,"GET ");
Send_ALL(sd,u);
Send_ALL(sd," HTTP/1.1 ");
Send_ALL(sd,"Host: www.biozombie.centelia.net\r\n");
Send_ALL(sd,"\r\n\r\n");



if ((i = recv(sd, buf,sizeof(buf),0)) > 0)
{
// Delay(10);


Write(Output(),buf,i);
#ifdef SAVE_OUT
Write(fp,buf,sizeof(buf));
#endif
}


#ifdef SAVE_OUT
Close((BPTR)fp);
#endif

if (i != 0)
{
netperror("recv");
return 20;
}

return 0;
}



//Stuff to make connection. Reuse freely.
int getSockAddr(const char * host,int port,struct sockaddr_in * addr)
{
int fsize=0;
struct hostent * remote;

if ((remote = gethostbyname(host)) != NULL)
memcpy(&addr->sin_addr, remote->h_addr, sizeof addr->sin_addr);
else if ((addr->sin_addr.s_addr = inet_addr(host)) == (unsigned long)-1)
{
return FALSE;
}

addr->sin_family = AF_INET;
addr->sin_port = htons(port);

return TRUE;
}

const int ONE = 1;

int inetconn (const struct sockaddr_in * addr)
{
int sd;


if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
netperror ("socket");
return -1;
}

setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, &ONE, sizeof ONE);

if (connect(sd, (struct sockaddr *)addr, sizeof (struct sockaddr_in)) < 0)
{
CloseSocket(sd);
netperror("connect");
return -1;
}

return sd;
}

Last edited by serk118; 26 April 2008 at 18:51.
 
Old 26 April 2008, 18:52   #2
serk118
 
Posts: n/a
re

its ok ppl the problem solve`d
 
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:30.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.13674 seconds with 12 queries