-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnetwork.html
More file actions
187 lines (187 loc) · 19.1 KB
/
Copy pathnetwork.html
File metadata and controls
187 lines (187 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SmallBASIC | network</title>
<meta name="description" content="SmallBASIC | One more basic">
<link rel="canonical" href="/network.html">
<link rel="keywords" href="network">
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" type="image/png" href="/images/sb-desktop-32x32.png">
<script src="/clipboard.js"></script>
</head>
<body>
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⯅</button>
<script src="/backtotop.js"></script>
<div class="wrapAll clearfix">
<nav class="navigation">
<div class="logo">
<a href="/"><img src='/images/sb-logo.png?v=2' alt="logo"></a>
</div>
<div class="navlinks">
<a href="/pages/download.html">Download</a>
<a href="/pages/news.html">News</a>
<a href="/pages/community.html">Community</a>
<a class='active' href="/pages/articles.html">Resources</a>
<a href="/pages/reference.html">Language Reference</a>
<a href="/pages/guide.html">SmallBASIC Manual</a>
</div>
</nav>
<div class="mainsection">
<div class="tabs clearfix">
<div class="tabsRight">
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/blob/master/_build/pages/network.markdown">Edit</a>
<a target="_github" href="https://github.com/smallbasic/smallbasic.github.io/commits/master/_build/pages/network.markdown">History</a>
</div>
</div>
<div class="article">
<h1 id="network-programming">Network programming</h1>
<blockquote>
<p>Accessing the network in SmallBASIC</p>
</blockquote>
<h2 id="http">HTTP</h2>
<p>Use the prefix “HTTP:” with the OPEN command to open a network HTTP
connection. You can then use the file number with the TLOAD command to
read the data.</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> <span class="st">"DuckDuckGo Search"</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="kw">while </span><span class="dv">1</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="co">'<=== when cycle around need to isolate input prompt</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">input</span> <span class="st">"(Just enter quits) Term? "</span>, queryTerm</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">if </span><span class="fu">trim</span>(queryTerm)=<span class="st">""</span> <span class="kw">then</span> ? <span class="st">"Cheers!"</span>:<span class="kw">end </span> <span class="co">'<=== need a way out</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> url = <span class="st">"http://api.duckduckgo.com/?q="</span> + <span class="fu">trim</span>(queryTerm) + <span class="st">"&format=json"</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">open</span> url <span class="kw">as</span> #<span class="dv">1</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">if </span>(<span class="fu">eof</span>(<span class="dv">1</span>)) <span class="kw">then</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">throw</span> <span class="st">"Connection failed: "</span> + url</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">fi</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="dt">dim</span> results</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">tload</span> #<span class="dv">1</span>, results</span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> json = <span class="kw">array</span>(results)</span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> num_results = <span class="fu">len</span>(json.RelatedTopics)</span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a> <span class="kw">for </span>i = <span class="dv">0</span> <span class="kw">to</span> num_results - <span class="dv">1</span></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> <span class="kw">if </span>(<span class="fu">isarray</span>(json.RelatedTopics(i).topics)) <span class="kw">then</span></span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a> num_topics = <span class="fu">len</span>(json.RelatedTopics(i).Topics)</span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a> <span class="kw">for </span>t = <span class="dv">0</span> <span class="kw">to</span> num_topics - <span class="dv">1</span></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="fu">cat</span>(<span class="dv">1</span>); <span class="st">" "</span>; json.RelatedTopics(i).Topics(t).FirstURL; <span class="fu">cat</span>(<span class="dv">0</span>)</span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="st">" "</span>; json.RelatedTopics(i).Topics(t).text</span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a> <span class="kw">next </span>t</span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a> <span class="kw">else</span></span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="fu">cat</span>(<span class="dv">1</span>); <span class="st">" "</span>; json.RelatedTopics(i).FirstURL; <span class="fu">cat</span>(<span class="dv">0</span>)</span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> <span class="st">" "</span>; json.RelatedTopics(i).Text</span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a> <span class="kw">endif</span></span>
<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a> <span class="kw">next </span>i</span>
<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">Close</span> #<span class="dv">1</span> <span class="co">'<===== oh this helps!</span></span>
<span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a><span class="kw">wend</span></span></code></pre></div>
<h2 id="image">Image</h2>
<p>Use the prefix “HTTP:” with the OPEN command to open an image file
over the network. You can then pass the file number to the IMAGE
command. This returns an system object which can then be used to
manipulate images in the graphical version of SmallBASIC.</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">' open some random image I found on the net</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">open</span> <span class="st">"http://img2.wikia.nocookie.net/__cb20150113215904/farmville/images/9/92/Lumberjack_Gnome-icon.png"</span> <span class="kw">as</span> #<span class="dv">1</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co">' the IMAGE function creates a 'system' object, this is stored in variable "i"</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="co">' the completed 'i' variable will then have three commands - SHOW, HIDE and SAVE</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>i = <span class="fu">image</span>(#<span class="dv">1</span>)</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="co">' SHOW takes 0, 2, 3 or 4 arguments [x,y [,zindex [, opacity]]]</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>i.show(<span class="dv">100</span>,<span class="dv">50</span>)</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="co">' printing 'i' will reveal all of its properties, these can be modified directly</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="co">' before calling show</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> i</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="kw">pause</span> <span class="dv">2</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>i.show(<span class="dv">20</span>,<span class="dv">20</span>,<span class="dv">1</span>)</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> i</span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a><span class="kw">pause</span> <span class="dv">2</span></span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a>i.show(<span class="dv">201</span>,<span class="dv">20</span>,<span class="dv">1</span>, <span class="dv">56</span>)</span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> i</span>
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a><span class="kw">pause</span> <span class="dv">2</span></span>
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a>i.show(<span class="dv">120</span>,<span class="dv">20</span>,<span class="dv">1</span>, <span class="dv">56</span>)</span>
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> i</span>
<span id="cb2-26"><a href="#cb2-26" aria-hidden="true" tabindex="-1"></a><span class="kw">pause</span> <span class="dv">2</span></span>
<span id="cb2-27"><a href="#cb2-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-28"><a href="#cb2-28" aria-hidden="true" tabindex="-1"></a>i.show(<span class="dv">40</span>,<span class="dv">120</span>,<span class="dv">1</span>,<span class="dv">100</span>)</span>
<span id="cb2-29"><a href="#cb2-29" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> i</span>
<span id="cb2-30"><a href="#cb2-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-31"><a href="#cb2-31" aria-hidden="true" tabindex="-1"></a><span class="co">' you can create a second variable using the same image handle</span></span>
<span id="cb2-32"><a href="#cb2-32" aria-hidden="true" tabindex="-1"></a><span class="co">' this allows you to manipulate the same image with a different set of properties</span></span>
<span id="cb2-33"><a href="#cb2-33" aria-hidden="true" tabindex="-1"></a>j = <span class="fu">image</span>(#<span class="dv">1</span>)</span>
<span id="cb2-34"><a href="#cb2-34" aria-hidden="true" tabindex="-1"></a>j.show(<span class="dv">10</span>,<span class="dv">25</span>,<span class="dv">0</span>,<span class="dv">5</span>)</span>
<span id="cb2-35"><a href="#cb2-35" aria-hidden="true" tabindex="-1"></a><span class="kw">pause</span> <span class="dt">true</span></span></code></pre></div>
<h2 id="sockets">Sockets</h2>
<p>Use the prefix “SOCL:” with the OPEN command to open a network
socket. You can then use the file number with other input/output
commands to interact with the connection.</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">open</span> <span class="st">"SOCL:192.168.178.76:8080"</span> <span class="kw">as</span> #<span class="dv">1</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> #<span class="dv">1</span>, <span class="st">"time"</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="fu">lineinput</span> #<span class="dv">1</span>, s</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="kw">print</span> s </span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="fu">close</span> #<span class="dv">1</span></span></code></pre></div>
<p>If you omit the host name in the SOCL: string passed to the OPEN
command, SmallBASIC will listen for connections from another
host/process.</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">rem Print a date string like '29 SEP 2018 09:31:49 ACST'</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="kw">func </span>get_time</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">local</span> today = <span class="fu">julian</span>(date)</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">local</span> start = <span class="fu">timer</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">local</span> t_hour, t_min, t_sec, t_str</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">timehms</span> start, t_hour, t_min, t_sec</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="co">rem </span><span class="al">TODO</span><span class="co">, format should support multiple arguments</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> t_str = <span class="fu">format</span>(<span class="st">" ##:"</span>, t_hour) + <span class="fu">format</span>(<span class="st">"##:"</span>, t_min) + <span class="fu">format</span>(<span class="st">"##"</span>, t_sec)</span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a> <span class="kw">return</span> <span class="fu">datefmt</span>(<span class="st">"dd mmm yyyy"</span>, today) + t_str</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a><span class="kw">while </span><span class="dv">1</span></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">open</span> <span class="st">"SOCL:8080"</span> <span class="kw">as</span> #<span class="dv">1</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">while </span>(<span class="kw">not</span> <span class="fu">eof</span>(<span class="dv">1</span>))</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">lineinput</span> #<span class="dv">1</span>, s</span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a> <span class="kw">if </span>(s == <span class="st">"time"</span>)</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> #<span class="dv">1</span>, get_time</span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a> <span class="kw">else</span></span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> <span class="kw">print</span> #<span class="dv">1</span>, <span class="st">"unknown command"</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> <span class="kw">endif</span></span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> <span class="kw">wend</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">close</span> #<span class="dv">1</span></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a><span class="kw">wend</span></span></code></pre></div>
<h2 id="web-server">Web server</h2>
<p>In addition to the graphical and command line versions of SmallBASIC,
there is also a web server version based on <a
href="https://www.gnu.org/software/libmicrohttpd">libmicrohttpd</a>.</p>
<p>You launch the web SmallBASIC in a folder containing one or more
SmallBASIC programs, you then point your browser to a URL formulated
from the listening port number and the SmallBASIC program name. For
example:</p>
<div class="sourceCode" id="cb5"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>$ ls cats.bas # program cats.bas exists <span class="kw">in</span> the current folder</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>$ sbasicw</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>Starting SmallBASIC web server <span class="kw">on</span> port:<span class="dv">8080</span></span></code></pre></div>
<p>In your Web Browser:</p>
<div class="sourceCode" id="cb6"><pre
class="sourceCode smallbasic"><code class="sourceCode smallbasic"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>http://localhost:<span class="dv">8080</span>/cats.bas</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>Output from cats.bas displayed <span class="kw">in</span> the web browser.</span></code></pre></div>
<p>If the SmallBASIC program includes graphical output statements these
are converted to HTML5 canvas operations. Only a snapshot of the output
is displayed i.e., you would only see one frame of an animation in your
web browser.</p>
</div>
<div class="pagefooter">
This page was last edited on Sat, 9 Sep 2023 22:31:16 +0200
|
<a href="https://en.wikipedia.org/wiki/Markdown" target="_blank" rel="nofollow">Markdown</a>
processed with
<a href="https://pandoc.org/MANUAL.html#pandocs-markdown" target="_blank" rel="nofollow">pandoc 3.1.12.1</a>
</div>
</div>
</div>
</body>
</html>