C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 06-17-2005, 02:09 AM   #1
Registered User
 
Join Date: Jun 2005
Posts: 2
Question winapi - socket - webdownload

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

Code:
#include <stdio.h> 
#include <errno.h> 
#include <string.h> 
#include <stdlib.h> 

/* Windows-System */ 
#ifdef _WIN32 
#include <winsock.h> 
#include <io.h> 
/* Unix-System */ 
#else 
#include <sys/socket.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <netdb.h> 
#include <unistd.h> 
#endif 

#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); 
    } 
    #endif 

    /* 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 
    closesocket(sock); 
    WSACleanup(); 
    #else 
    close(sock); 
    #endif 

    return 0; 

}
jo-the-ripper is offline   Reply With Quote
Old 06-17-2005, 07:23 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,676
Step 1 - read this Useful Links And Good Books
Step 2 - download www.ethereal.com

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.

Salem is offline   Reply With Quote
Old 06-18-2005, 07:03 PM   #3
Yes, my avatar is stolen
 
anonytmouse's Avatar
 
Join Date: Dec 2002
Posts: 2,544
HTTP uses "\r\n" as a newline, rather than just "\n".
Code:
    sprintf (command, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", file, host);
anonytmouse is offline   Reply With Quote
Old 06-19-2005, 03:51 AM   #4
Registered User
 
Join Date: Jun 2005
Posts: 2
Question Re:

Hi again,

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

i need only this lines...

Code:
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...
jo-the-ripper is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22