aboutsummaryrefslogtreecommitdiff
path: root/doc/introduction.html
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-16 04:28:21 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-16 04:28:21 +0000
commit0a4c1534f39511894728da193ab8225ad6022de9 (patch)
tree683b711accf64eca486b138cbc034c609f93a53a /doc/introduction.html
parent8e80e38f2c3121242b3b2f7a45a960c9af4d1a68 (diff)
downloadluasocket-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.html77
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>
39LuaSocket is a <a href="http://www.lua.org">Lua</a> extension library
40that is composed by two parts: a C core that provides support for the TCP
41and UDP transport layers, and a set of Lua modules that add support for
42the SMTP (sending e-mails), HTTP (WWW access) and FTP (uploading and
43downloading files) protocols and other functionality commonly needed by
44applications that deal with the Internet. This introduction is about the C
45core.
46</p>
47
48<p>
39Communication in LuaSocket is performed via I/O objects. These can 49Communication in LuaSocket is performed via I/O objects. These can
40represent different network domains. Currently, support is provided for TCP 50represent different network domains. Currently, support is provided for TCP
41and UDP, but nothing prevents other developers from implementing SSL, Local 51and UDP, but nothing prevents other developers from implementing SSL, Local
42Domain, Pipes, File Descriptors etc. I/O objects provide a standard 52Domain, Pipes, File Descriptors etc. I/O objects provide a standard
43interface to I/O across different domains and operating systems. 53interface to I/O across different domains and operating systems.
44LuaSocket&nbsp;2.0 has been rewritten from scratch to simplify the future
45addition 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
52preserved. 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. 60preserved. 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>
65One of the simplifications is the receive pattern capability.
66Applications can read data from stream domains (such as TCP)
67line by line, block by block, or until the connection is closed.
68All I/O reads are buffered and the performance differences between
69different receive patterns are negligible.
70</p>
71
55<p> 72<p>
56One of the simplifications is the timeout control 73Another advantage is the flexible timeout control
57mechanism. As in C, all I/O operations are blocking by default. For 74mechanism. As in C, all I/O operations are blocking by default. For
58example, the <a href=tcp.html#send><tt>send</tt></a>, 75example, 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>
73Another important difference is the receive pattern capability.
74Applications can read data from stream domains (such as TCP)
75line by line, block by block, or until the connection is closed.
76All I/O reads are buffered and the performance differences between
77different receive patterns are negligible.
78</p>
79
80<p>
81Finally, the host name resolution is transparent, meaning that most 90Finally, the host name resolution is transparent, meaning that most
82functions and methods accept both IP addresses and host names. In case a 91functions and methods accept both IP addresses and host names. In case a
83host name is given, the library queries the system's resolver and 92host 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>
92Previous versions of LuaSocket provided global functions for operating on 101Together, these changes make network programming in LuaSocket much simpler
93I/O objects. To give the library a Lua 5.0 feel, these have been eliminated 102than it is in C, as the following sections will show.
94from LuaSocket 2.0. I/O operations are only available as methods of the
95corresponding I/O objects. Naturally, different I/O objects accept
96different operations. The TCP and UDP objects are
97introduced in the following sections, following a few words about
98initialization.
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>
106The core LuaSocket functionality is implemented in C, and usually available as 110The core LuaSocket is almost entirely implemented in C. It is
107a dynamic library which the interpreter can load when required. 111usually available as a dynamic library which the interpreter can load
112with the help of a loader module written in Lua.
108Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket 113Beginning with version 2.0 and following the Lua 5.0 trend, all LuaSocket
109functionality is defined inside a tables (or rather a namespaces). No global 114functionality is defined inside tables (or rather a namespaces). No global
110variables are ever created. 115variables are ever created.
111Namespaces are obtained with the <tt>require</tt> Lua function, which loads 116Namespaces are obtained with the <tt>require</tt> Lua function, which loads
112and initializes any required libraries and return the namespace. 117and initializes any required library and returns the namespace.
113For example, the core functionality or LuaSocket is usually available 118For example, the core functionality or LuaSocket is usually available
114from the "<tt>socket</tt>" namespace. 119from the "<tt>socket</tt>" namespace.
115</p> 120</p>
@@ -130,13 +135,14 @@ words, applications communicating through TCP can send and receive data as
130an error free stream of bytes. Data is split in one end and 135an error free stream of bytes. Data is split in one end and
131reassembled transparently on the other end. There are no boundaries in 136reassembled transparently on the other end. There are no boundaries in
132the data transfers. The library allows users to read data from the 137the data transfers. The library allows users to read data from the
133sockets in several different granularity: patterns are available for 138sockets in several different granularities: patterns are available for
134lines, arbitrary sized blocks or "read up to connection closed", all with 139lines, arbitrary sized blocks or "read up to connection closed", all with
135good performance. 140good performance.
136</p> 141</p>
137 142
138<p> 143<p>
139The library distinguishes three types of TCP sockets: master, client and server sockets. 144The 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
145transformed into a server socket 151transformed into a server socket
146after it is associated with a <em>local</em> address by a call to the 152after 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
148can be changed into a client socket with the method 155can 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>,
150that associates it with a <em>remote</em> address. 157which 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
158other methods available for server socket objects are 165other 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
185port (one that is chosen by the operating system) on the local host and 193port (one that is chosen by the operating system) on the local host and
186awaits client connections on that port. When a connection is established, 194awaits client connections on that port. When a connection is established,
187the program reads a line from the remote end and sends it back, closing 195the program reads a line from the remote end and sends it back, closing
188the connection immediately after. You can test it using the telnet 196the connection immediately. You can test it using the telnet
189program. 197program.
190</p> 198</p>
191 199
@@ -232,6 +240,12 @@ error correction).
232</p> 240</p>
233 241
234<p> 242<p>
243Note that although no guarantees are made, these days
244networks are so good that, under normal circumstances, few errors
245happen in practice.
246</p>
247
248<p>
235An UDP socket object is created by the 249An 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
237sockets do not need to be connected before use. The method 251sockets 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
291host = "localhost" 305local host, port = "localhost", 13
292port = 13
293-- load namespace 306-- load namespace
294local socket = require("socket") 307local socket = require("socket")
295-- convert host name to ip address 308-- convert host name to ip address
296local ip = socket.try(socket.dns.toip(host)) 309local ip = socket.try(socket.dns.toip(host))
297-- create a new UDP object 310-- create a new UDP object
298local udp = socket.udp() 311local udp = socket.try(socket.udp())
299-- contact daytime host 312-- contact daytime host
300socket.try(udp:sendto("anything", ip, port)) 313socket.try(udp:sendto("anything", ip, port))
301-- retrieve the answer and print results 314-- retrieve the answer and print results