diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-15 23:00:56 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-15 23:00:56 +0000 |
commit | 843a431ef98fd541d98fd3898463985d9bfcde28 (patch) | |
tree | 2de4cd78a58ee17aa029528ede8ff5989b819019 /doc/introduction.html | |
parent | cb03a0e95429cc0d498e77fd50241c87fd2bf3ea (diff) | |
download | luasocket-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.html | 75 |
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> |
39 | Communication in LuaSocket is performed via I/O objects. These can | 39 | Communication in LuaSocket is performed via I/O objects. These can |
40 | represent different network domains. Currently, support is | 40 | represent different network domains. Currently, support is provided for TCP |
41 | provided for TCP and UDP, but there is work in progress to implement SSL, | 41 | and UDP, but nothing prevents other developers from implementing SSL, Local |
42 | Local Domain, Pipes, File Descriptors etc. I/O objects provide a standard | 42 | Domain, Pipes, File Descriptors etc. I/O objects provide a standard |
43 | interface to I/O across different domains and operating systems. | 43 | interface to I/O across different domains and operating systems. |
44 | LuaSocket 2.0 has been rewritten from scratch to simplify the future | 44 | LuaSocket 2.0 has been rewritten from scratch to simplify the future |
45 | addition of new domains. | 45 | addition 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 | |||
93 | I/O objects. To give the library a Lua 5.0 feel, these have been eliminated | 93 | I/O objects. To give the library a Lua 5.0 feel, these have been eliminated |
94 | from LuaSocket 2.0. I/O operations are only available as methods of the | 94 | from LuaSocket 2.0. I/O operations are only available as methods of the |
95 | corresponding I/O objects. Naturally, different I/O objects accept | 95 | corresponding I/O objects. Naturally, different I/O objects accept |
96 | different operations. The core functionality for TCP and UDP objects is | 96 | different operations. The TCP and UDP objects are |
97 | introduced in the following sections, following a few words about | 97 | introduced in the following sections, following a few words about |
98 | initialization. | 98 | initialization. |
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> |
106 | The core LuaSocket functionality is implemented in C, and usually available as | ||
107 | a dynamic library which the interpreter can load when required. | ||
106 | Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket | 108 | Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket |
107 | functionality is defined inside a table (or rather a namespace) stored with | 109 | functionality is defined inside a tables (or rather a namespaces). No global |
108 | the global name <tt>socket</tt>. To have this table created and its | 110 | variables are ever created. |
109 | contents made available to a Lua script, the interpreter running the script | 111 | Namespaces are obtained with the <tt>require</tt> Lua function, which loads |
110 | must be linked to the LuaSocket library, and to whatever libraries the | 112 | and initializes any required libraries and return the namespace. |
111 | host OS requires for network access (Windows requires ws2_32.lib, for | 113 | For example, the core functionality or LuaSocket is usually available |
112 | instance). LuaSocket is initialized in the | 114 | from the "<tt>socket</tt>" namespace. |
113 | Lua state given as the argument to the function | ||
114 | <tt>luaopen_socket</tt>, the only C function exported by the library. | ||
115 | After initialization, scripts are free to use all of the LuaSocket API. | ||
116 | </p> | 115 | </p> |
117 | 116 | ||
117 | <pre class="example"> | ||
118 | socket = require("socket") | ||
119 | print(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 |
189 | server, err = socket.tcp() | 194 | local socket = require("socket") |
190 | assert(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 | 196 | local server = socket.try(socket.bind("*", 0)) |
192 | ret, err = server:bind("*", 0) | ||
193 | assert(ret, err) | ||
194 | -- find out which port the OS chose for us | 197 | -- find out which port the OS chose for us |
195 | ip, port = server:getsockname() | 198 | local ip, port = server:getsockname() |
196 | -- print a message informing what's up | 199 | -- print a message informing what's up |
197 | print("Please telnet to localhost on port " .. port) | 200 | print("Please telnet to localhost on port " .. port) |
198 | print("After connecting, you have 10s to enter a line to be echoed") | 201 | print("After connecting, you have 10s to enter a line to be echoed") |
199 | -- loop forever waiting for clients | 202 | -- loop forever waiting for clients |
200 | while 1 do | 203 | while 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() |
213 | end | 214 | end |
@@ -286,21 +287,19 @@ error message. | |||
286 | </p> | 287 | </p> |
287 | 288 | ||
288 | <pre class=example> | 289 | <pre class=example> |
289 | host = "localhost" -- change here to the host you want to contact | 290 | -- change here to the host an port you want to contact |
290 | port = port or 13 | 291 | host = "localhost" |
292 | port = 13 | ||
293 | -- load namespace | ||
294 | local socket = require("socket") | ||
291 | -- convert host name to ip address | 295 | -- convert host name to ip address |
292 | ip, err = socket.dns.toip(host) | 296 | local ip = socket.try(socket.dns.toip(host)) |
293 | assert(ip, err) | ||
294 | -- create a new UDP object | 297 | -- create a new UDP object |
295 | udp = socket.udp() | 298 | local udp = socket.udp() |
296 | -- contact daytime host | 299 | -- contact daytime host |
297 | nsent, err = udp:sendto("anything", ip, port) | 300 | socket.try(udp:sendto("anything", ip, port)) |
298 | assert(not err, err) | 301 | -- retrieve the answer and print results |
299 | -- retrieve the answer | 302 | io.write(socket.try((udp:receive()))) |
300 | dgram, err = udp:receive() | ||
301 | assert(dgram, err) | ||
302 | -- display to user | ||
303 | print(dgram) | ||
304 | </pre> | 303 | </pre> |
305 | </blockquote> | 304 | </blockquote> |
306 | 305 | ||