Author Topic: SDL: Getting the contents of a web page using SDL_NET  (Read 4098 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
SDL: Getting the contents of a web page using SDL_NET
« on: July 27, 2008, 11:28:59 PM »
 
Simple http get example using the SDL_net library.

DLL dependencies: SDL.DLL, SDL_NET.DLL.

Code: [Select]
' Simple http get example using the SDL_net library

#INCLUDE "SDL_NET.INC"

%RECVBUFFLEN = 8192
$DEFAULT_HOST = "www.powerbasic.com"

FUNCTION PBMAIN () AS LONG

   LOCAL szHostName AS ASCIIZ * 260
   LOCAL strBuffer AS STRING
   LOCAL szRecvBuffer AS ASCIIZ * %RECVBUFFLEN + 1
   LOCAL nBytes AS LONG
   LOCAL ip AS IPAddress
   LOCAL psocket AS DWORD

   szHostName = $DEFAULT_HOST

   IF SDLNet_Init <> 0 THEN
      MSGBOX "SDLNet_Init failed"
      EXIT FUNCTION
   END IF

   IF SDLNet_ResolveHost(VARPTR(ip), szHostName, 80) <> 0 THEN
      MSGBOX "SDLNet_ResolveHost failed"
      GOTO Terminate
   END IF

   psocket = SDLNet_TCP_Open(VARPTR(ip))
   IF psocket = %NULL THEN
      MSGBOX "SDLNet_TCP_Open failed"
      GOTO Terminate
   END IF

   strBuffer = "GET / HTTP/1.0" + $CRLF & _
               "Host: " & szHostName & $CRLF & _
               "Connection: close" & $CRLF & _
               "User-Agent: GetHTTP 0.0" + $CRLF & $CRLF

   IF SDLNet_TCP_Send(psocket, STRPTR(strBuffer), LEN(strBuffer)) < LEN(strBuffer) THEN
      MSGBOX "SDLNet_TCP_Send failed""
      GOTO Terminate
   END IF

   strBuffer = ""
   DO
      nBytes = SDLNet_TCP_Recv(psocket, VARPTR(szRecvBuffer), %RECVBUFFLEN)
      IF nBytes <= 0 THEN EXIT DO
      strBuffer = strBuffer & LEFT$(szRecvBuffer, nBytes)
   LOOP
   MSGBOX strBuffer

Terminate:

   IF pSocket THEN SDLNet_TCP_Close(psocket)
   SDLNet_Quit

END FUNCTION
« Last Edit: August 07, 2011, 08:04:45 PM by José Roca »