aboutsummaryrefslogtreecommitdiff
path: root/doc/introduction.html
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 23:00:56 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 23:00:56 +0000
commit843a431ef98fd541d98fd3898463985d9bfcde28 (patch)
tree2de4cd78a58ee17aa029528ede8ff5989b819019 /doc/introduction.html
parentcb03a0e95429cc0d498e77fd50241c87fd2bf3ea (diff)
downloadluasocket-843a431ef98fd541d98fd3898463985d9bfcde28.tar.gz
luasocket-843a431ef98fd541d98fd3898463985d9bfcde28.tar.bz2
luasocket-843a431ef98fd541d98fd3898463985d9bfcde28.zip
Almost done with manual...
Diffstat (limited to 'doc/introduction.html')
-rw-r--r--doc/introduction.html75
1 files changed, 37 insertions, 38 deletions
diff --git a/doc/introduction.html b/doc/introduction.html
index e6f0ea2..86c552b 100644
--- a/doc/introduction.html
+++ b/doc/introduction.html
@@ -37,12 +37,12 @@
37 37
38<p> 38<p>
39Communication in LuaSocket is performed via I/O objects. These can 39Communication in LuaSocket is performed via I/O objects. These can
40represent different network domains. Currently, support is 40represent different network domains. Currently, support is provided for TCP
41provided for TCP and UDP, but there is work in progress to implement SSL, 41and UDP, but nothing prevents other developers from implementing SSL, Local
42Local Domain, Pipes, File Descriptors etc. I/O objects provide a standard 42Domain, Pipes, File Descriptors etc. I/O objects provide a standard
43interface to I/O across different domains and operating systems. 43interface to I/O across different domains and operating systems.
44LuaSocket&nbsp;2.0 has been rewritten from scratch to simplify the future 44LuaSocket&nbsp;2.0 has been rewritten from scratch to simplify the future
45addition of new domains. 45addition of new domains.
46</p> 46</p>
47 47
48<p> 48<p>
@@ -93,7 +93,7 @@ Previous versions of LuaSocket provided global functions for operating on
93I/O objects. To give the library a Lua 5.0 feel, these have been eliminated 93I/O objects. To give the library a Lua 5.0 feel, these have been eliminated
94from LuaSocket 2.0. I/O operations are only available as methods of the 94from LuaSocket 2.0. I/O operations are only available as methods of the
95corresponding I/O objects. Naturally, different I/O objects accept 95corresponding I/O objects. Naturally, different I/O objects accept
96different operations. The core functionality for TCP and UDP objects is 96different operations. The TCP and UDP objects are
97introduced in the following sections, following a few words about 97introduced in the following sections, following a few words about
98initialization. 98initialization.
99</p> 99</p>
@@ -103,18 +103,23 @@ initialization.
103<h3>Initializing the library</h3> 103<h3>Initializing the library</h3>
104 104
105<p> 105<p>
106The core LuaSocket functionality is implemented in C, and usually available as
107a dynamic library which the interpreter can load when required.
106Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket 108Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket
107functionality is defined inside a table (or rather a namespace) stored with 109functionality is defined inside a tables (or rather a namespaces). No global
108the global name <tt>socket</tt>. To have this table created and its 110variables are ever created.
109contents made available to a Lua script, the interpreter running the script 111Namespaces are obtained with the <tt>require</tt> Lua function, which loads
110must be linked to the LuaSocket library, and to whatever libraries the 112and initializes any required libraries and return the namespace.
111host OS requires for network access (Windows requires ws2_32.lib, for 113For example, the core functionality or LuaSocket is usually available
112instance). LuaSocket is initialized in the 114from the "<tt>socket</tt>" namespace.
113Lua state given as the argument to the function
114<tt>luaopen_socket</tt>, the only C function exported by the library.
115After initialization, scripts are free to use all of the LuaSocket API.
116</p> 115</p>
117 116
117<pre class="example">
118socket = require("socket")
119print(socket.VERSION)
120-- LuaSocket 2.0
121</pre>
122
118<!-- tcp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 123<!-- tcp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
119 124
120<h3 id=tcp>TCP</h3> 125<h3 id=tcp>TCP</h3>
@@ -185,29 +190,25 @@ program.
185</p> 190</p>
186 191
187<pre class=example> 192<pre class=example>
188-- create a new TCP object 193-- load namespace
189server, err = socket.tcp() 194local socket = require("socket")
190assert(server, err) 195-- create a TCP socket and bind it to the local host, at any port
191-- bind it to the local host, at any port 196local server = socket.try(socket.bind("*", 0))
192ret, err = server:bind("*", 0)
193assert(ret, err)
194-- find out which port the OS chose for us 197-- find out which port the OS chose for us
195ip, port = server:getsockname() 198local ip, port = server:getsockname()
196-- print a message informing what's up 199-- print a message informing what's up
197print("Please telnet to localhost on port " .. port) 200print("Please telnet to localhost on port " .. port)
198print("After connecting, you have 10s to enter a line to be echoed") 201print("After connecting, you have 10s to enter a line to be echoed")
199-- loop forever waiting for clients 202-- loop forever waiting for clients
200while 1 do 203while 1 do
201 -- wait for a conection from any client 204 -- wait for a conection from any client
202 client, err = server:accept() 205 local client = server:accept()
203 -- make sure we don't block waiting for this client's line 206 -- make sure we don't block waiting for this client's line
204 client:settimeout(10) 207 client:settimeout(10)
205 -- receive the line 208 -- receive the line
206 line, err = client:receive() 209 local line, err = client:receive()
207 -- if there was no error, send it back to the client 210 -- if there was no error, send it back to the client
208 if not err then 211 if not err then client:send(line .. "\n") end
209 client:send(line .. "\n")
210 end
211 -- done with client, close the object 212 -- done with client, close the object
212 client:close() 213 client:close()
213end 214end
@@ -286,21 +287,19 @@ error message.
286</p> 287</p>
287 288
288<pre class=example> 289<pre class=example>
289host = "localhost" -- change here to the host you want to contact 290-- change here to the host an port you want to contact
290port = port or 13 291host = "localhost"
292port = 13
293-- load namespace
294local socket = require("socket")
291-- convert host name to ip address 295-- convert host name to ip address
292ip, err = socket.dns.toip(host) 296local ip = socket.try(socket.dns.toip(host))
293assert(ip, err)
294-- create a new UDP object 297-- create a new UDP object
295udp = socket.udp() 298local udp = socket.udp()
296-- contact daytime host 299-- contact daytime host
297nsent, err = udp:sendto("anything", ip, port) 300socket.try(udp:sendto("anything", ip, port))
298assert(not err, err) 301-- retrieve the answer and print results
299-- retrieve the answer 302io.write(socket.try((udp:receive())))
300dgram, err = udp:receive()
301assert(dgram, err)
302-- display to user
303print(dgram)
304</pre> 303</pre>
305</blockquote> 304</blockquote>
306 305