Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline kvasir

  • Full Member
  • ***
  • Join Date: Aug 2004
  • Posts: 249
    • Show all replies
    • http://watertonian.freeiz.com/1200brag/index.html
Re: Example of C source code for getting web page.
« 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 kvasir

  • Full Member
  • ***
  • Join Date: Aug 2004
  • Posts: 249
    • Show all replies
    • http://watertonian.freeiz.com/1200brag/index.html
Re: Example of C source code for getting web page.
« Reply #1 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