Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

nicht funktionierendes httpget()

Uploader:MitgliedPMedia
Datum/Zeit:08.01.2008 00:33:06

#ifdef __FB_WIN32__
#include once "win/winsock2.bi"
#else
#include once "crt/netdb.bi"
#include once "crt/sys/socket.bi"
#include once "crt/netinet/in.bi"
#include once "crt/arpa/inet.bi"
#include once "crt/unistd.bi"
#endif

#ifndef recvbufflen
#define RECVBUFFLEN 8192
#endif
#ifndef newline
#define newline chr(13,10)
#endif

Sub InitWinsock Constructor
    #ifndef initwinsock
    #define initwinsock
    #ifdef __FB_WIN32__
    '' init winsock
    Dim wsaData As WSAData

    If( WSAStartup( MAKEWORD( 1, 1 ), @wsaData ) <> 0 ) Then
        Print "Error: WSAStartup failed"
        End 1
    End If
    #Endif
    #Endif
End Sub

Sub ExitWinsock Destructor
    #ifndef deinitwinsock
    #define deinitwinsock
    #ifdef __FB_WIN32__
    WSACleanup
    #Endif
    #Endif
End Sub

Function httpget(server As String, path As String) As String
    Dim IP As Integer
    Dim ia As in_addr
    Dim s As SOCKET
    Dim hostentry As hostent Ptr
    Dim sendbuffer As String
    Dim recvbuffer As Zstring * RECVBUFFLEN+1
    Dim bytes As Integer
    Dim sa As sockaddr_in

    ia.S_addr = inet_addr( server )
    If ( ia.S_addr = INADDR_NONE ) Then
        hostentry = gethostbyname( server )
        If ( hostentry = 0 ) Then
            return "IP couldn't be resolved!"
        End If
        IP = *cast( Integer Ptr, *hostentry->h_addr_list )
    Else
        IP = ia.S_addr
    End If
    s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    If( s = 0 ) Then
        return "Socket couldn't be opened."
    End If
    sa.sin_port         = htons( 80 )
    sa.sin_family       = AF_INET
    sa.sin_addr.S_addr  = ip
    If ( connect( s, cast( PSOCKADDR, @sa ), Len( sa )) = SOCKET_ERROR ) Then
        closesocket( s )
        return "Couldn't connect to host"
    End If
    sendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _
    "Host: " + server + NEWLINE + _
    "Connection: close" + NEWLINE + _
    NEWLINE
    If( send( s, sendBuffer, Len( sendBuffer ), 0 ) = SOCKET_ERROR ) Then
        closesocket( s )
        return "Couldn't send request"
    End If
    Do
        bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
        If( bytes <= 0 ) Then
            Exit Do
        End If
        recvbuffer[bytes] = 0
    Loop
    shutdown( s, 2 )
    closesocket( s )
    return recvbuffer
End Function


Print httpget("pmedia.max06.de", "/index.html")
Sleep