Menu

please help with http client.

Anonymous
2002-05-31
2012-09-26
  • Anonymous

    Anonymous - 2002-05-31

    This logic is compiling but I get a program error while trying to run. Does anyone notice anything incorrect with this logic.

    #include <windows.h>
    #include <stdio.h>

    WSADATA ws;
    int d;
    char aa[100];
    struct sockaddr_in a;
    SOCKET s;
    int ii;
    struct hostent *hent = gethostbyname("yahoo.com");

    void abc(char *p){
        FILE *fp = fopen("c:\\getThatPage.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
    }

    int STDCALL WinMain (HINSTANCE i, HINSTANCE j, char *k, int l){

            d = WSAStartup(0x101,&ws);
            sprintf(aa," WSASTARTUP = %d",d);
            abc(aa);
            s = socket(AF_INET,SOCK_STREAM,0);
            sprintf(aa," SOCKET = %d",s);
            abc(aa);
            a.sin_family = AF_INET;
            a.sin_port = htons(80);
            a.sin_addr.s_addr = ((struct in_addr *)(hent->h_addr))->s_addr;
            d = connect(s, (struct sockaddr *)&a, sizeof( a));
            strcpy(aa, "GET / \r\n");
            strcat(aa, "HTTP 1.0 \r\n\r\n");
            send(s,aa,sizeof(aa),0);
            ii = 1;
            while (ii != 0)
            {
                ii = recv(s,aa,100,0);
                abc(aa);
            }
               WSACleanup();
    }

    I have also linked the wsock32 file: -lwsock32

    Thank you,
    Daniel Watson

     
    • Scott Dial

      Scott Dial - 2002-06-03

      struct hostent *hent = gethostbyname("yahoo.com");

      that call takes place before WSAStartup(...).

       
    • Anonymous

      Anonymous - 2002-06-03

      Thanks Scott,
      works great.

      Daniel

       

Log in to post a comment.