Welcome, Guest. Please login or register.

Author Topic: Example of C source code for getting web page.  (Read 8420 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Example of C source code for getting web page.
« Reply #29 from previous page: January 22, 2006, 04:09:00 AM »
@AmigaEd

I believe it's include conflict between sys/time.h and devices/timer.h. I didn't see it as MorphOS includes account for the problem.

Anyway, clib/alib_protos.h isn't really required here as it only declares one of the prototypes (NewList()). As you noticed it works without.
 

Offline uncharted

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 1520
    • Show only replies by uncharted
Re: Example of C source code for getting web page.
« Reply #30 on: January 22, 2006, 12:22:39 PM »
I missed this thread before.

I dunno if this is still of use to you (i've just skim read most of the thread), but this site has an example program that grabs data from an HTTP server

http://www.ezcyberspace.com/gcc/part9.html

As a nice bonus it also does it with a nice MUI interface :-)

The whole tutorial is pretty good, I've used it to get GCC working in the past.
 

Offline AmigaEdTopic starter

  • His Dudeness, El Duderino
  • Hero Member
  • *****
  • Join Date: Jan 2005
  • Posts: 512
    • Show only replies by AmigaEd
Re: Example of C source code for getting web page.
« Reply #31 on: January 22, 2006, 03:35:18 PM »
@uncharted,
Hey that is a pretty nice tutorial, just what a newbie like me needs...  :-D

Thank you,
AmigaEd
"Pretty soon they will have numbers tattooed on our foreheads." - Jay Miner 1990

La Familia...
A1K - La Primera Dama -1987
A1K - La Princesa- January 2005
A2K - La Reina - February 2005
A2K - Doomy - March 2005
A500 - El Gran Jugador - April 2005
A1200 - La Hermosa Vista - May 2005
A2KHD - El Duro Grande - May 2005
A600 - PrĂ­stino - May 2005
A1200 - El Trueno Grande - July 2005
CDTV - El Misterioso - August 2005
C64 - El Gran Lebows
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Example of C source code for getting web page.
« Reply #32 on: January 22, 2006, 04:36:03 PM »
@uncharted

I wouldn't really call it that good. Here are the obvious bugs I could find from it:

- The HTTP GET is broken (missing carriage returns, missing "Host:" breaking vhosts etc).

- getservbyname and getprotobyname can return NULL. The program doesn't check for this, but read zeropage instead.

- It leaks sockets in error conditions, eventually (after 64 attempts) you would just run out  and socket() would just return error.

- The code will crash or hang if recv() returns error.

- The GUI is dead while the program is getting the data. (Ok, this is just an example so not offloading the network code to separate thread isn't that bad... :-))
 

Offline uncharted

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 1520
    • Show only replies by uncharted
Re: Example of C source code for getting web page.
« Reply #33 on: January 22, 2006, 04:46:30 PM »
I meant "good" as in easy to follow and understand.  :-)

Perhaps a constructive thing to do would be to Email the page's maintainer with a list of bugs?
 

Offline AmigaEdTopic starter

  • His Dudeness, El Duderino
  • Hero Member
  • *****
  • Join Date: Jan 2005
  • Posts: 512
    • Show only replies by AmigaEd
Re: Example of C source code for getting web page.
« Reply #34 on: January 22, 2006, 06:04:48 PM »
Yes, I also meant nice tutorial as in "easy to follow".

Piru, I understand part of the bugs you are talking about and the other half I'm lost. That's o.k. though... I'm learning and that is the important part.

I hope that someday I will be able to write some C code that works, is mostly bug free and is useful.

Best Regards,
AmigaEd


"Pretty soon they will have numbers tattooed on our foreheads." - Jay Miner 1990

La Familia...
A1K - La Primera Dama -1987
A1K - La Princesa- January 2005
A2K - La Reina - February 2005
A2K - Doomy - March 2005
A500 - El Gran Jugador - April 2005
A1200 - La Hermosa Vista - May 2005
A2KHD - El Duro Grande - May 2005
A600 - PrĂ­stino - May 2005
A1200 - El Trueno Grande - July 2005
CDTV - El Misterioso - August 2005
C64 - El Gran Lebows
 

Offline kvasir

  • Full Member
  • ***
  • Join Date: Aug 2004
  • Posts: 249
    • Show only replies by kvasir
    • http://watertonian.freeiz.com/1200brag/index.html
Re: Example of C source code for getting web page.
« Reply #35 on: March 18, 2012, 06:22:00 PM »
Quote from: koaftder;228106
haha, i'm supprised you took the time to list all the unresponsible things i did. Since you took the time to review it, i'll take the time to fix it.


Hello, I'm tryig my hand at this stuff too, and just wanted to make sure I'm on the right track here. I managed to get your code to work with some modifications, I was wondering if this is needed with the compiler/sdk I'm using (BTTR's ADE hardfile), or if I'm doing something wrong?
(Stuck //added comments to stuff I put in there)
Code: [Select]

#include <exec/libraries.h> //added
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> //added
struct Library *SocketBase = NULL; //added
int bailout(); //added

int main ( void )
{
 int socket_handle ;
 struct sockaddr_in socket_detials ;
 char * input_buffer;
 char * pinput_buffer ;
 ssize_t bytes_received ;
 ssize_t bytes_sent ;
 char * phttpget ;
 char * httpget =
 &quot;GET / HTTP/1.0\r\n&quot;
 &quot;Host: www.amiga.org\r\n&quot;
 &quot;\r\n&quot;;

 phttpget = httpget ;
 bytes_sent = 0 ;

 input_buffer = malloc(1024);
 if ( input_buffer == NULL )
 {
  printf ( &quot;Sorry, couldnt allocate memory for input buffer\n&quot; );
  return bailout() ; // return -1; to return bailout();
 }
 
 SocketBase = OpenLibrary(&quot;bsdsocket.library&quot;, 2); // added
 if(!SocketBase)
 {
  printf(&quot;Unable to open bsdsocket.library\n&quot;);
  return(10);
 }; // </added>
 
 memset ( input_buffer, 0, 1024 ) ;

 memset ( &socket_detials , 0 , sizeof(struct sockaddr_in) );

 socket_handle = socket ( AF_INET, SOCK_STREAM, 0) ;
 if ( socket_handle == -1 )
 {
  printf ( &quot;Could not create socket\n&quot; ) ;
  return bailout() ; // return -1; to return bailout();
 }
 socket_detials.sin_family = AF_INET ;
 socket_detials.sin_addr.s_addr=inet_addr(&quot;68.90.68.66&quot;);
 socket_detials.sin_port = htons(80);

 if ( connect (socket_handle,(struct sockaddr*)&socket_detials, sizeof ( struct sockaddr)) == -1 )
 {
  printf ( &quot;Couldnt connect to server\n&quot; ) ;
  return bailout() ; // return -1; to return bailout();
 }

 printf ( &quot;Attempting to send %d bytes to server\n&quot; , strlen ( httpget ) );
 for(;;)
 {
  bytes_sent = send ( socket_handle , phttpget, strlen(phttpget), 0 ) ;
  if ( bytes_sent == -1 )
  {
   printf ( &quot;An error occured sending data\n&quot; );
   return bailout() ; // return -1; to return bailout();
  }
  if ( httpget+strlen(httpget) == phttpget )
  break ;
  phttpget += bytes_sent ;
 }

 for (;;)
 {
  bytes_received = recv ( socket_handle , input_buffer , 1023, 0 ) ;
  if ( bytes_received == -1 )
  {
   printf ( &quot;An error occured during the receive procedure \n&quot; ) ;
   return bailout() ; // return 0; to return bailout();
  }
  if ( bytes_received == 0 )
  break ;
  pinput_buffer = input_buffer + bytes_received ;
  *pinput_buffer = 0 ;
  printf ( &quot;%s&quot; , input_buffer ) ;
 }
 if(SocketBase) // added
 {
  CloseLibrary(SocketBase);
  SocketBase=NULL;
 }; // </added>
 printf ( &quot;\nFinished receiving data\n&quot; ) ;
 return 0 ;
}

int bailout()
{
 if(SocketBase)
 {
  CloseLibrary(SocketBase);
  SocketBase=NULL;
 };
 return(-1);
};

Then compmilede with:

Code: [Select]

16, Work:Code/network/amiga.org>gcc working.c -o working -noixemul -Inetinclude: -lsocket
working.c: In function `main':
working.c:36: warning: assignment makes pointer from integer without a cast
Nates shell.
16, Work:Code/network/amiga.org>



Kinda wondering if the warning on 36 is a problem? I wouldn't think it is, as the assignment is the problem, and that line actually changes it (I think), but I've seen enough blinking red boxes on my TV to be paranoid. (With this setup, I actually have to change the channel to see those...)
--
Amiga 1200T 68060 50MHZ 192MB Fast
 40GB IDE, 100MB Zip, CD/RW, DVD/Rom
 Mediator+ 4MBSVGA, Soundblaster, 100mbps Ethernet
 Subway USB+ endless list of gadgets :-D
My full specs
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Example of C source code for getting web page.
« Reply #36 on: March 18, 2012, 08:40:57 PM »
@kvasir

The code is fairly good. However, this code here has a bug:
Code: [Select]
for (;;)
 {
  bytes_received = recv ( socket_handle , input_buffer , 1023, 0 ) ;
  if ( bytes_received == -1 )
  {
   printf ( &quot;An error occured during the receive procedure \n&quot; ) ;
   return bailout() ; // return 0; to return bailout();
  }
  if ( bytes_received == 0 )
  break ;
  pinput_buffer = input_buffer + bytes_received ;
  *pinput_buffer = 0 ;
  printf ( &quot;%s&quot; , input_buffer ) ;
 }
It doesn't handle the response arriving in multiple parts correctly.

Another thing to look for is the inadvertent adding of ; after blocks. It's a bad habit you should learn away from.

Trying to get HTTP GET right with custom code is probably one of the most difficult tasks. Unless if for exercise you really should use libcurl.
« Last Edit: March 18, 2012, 08:44:26 PM by Piru »
 

Offline kvasir

  • Full Member
  • ***
  • Join Date: Aug 2004
  • Posts: 249
    • Show only replies by kvasir
    • http://watertonian.freeiz.com/1200brag/index.html
Re: Example of C source code for getting web page.
« Reply #37 on: March 18, 2012, 09:57:24 PM »
Quote from: Piru;684325
@kvasir
.... It doesn't handle the response arriving in multiple parts correctly.

Another thing to look for is the inadvertent adding of ; after blocks. It's a bad habit you should learn away from.

Trying to get HTTP GET right with custom code is probably one of the most difficult tasks. Unless if for exercise you really should use libcurl.


 Thanks Piru, I think I saw an example that did a stup-up realloc() in a while loop for that. Just wanted to make sure the stuff I added in wasn't because I screwed something else up. Of all the sample code I've managed to dig up, this is the only one that I've been able to get working as well as understand.
 Probably the sort of thing I'll isolate into a aheader file and use that way. Like libcurl doesn't do that anyway, but at least this way I'm learning some stuff, too.
--
Amiga 1200T 68060 50MHZ 192MB Fast
 40GB IDE, 100MB Zip, CD/RW, DVD/Rom
 Mediator+ 4MBSVGA, Soundblaster, 100mbps Ethernet
 Subway USB+ endless list of gadgets :-D
My full specs