Thread: winapi - socket - webdownload

  1. #1
    Registered User
    Join Date
    Jun 2005

    Question winapi - socket - webdownload

    Hi. can somebody help me. i want to get the completly download of an html -> in one string.

    #include <stdio.h> 
    #include <errno.h> 
    #include <string.h> 
    #include <stdlib.h> 
    /* Windows-System */ 
    #ifdef _WIN32 
    #include <winsock.h> 
    #include <io.h> 
    /* Unix-System */ 
    #include <sys/socket.h> 
    #include <sys/types.h> 
    #include <netinet/in.h> 
    #include <arpa/inet.h> 
    #include <netdb.h> 
    #include <unistd.h> 
    #define HTTP_PORT 80 
    int main(int argc, char **argv) 
        int sock; 
        struct sockaddr_in host_addr; 
        struct hostent *hostinfo; 
        char *host, *file; 
        char command[1024]; 
        char buf[1024]; 
        int bytes_sent, bytes_recv; 
        /* Ist der Aufruf korrekt? */ 
        if (argc != 3) { 
            fprintf (stderr, "Aufruf: httprecv host file\n"); 
            exit (EXIT_FAILURE); 
        host = argv[1]; 
        file  = argv[2]; 
        /* ggf. Winsock initialisieren */ 
        #ifdef _WIN32 
        WSADATA wsaData; 
        if (WSAStartup (MAKEWORD(1, 1), &wsaData) != 0) { 
            fprintf (stderr, "WSAStartup(): Kann Winsock nicht initialisieren.\n"); 
            exit (EXIT_FAILURE); 
        /* Socket erzeugen */ 
        sock = socket (AF_INET, SOCK_STREAM, 0); 
        if (sock == -1) { 
            perror ("socket()"); 
            exit (EXIT_FAILURE); 
        /* Adresse des Servers festlegen */ 
        memset( &host_addr, 0, sizeof (host_addr)); 
        host_addr.sin_family = AF_INET; 
        host_addr.sin_port = htons (HTTP_PORT); 
        host_addr.sin_addr.s_addr = inet_addr (host); 
        if (host_addr.sin_addr.s_addr == INADDR_NONE) { 
            /* Server wurde nicht mit IP sondern mit dem Namen angegeben */ 
            hostinfo = gethostbyname (host); 
            if (hostinfo == NULL) { 
                perror ("gethostbyname()"); 
                exit (EXIT_FAILURE); 
            memcpy((char*) &host_addr.sin_addr.s_addr, hostinfo->h_addr, hostinfo->h_length); 
        /* Verbindung aufbauen */ 
        if (connect(sock, (struct sockaddr *) &host_addr, sizeof(struct sockaddr)) == -1) { 
            perror ("connect()"); 
            exit (EXIT_FAILURE); 
        /* HTTP-GET-Befehl erzeugen */ 
        sprintf (command, "GET %s HTTP/1.0\nHost: %s\n\n", file, host); 
        /* Befehl senden */ 
        bytes_sent = send (sock, command, strlen (command), 0); 
        if (bytes_sent == -1) { 
            perror ("send()"); 
            exit (EXIT_FAILURE); 
        // Antwort des Servers empfangen und ausgeben */ 
        while ((bytes_recv = recv (sock, buf, sizeof(buf), 0)) > 0) { 
            write (1, buf, bytes_recv); 
        if (bytes_recv == -1) { 
            perror ("recv()"); 
            exit (EXIT_FAILURE); 
        printf ("\n"); 
        #ifdef _WIN32 
        return 0; 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Step 1 - read this Useful Links And Good Books
    Step 2 - download

    Ethereal is just so damn useful at watching networks that it's folly to try anything without it. Use it to compare what you do with what a browser does. Then understand and fix the differences.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    HTTP uses "\r\n" as a newline, rather than just "\n".
        sprintf (command, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", file, host);

  4. #4
    Registered User
    Join Date
    Jun 2005

    Question Re:

    Hi again,

    thanks for the replys....but there couldn't help me...

    i need only this lines...

    while ((bytes_recv = recv (sock, buf, sizeof(buf), 0)) > 0) 
    {write (1, buf, bytes_recv);       
    i want to have all the cutted parts of buf in on complete string...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  3. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 01:51 AM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 09:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM