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)
#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 =
"GET / HTTP/1.0\r\n"
"Host: www.amiga.org\r\n"
"\r\n";
phttpget = httpget ;
bytes_sent = 0 ;
input_buffer = malloc(1024);
if ( input_buffer == NULL )
{
printf ( "Sorry, couldnt allocate memory for input buffer\n" );
return bailout() ; // return -1; to return bailout();
}
SocketBase = OpenLibrary("bsdsocket.library", 2); // added
if(!SocketBase)
{
printf("Unable to open bsdsocket.library\n");
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 ( "Could not create socket\n" ) ;
return bailout() ; // return -1; to return bailout();
}
socket_detials.sin_family = AF_INET ;
socket_detials.sin_addr.s_addr=inet_addr("68.90.68.66");
socket_detials.sin_port = htons(80);
if ( connect (socket_handle,(struct sockaddr*)&socket_detials, sizeof ( struct sockaddr)) == -1 )
{
printf ( "Couldnt connect to server\n" ) ;
return bailout() ; // return -1; to return bailout();
}
printf ( "Attempting to send %d bytes to server\n" , strlen ( httpget ) );
for(;;)
{
bytes_sent = send ( socket_handle , phttpget, strlen(phttpget), 0 ) ;
if ( bytes_sent == -1 )
{
printf ( "An error occured sending data\n" );
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 ( "An error occured during the receive procedure \n" ) ;
return bailout() ; // return 0; to return bailout();
}
if ( bytes_received == 0 )
break ;
pinput_buffer = input_buffer + bytes_received ;
*pinput_buffer = 0 ;
printf ( "%s" , input_buffer ) ;
}
if(SocketBase) // added
{
CloseLibrary(SocketBase);
SocketBase=NULL;
}; // </added>
printf ( "\nFinished receiving data\n" ) ;
return 0 ;
}
int bailout()
{
if(SocketBase)
{
CloseLibrary(SocketBase);
SocketBase=NULL;
};
return(-1);
};
Then compmilede with:
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...)