diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 04:28:21 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 04:28:21 +0000 |
commit | 0a4c1534f39511894728da193ab8225ad6022de9 (patch) | |
tree | 683b711accf64eca486b138cbc034c609f93a53a /doc/introduction.html | |
parent | 8e80e38f2c3121242b3b2f7a45a960c9af4d1a68 (diff) | |
download | luasocket-0a4c1534f39511894728da193ab8225ad6022de9.tar.gz luasocket-0a4c1534f39511894728da193ab8225ad6022de9.tar.bz2 luasocket-0a4c1534f39511894728da193ab8225ad6022de9.zip |
Still work to do in the manual...
Diffstat (limited to 'doc/introduction.html')
-rw-r--r-- | doc/introduction.html | 77 |
1 files changed, 45 insertions, 32 deletions
diff --git a/doc/introduction.html b/doc/introduction.html index 86c552b..59d1956 100644 --- a/doc/introduction.html +++ b/doc/introduction.html | |||
@@ -36,13 +36,21 @@ | |||
36 | <h2>Introduction</h2> | 36 | <h2>Introduction</h2> |
37 | 37 | ||
38 | <p> | 38 | <p> |
39 | LuaSocket is a <a href="http://www.lua.org">Lua</a> extension library | ||
40 | that is composed by two parts: a C core that provides support for the TCP | ||
41 | and UDP transport layers, and a set of Lua modules that add support for | ||
42 | the SMTP (sending e-mails), HTTP (WWW access) and FTP (uploading and | ||
43 | downloading files) protocols and other functionality commonly needed by | ||
44 | applications that deal with the Internet. This introduction is about the C | ||
45 | core. | ||
46 | </p> | ||
47 | |||
48 | <p> | ||
39 | Communication in LuaSocket is performed via I/O objects. These can | 49 | Communication in LuaSocket is performed via I/O objects. These can |
40 | represent different network domains. Currently, support is provided for TCP | 50 | represent different network domains. Currently, support is provided for TCP |
41 | and UDP, but nothing prevents other developers from implementing SSL, Local | 51 | and UDP, but nothing prevents other developers from implementing SSL, Local |
42 | Domain, Pipes, File Descriptors etc. I/O objects provide a standard | 52 | Domain, Pipes, File Descriptors etc. I/O objects provide a standard |
43 | interface to I/O across different domains and operating systems. | 53 | interface to I/O across different domains and operating systems. |
44 | LuaSocket 2.0 has been rewritten from scratch to simplify the future | ||
45 | addition of new domains. | ||
46 | </p> | 54 | </p> |
47 | 55 | ||
48 | <p> | 56 | <p> |
@@ -52,8 +60,17 @@ Second, the simplicity and the feel of the Lua language should be | |||
52 | preserved. To achieve these goals, the LuaSocket API keeps the function names and semantics the C API whenever possible, but their usage in Lua has been greatly simplified. | 60 | preserved. To achieve these goals, the LuaSocket API keeps the function names and semantics the C API whenever possible, but their usage in Lua has been greatly simplified. |
53 | </p> | 61 | </p> |
54 | 62 | ||
63 | |||
64 | <p> | ||
65 | One of the simplifications is the receive pattern capability. | ||
66 | Applications can read data from stream domains (such as TCP) | ||
67 | line by line, block by block, or until the connection is closed. | ||
68 | All I/O reads are buffered and the performance differences between | ||
69 | different receive patterns are negligible. | ||
70 | </p> | ||
71 | |||
55 | <p> | 72 | <p> |
56 | One of the simplifications is the timeout control | 73 | Another advantage is the flexible timeout control |
57 | mechanism. As in C, all I/O operations are blocking by default. For | 74 | mechanism. As in C, all I/O operations are blocking by default. For |
58 | example, the <a href=tcp.html#send><tt>send</tt></a>, | 75 | example, the <a href=tcp.html#send><tt>send</tt></a>, |
59 | <a href=tcp.html#receive><tt>receive</tt></a> and | 76 | <a href=tcp.html#receive><tt>receive</tt></a> and |
@@ -70,14 +87,6 @@ call might perform several OS calls, so that the two timeout values are | |||
70 | </p> | 87 | </p> |
71 | 88 | ||
72 | <p> | 89 | <p> |
73 | Another important difference is the receive pattern capability. | ||
74 | Applications can read data from stream domains (such as TCP) | ||
75 | line by line, block by block, or until the connection is closed. | ||
76 | All I/O reads are buffered and the performance differences between | ||
77 | different receive patterns are negligible. | ||
78 | </p> | ||
79 | |||
80 | <p> | ||
81 | Finally, the host name resolution is transparent, meaning that most | 90 | Finally, the host name resolution is transparent, meaning that most |
82 | functions and methods accept both IP addresses and host names. In case a | 91 | functions and methods accept both IP addresses and host names. In case a |
83 | host name is given, the library queries the system's resolver and | 92 | host name is given, the library queries the system's resolver and |
@@ -89,13 +98,8 @@ functions from the DNS module are provided to convert between host names and IP | |||
89 | </p> | 98 | </p> |
90 | 99 | ||
91 | <p> | 100 | <p> |
92 | Previous versions of LuaSocket provided global functions for operating on | 101 | Together, these changes make network programming in LuaSocket much simpler |
93 | I/O objects. To give the library a Lua 5.0 feel, these have been eliminated | 102 | than it is in C, as the following sections will show. |
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 | ||
96 | different operations. The TCP and UDP objects are | ||
97 | introduced in the following sections, following a few words about | ||
98 | initialization. | ||
99 | </p> | 103 | </p> |
100 | 104 | ||
101 | <!-- initializing +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 105 | <!-- initializing +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
@@ -103,13 +107,14 @@ initialization. | |||
103 | <h3>Initializing the library</h3> | 107 | <h3>Initializing the library</h3> |
104 | 108 | ||
105 | <p> | 109 | <p> |
106 | The core LuaSocket functionality is implemented in C, and usually available as | 110 | The core LuaSocket is almost entirely implemented in C. It is |
107 | a dynamic library which the interpreter can load when required. | 111 | usually available as a dynamic library which the interpreter can load |
112 | with the help of a loader module written in Lua. | ||
108 | Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket | 113 | Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket |
109 | functionality is defined inside a tables (or rather a namespaces). No global | 114 | functionality is defined inside tables (or rather a namespaces). No global |
110 | variables are ever created. | 115 | variables are ever created. |
111 | Namespaces are obtained with the <tt>require</tt> Lua function, which loads | 116 | Namespaces are obtained with the <tt>require</tt> Lua function, which loads |
112 | and initializes any required libraries and return the namespace. | 117 | and initializes any required library and returns the namespace. |
113 | For example, the core functionality or LuaSocket is usually available | 118 | For example, the core functionality or LuaSocket is usually available |
114 | from the "<tt>socket</tt>" namespace. | 119 | from the "<tt>socket</tt>" namespace. |
115 | </p> | 120 | </p> |
@@ -130,13 +135,14 @@ words, applications communicating through TCP can send and receive data as | |||
130 | an error free stream of bytes. Data is split in one end and | 135 | an error free stream of bytes. Data is split in one end and |
131 | reassembled transparently on the other end. There are no boundaries in | 136 | reassembled transparently on the other end. There are no boundaries in |
132 | the data transfers. The library allows users to read data from the | 137 | the data transfers. The library allows users to read data from the |
133 | sockets in several different granularity: patterns are available for | 138 | sockets in several different granularities: patterns are available for |
134 | lines, arbitrary sized blocks or "read up to connection closed", all with | 139 | lines, arbitrary sized blocks or "read up to connection closed", all with |
135 | good performance. | 140 | good performance. |
136 | </p> | 141 | </p> |
137 | 142 | ||
138 | <p> | 143 | <p> |
139 | The library distinguishes three types of TCP sockets: master, client and server sockets. | 144 | The library distinguishes three types of TCP sockets: <em>master</em>, |
145 | <em>client</em> and <em>server</em> sockets. | ||
140 | </p> | 146 | </p> |
141 | 147 | ||
142 | <p> | 148 | <p> |
@@ -144,10 +150,11 @@ Master sockets are newly created TCP sockets returned by the function | |||
144 | <a href=tcp.html#tcp><tt>socket.tcp</tt></a>. A master socket is | 150 | <a href=tcp.html#tcp><tt>socket.tcp</tt></a>. A master socket is |
145 | transformed into a server socket | 151 | transformed into a server socket |
146 | after it is associated with a <em>local</em> address by a call to the | 152 | after it is associated with a <em>local</em> address by a call to the |
147 | <a href=tcp.html#bind><tt>bind</tt></a> method. Conversely, it | 153 | <a href=tcp.html#bind><tt>bind</tt></a> method followed by a call to the |
154 | <a href=tcp.html#listen><tt>listen</tt></a>. Conversely, a master socket | ||
148 | can be changed into a client socket with the method | 155 | can be changed into a client socket with the method |
149 | <a href=tcp.html#connect><tt>connect</tt></a>, | 156 | <a href=tcp.html#connect><tt>connect</tt></a>, |
150 | that associates it with a <em>remote</em> address. | 157 | which associates it with a <em>remote</em> address. |
151 | </p> | 158 | </p> |
152 | 159 | ||
153 | <p> | 160 | <p> |
@@ -158,7 +165,7 @@ client socket object is returned representing this connection. The | |||
158 | other methods available for server socket objects are | 165 | other methods available for server socket objects are |
159 | <a href=tcp.html#getsockname><tt>getsockname</tt></a>, | 166 | <a href=tcp.html#getsockname><tt>getsockname</tt></a>, |
160 | <a href=tcp.html#setoption><tt>setoption</tt></a>, | 167 | <a href=tcp.html#setoption><tt>setoption</tt></a>, |
161 | <a href=tcp.html#settimeout><tt>settimeout</tt></a> and | 168 | <a href=tcp.html#settimeout><tt>settimeout</tt></a>, and |
162 | <a href=tcp.html#close><tt>close</tt></a>. | 169 | <a href=tcp.html#close><tt>close</tt></a>. |
163 | </p> | 170 | </p> |
164 | 171 | ||
@@ -172,7 +179,8 @@ available for client socket objects are | |||
172 | <a href=tcp.html#getsockname><tt>getsockname</tt></a>, | 179 | <a href=tcp.html#getsockname><tt>getsockname</tt></a>, |
173 | <a href=tcp.html#getpeername><tt>getpeername</tt></a>, | 180 | <a href=tcp.html#getpeername><tt>getpeername</tt></a>, |
174 | <a href=tcp.html#setoption><tt>setoption</tt></a>, | 181 | <a href=tcp.html#setoption><tt>setoption</tt></a>, |
175 | <a href=tcp.html#settimeout><tt>settimeout</tt></a> and | 182 | <a href=tcp.html#settimeout><tt>settimeout</tt></a>, |
183 | <a href=tcp.html#shutdown><tt>shutdown</tt></a>, and | ||
176 | <a href=tcp.html#close><tt>close</tt></a>. | 184 | <a href=tcp.html#close><tt>close</tt></a>. |
177 | </p> | 185 | </p> |
178 | 186 | ||
@@ -185,7 +193,7 @@ A simple echo server, using LuaSocket. The program binds to an ephemeral | |||
185 | port (one that is chosen by the operating system) on the local host and | 193 | port (one that is chosen by the operating system) on the local host and |
186 | awaits client connections on that port. When a connection is established, | 194 | awaits client connections on that port. When a connection is established, |
187 | the program reads a line from the remote end and sends it back, closing | 195 | the program reads a line from the remote end and sends it back, closing |
188 | the connection immediately after. You can test it using the telnet | 196 | the connection immediately. You can test it using the telnet |
189 | program. | 197 | program. |
190 | </p> | 198 | </p> |
191 | 199 | ||
@@ -232,6 +240,12 @@ error correction). | |||
232 | </p> | 240 | </p> |
233 | 241 | ||
234 | <p> | 242 | <p> |
243 | Note that although no guarantees are made, these days | ||
244 | networks are so good that, under normal circumstances, few errors | ||
245 | happen in practice. | ||
246 | </p> | ||
247 | |||
248 | <p> | ||
235 | An UDP socket object is created by the | 249 | An UDP socket object is created by the |
236 | <a href=udp.html#udp><tt>socket.udp</tt></a> function. UDP | 250 | <a href=udp.html#udp><tt>socket.udp</tt></a> function. UDP |
237 | sockets do not need to be connected before use. The method | 251 | sockets do not need to be connected before use. The method |
@@ -288,14 +302,13 @@ error message. | |||
288 | 302 | ||
289 | <pre class=example> | 303 | <pre class=example> |
290 | -- change here to the host an port you want to contact | 304 | -- change here to the host an port you want to contact |
291 | host = "localhost" | 305 | local host, port = "localhost", 13 |
292 | port = 13 | ||
293 | -- load namespace | 306 | -- load namespace |
294 | local socket = require("socket") | 307 | local socket = require("socket") |
295 | -- convert host name to ip address | 308 | -- convert host name to ip address |
296 | local ip = socket.try(socket.dns.toip(host)) | 309 | local ip = socket.try(socket.dns.toip(host)) |
297 | -- create a new UDP object | 310 | -- create a new UDP object |
298 | local udp = socket.udp() | 311 | local udp = socket.try(socket.udp()) |
299 | -- contact daytime host | 312 | -- contact daytime host |
300 | socket.try(udp:sendto("anything", ip, port)) | 313 | socket.try(udp:sendto("anything", ip, port)) |
301 | -- retrieve the answer and print results | 314 | -- retrieve the answer and print results |