nettobac  0.0.0
Network features for FreeBASIC code
example_client.bas
Go to the documentation of this file.
1 /'* \file example_client.bas
2 \brief Example code to test \Proj package in a server scenario
3 
4 Copyright (C) GPLv3, see ReadMe.md for details.
5 
6 \since 0.0.0
7 '/
8 
9 #INCLUDE ONCE "nettobac.bas"
10 #INCLUDE ONCE "nettobac_http.bas"
11 
12 '* \brief The folder to store the data
13 #DEFINE FOLD "data"
14 '* \brief The macro to report an error
15 #DEFINE ERR_MSG !"\n\n" _
16  & LEN(res) & " bytes received --> error " & *msg _
17  & !"\n" & page & !" returns:" _
18  & !"\n\n" & res
19 
20 
21 /'* \brief Store data in a file
22 \param Dat the data to store
23 \param Nam the path/name of the file to create (or override)
24 
25 This function opens the file named `Nam` in the current folder for
26 output (overriding an existend file, if any) and writes the STRING
27 `Dat` in to it. The SUB gets called in order to store the downloaded
28 data on disk.
29 
30 \since 0.0.0
31 '/
32 SUB saveData(BYREF Dat AS STRING, BYREF Nam AS STRING)
33  VAR fnr = FREEFILE
34  IF OPEN(Nam FOR OUTPUT AS fnr) THEN
35  ? "open failed: " & Nam
36  ELSE
37  PRINT #fnr, Dat;
38  CLOSE #fnr
39  ? "saved: " & Nam
40  END IF
41 END SUB
42 
43 
44 /'* \brief Operate as a client, download files
45 \returns the value to `END` the program
46 
47 FIXME
48 
49 \since 0.0.0
50 '/
51 FUNCTION doClientActions() AS INTEGER
52  SCOPE
53  VAR res = "" _
54  , page = "users.freebasic-portal.de/tjf/Projekte/libpruio/doc/html/index.html" _
55  , msg = httpLoad(res, page)
56  IF msg THEN ?ERR_MSG : RETURN 1 _
57  ELSE saveData(res, "index.htm")
58  END SCOPE
59 
60  SCOPE
61  VAR res = "" _
62  , page = "freebasic.net/sites/default/files/horse_original_r_0.gif" _
63  , msg = httpLoad(res, page, MIME_GIF)
64  IF msg THEN ?ERR_MSG : RETURN 1 _
65  ELSE saveData(res, "fb_logo.gif")
66  END SCOPE
67 
68  SCOPE
69  VAR res = "" _
70  , page = "staticmap.openstreetmap.de/staticmap.php" _
71  & "?center=40.714728,-73.998672" _
72  & "&zoom=12" _
73  & "&size=320x248" _
74  & "&maptype=osmarenderer" _
75  , msg = httpLoad(res, page, MIME_PNG, , &b1) ' note: raw data incl. header
76  IF msg THEN
77  ?ERR_MSG : RETURN 1
78  ELSE
79  VAR p = INSTR(res, CHR(137, 80, 78, 71)) ' get start of PNG data
80  IF p THEN saveData(MID(Res, p), "osm.png") _
81  ELSE ?"PNG data not found"
82  END IF
83  END SCOPE
84  RETURN 0
85 END FUNCTION
86 
87 '& int main(){
88 
89 ?MSG_ALL
90 
91 CHDIR(EXEPATH())
92 IF CHDIR(FOLD) THEN MKDIR(FOLD) : IF CHDIR(FOLD) THEN ?"no write permission (press any key)" : SLEEP : END
93 
94 END doClientActions()
95 
96 '& doClientActions();};
97 
98