john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

httpget cnet

/* john pfeiffer 17jul08   interactive network commands - telnet like program, 
returns the commands typed in and the slashdot.org home page and writes them to log.txt
run the program without any parameters for help
use gcwinsock.bat (gcc.exe -o %1.exe %1 -lws2_32 -lwsock32 -Iinclude -Llib)
*/

#include <stdio.h>
#include <stdlib.h>     
#include <winsock.h>    /* you MUST load static libaries libwsock32.a & lws2_32.a
                        gcc.exe -o %1.exe %1 -lws2_32 -lwsock32 -Iinclude -Llib        */
#define BUFFERMAX 1024

void usage(void)
{   printf("error, proper usage: domain port logfile.txt, e.g.\n\n");
    printf("slashdot.org 80 logfile.txt\n");    
    printf("GET / HTTP/1.0 \n");
    printf("host: slashdot.org\n");
    printf("press enter to quit entering commands and receive from the server");

}/* end of func usage */

void zerobuffer(char buffer[BUFFERMAX])
{   int i;
    for(i =0; i< BUFFERMAX; i++)
    {   buffer[i] = 0; }
}/* end of func zerobuffer */

int main(int argc, char* argv[])
{
    int bytes_rcvd=0;
    short port;                 /* port number on remote host */
    char buffer[BUFFERMAX];
    char command[32];           /* get or post */
    struct sockaddr_in a;       /* sockaddr_in(ternet) */
    struct hostent* Hostnameptr;       /* if the parameter is a name, not an ip addr */
    WSADATA ws;
    FILE* ofp;

    int d = WSAStartup(0x0202,&ws);                     /* addr family internet, stream, TCP = 0 */
    SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  /* get handle to socket (should be > 0) */

    a.sin_family = AF_INET;                         /* internet protocol addressing */  
    port=atoi( argv[2] );                           /* convert the ascii string to a number */
    a.sin_port = htons( port );                     /* connect to port number to Network Byte Order */
    a.sin_addr.s_addr = inet_addr(argv[1]);          /* the first parameter, e.g. slashdot.org = 66.35.250.150  */

    if( a.sin_addr.s_addr == INADDR_NONE )          /* non existent ip address? */
    {   Hostnameptr = gethostbyname(argv[1]);       /* gethostbyname must be in lowercase! */
        a.sin_addr.s_addr = *(u_long *) Hostnameptr->h_addr_list[0]; /* extract the hostname after lookup */    
    }

    printf("WSAStartup = %d, ",d);                           
    d = connect( s, (struct sockaddr *) &a, sizeof(a) );         /* must cast */
    printf(" SOCKET = %d, Connected? %d (should be: 0)\n",s,d);                           
    if( d < 0 )
    {    printf("\nERROR GETTING SOCKET\n");  
         usage();
         exit(1);  
    }
    if( argc < 4 )
    {   usage();
        exit(1);   
    }

    ofp = fopen(argv[3], "w");
    if( ofp == NULL ){    printf("ouptut file error\n");    exit(1);    }
    printf("%s writing session with %s(%s) port %s to %s\n",argv[0], argv[1],inet_ntoa( a.sin_addr ), argv[2], argv[3]);

    zerobuffer(buffer);
    do
    {
        gets(command);

        if( command[0] != 0 )
        {   strcat(buffer, command);        
            strcat(buffer,"\r\n");   
        }        
    }
    while( command[0] != 0 );   /* empty line - just hitting the return key to quit */

    strcat(buffer,"\r\n");       
    puts(buffer);
    fputs(buffer,ofp);


    send(s,buffer,sizeof(buffer),0);      /* the zero at the end?  */

    do{
        zerobuffer(buffer);
        bytes_rcvd = recv(s,buffer,BUFFERMAX-1,0);

        if ( bytes_rcvd > 0 )
            printf("Bytes received: %d\n", bytes_rcvd);
        else if ( bytes_rcvd == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed: %d\n", WSAGetLastError());

        puts(buffer);
        fputs(buffer,ofp);

    }while( bytes_rcvd > 0 );

    fclose(ofp);

    shutdown(s,1);          /*better than close()  */
    closesocket(s);
    WSACleanup();

    return 0;
} /*end main*/

  • « file tabs to 4 spaces
  • file linked list count brackets list v2 unfinished »

Published

Jul 17, 2008

Category

c

~357 words

Tags

  • c 95
  • cnet 1
  • httpget 2