BCX Console Demonstration Program S128.bas

 ' ---------------------------------------------------------------
 '   Console utility used to retrieve files from a HTTP server.
 ' ---------------------------------------------------------------
 ' From the book WinSock 2.0 by Lewis Napper covering programming
 ' with sockets.      Adapted to BCX BASIC by Kevin Diggins
 ' ---------------------------------------------------------------
 $HEADER
 #if defined (__LCC__)
  #include <winsock2.h>
 #endif
 $HEADER
 
 $LIBRARY <WSOCK32.LIB> ' LINK WITH WSOCK32.LIB
 
 #INCLUDE <fcntl.h>
 
 GLOBAL wsaData AS WSADATA
 GLOBAL wVersionRequested
 GLOBAL nRet
 
 wVersionRequested = MAKEWORD(1,1)
 
 IF ARGC <> 3 THEN
   PRINT "Syntax : GetHTTP ServerName PathAndFileName"
   PRINT "Example: GetHTTP www.yahoo.com index.html"
   END
 END IF
 
 nRet = WSAStartup(wVersionRequested, &wsaData)
 
 IF nRet THEN
   PRINT "WSAStartup()", nRet
   WSACleanup()
   END
 END IF
 
 IF wsaData.wVersion <> wVersionRequested THEN
   PRINT "WinSock version not supported"
   WSACleanup()
   END
 END IF
 
 setmode(_fileno(stdout), _O_BINARY)
 
 GetHTTP (ARGV[1], ARGV$[2])
 
 WSACleanup()
 
 SUB GetHTTP (lpServerName$, lpFileName$)
   ' =======================================
   DIM  LOCAL  nRet
   DIM  LOCAL  iaHost       AS  IN_ADDR
   DIM  LOCAL  lpHostEntry  AS  LPHOSTENT
   DIM  LOCAL  Socket       AS  SOCKET
   DIM  LOCAL  lpServEnt    AS  LPSERVENT
   DIM  LOCAL  saServer     AS  SOCKADDR_IN
   DIM  LOCAL  szBuffer$
   ' =======================================
 
   iaHost.s_addr = inet_addr(lpServerName)
 
   IF iaHost.s_addr = INADDR_NONE THEN
     lpHostEntry = gethostbyname(lpServerName$)
   ELSE
     lpHostEntry = gethostbyaddr((char*)&iaHost, SIZEOF(IN_ADDR), AF_INET)
   END IF
 
   IF lpHostEntry = NULL THEN
     PRINT "Error: HOST NOT FOUND : gethostbyname()"
     END
   END IF
 
   Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
 
   IF Socket = INVALID_SOCKET THEN
     PRINT "Error: INVALID_SOCKET : socket ()"
     END
   END IF
 
   lpServEnt = getservbyname("http", "tcp")
 
   IF lpServEnt = NULL THEN
     saServer.sin_port = htons(80)
   ELSE
     saServer.sin_port = lpServEnt->s_port
   END IF
 
   saServer.sin_family = AF_INET
   saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list)
   nRet = connect(Socket, (LPSOCKADDR)&saServer, SIZEOF (SOCKADDR_IN))
 
   IF nRet = SOCKET_ERROR THEN
     PRINT "Error: SOCKET_ERROR : connect()"
     closesocket(Socket)
     END
   END IF
 
   szBuffer$ = "GET " & CHR$(13) & CHR$(10) & lpFileName$
   nRet      =  send(Socket, szBuffer$, LEN(szBuffer$), 0)
 
   IF nRet = SOCKET_ERROR THEN
     PRINT "Error: SOCKET_ERROR : send()"
     closesocket(Socket)
     END
   END IF
 
   WHILE (TRUE)
     nRet = recv(Socket, szBuffer$, SIZEOF(szBuffer$), 0)
     IF nRet = SOCKET_ERROR THEN
       PRINT "Error: SOCKET_ERROR : recv()"
       EXIT LOOP
     END IF
     IF nRet = 0 THEN
       EXIT LOOP
     END IF
     PRINT szBuffer$
   WEND
   closesocket(Socket)
 END SUB