diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-31 01:00:15 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-31 01:00:15 +0000 |
commit | 982781f1464c9b7a8133130433f83dbf1f59a2c0 (patch) | |
tree | 8f96f9e9fa1e6bef8b8356037986ddc18673cade /doc/introduction.html | |
parent | 6789b83ff5c15296267f880d3b98cf8a1800c30a (diff) | |
download | luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.tar.gz luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.tar.bz2 luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.zip |
LuaSocket 2.0 User's Manual.
Diffstat (limited to 'doc/introduction.html')
-rw-r--r-- | doc/introduction.html | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/doc/introduction.html b/doc/introduction.html new file mode 100644 index 0000000..7adf45b --- /dev/null +++ b/doc/introduction.html | |||
@@ -0,0 +1,328 @@ | |||
1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
2 | "http://www.w3.org/TR/html4/strict.dtd"> | ||
3 | <html> | ||
4 | |||
5 | <head> | ||
6 | <title>LuaSocket: Network support for the Lua language</title> | ||
7 | <link rel="stylesheet" href="reference.css" type="text/css"> | ||
8 | </head> | ||
9 | |||
10 | <body> | ||
11 | |||
12 | <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
13 | |||
14 | <div class=header> | ||
15 | <hr> | ||
16 | <center> | ||
17 | <table summary="LuaSocket logo"> | ||
18 | <tr><td align=center><a href="http://www.lua.org"> | ||
19 | <img border=0 alt="LuaSocket" src="luasocket.png"> | ||
20 | </a></td></tr> | ||
21 | <tr><td align=center valign=top>Network support for the Lua language | ||
22 | </td></tr> | ||
23 | </table> | ||
24 | <p class=bar> | ||
25 | <a href="home.html">home</a> · | ||
26 | <a href="home.html#download">download</a> · | ||
27 | <a href="introduction.html">introduction</a> · | ||
28 | <a href="reference.html">reference</a> | ||
29 | </p> | ||
30 | </center> | ||
31 | <hr> | ||
32 | </div> | ||
33 | |||
34 | <!-- introduction +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
35 | |||
36 | <h2>Introduction</h2> | ||
37 | |||
38 | <p> | ||
39 | Communication in LuaSocket is performed via I/O objects. These can | ||
40 | represent different network domains. Currently, support is | ||
41 | provided for TCP and UDP, but there is work in progress to implement SSL, | ||
42 | Local Domain, Pipes, File Descriptors etc. I/O objects provide a standard | ||
43 | 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 new domains. | ||
46 | </p> | ||
47 | |||
48 | <p> | ||
49 | The LuaSocket API was designed with two goals in mind. First, users | ||
50 | experienced with the C API to sockets should feel comfortable using LuaSocket. | ||
51 | 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. | ||
53 | </p> | ||
54 | |||
55 | <p> | ||
56 | One of the simplifications is the timeout control | ||
57 | 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>, | ||
59 | <a href=tcp.html#receive><tt>receive</tt></a> and | ||
60 | <a href=tcp.html#accept><tt>accept</tt></a> methods | ||
61 | of the TCP domain will block the caller application until | ||
62 | the operation is completed (if ever!). However, with a call to the | ||
63 | <a href=tcp.html#settimeout><tt>settimeout</tt></a> | ||
64 | method, an application can specify upper limits on | ||
65 | the time it can be blocked by LuaSocket (the "<tt>total</tt>" timeout), on | ||
66 | the time LuaSocket can internally be blocked by any OS call (the | ||
67 | "<tt>block</tt>" timeout) or a combination of the two. Each LuaSocket | ||
68 | call might perform several OS calls, so that the two timeout values are | ||
69 | <em>not</em> equivalent. | ||
70 | </p> | ||
71 | |||
72 | <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 | ||
82 | 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 | ||
84 | tries the main returned IP address. Note that direct use of IP addresses | ||
85 | is more efficient, of course. The | ||
86 | <a href=dns.html#toip><tt>toip</tt></a> | ||
87 | and <a href=dns.html#tohostname><tt>tohostname</tt></a> | ||
88 | functions from the DNS module are provided to convert between host names and IP addresses. | ||
89 | </p> | ||
90 | |||
91 | <p> | ||
92 | 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 | ||
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 core functionality for TCP and UDP objects is | ||
97 | introduced in the following sections, following a few words about | ||
98 | initialization. | ||
99 | </p> | ||
100 | |||
101 | <!-- initializing +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
102 | |||
103 | <h3>Initializing the library</h3> | ||
104 | |||
105 | <p> | ||
106 | 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 | ||
108 | the global name <tt>socket</tt>. To have this table created and its | ||
109 | contents made available to a Lua script, the interpreter running the script | ||
110 | must be linked to the LuaSocket library, and to whatever libraries the | ||
111 | host OS requires for network access (Windows requires ws2_32.lib, for | ||
112 | instance). LuaSocket is initialized in the | ||
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, the scripts are free to use all LuaSocket API. | ||
116 | </p> | ||
117 | |||
118 | <!-- tcp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
119 | |||
120 | <h3 id=tcp>TCP</h3> | ||
121 | |||
122 | <p> | ||
123 | TCP (Transfer Control Protocol) is reliable stream protocol. In other | ||
124 | words, applications communicating through TCP can send and receive data as | ||
125 | an error free stream of bytes. Data is split in one end and | ||
126 | reassembled transparently on the other end. There are no boundaries in | ||
127 | the data transfers. The library allows users to read data from the | ||
128 | sockets in several different granularity: patterns are available for | ||
129 | lines, arbitrary sized blocks or "read up to connection closed", all with | ||
130 | good performance. | ||
131 | </p> | ||
132 | |||
133 | <p> | ||
134 | The library distinguishes three types of TCP sockets: master, client and server sockets. | ||
135 | </p> | ||
136 | |||
137 | <p> | ||
138 | Master sockets are newly created TCP sockets returned by the function | ||
139 | <a href=tcp.html#tcp><tt>socket.tcp</tt></a>. A master socket is | ||
140 | transformed into a server socket | ||
141 | after it is associated with a <em>local</em> address by a call to the | ||
142 | <a href=tcp.html#bind><tt>bind</tt></a> method. Conversely, it | ||
143 | can be changed into a client socket with the method | ||
144 | <a href=tcp.html#connect><tt>connect</tt></a>, | ||
145 | that associates it with a <em>remote</em> address. | ||
146 | </p> | ||
147 | |||
148 | <p> | ||
149 | On server sockets, applications can use the | ||
150 | <a href=tcp.html#accept><tt>accept</tt></a> method | ||
151 | to wait for a client connection. Once a connection is established, a | ||
152 | client socket object is returned representing this connection. The | ||
153 | other methods available for server socket objects are | ||
154 | <a href=tcp.html#getsockname><tt>getsockname</tt></a>, | ||
155 | <a href=tcp.html#setoption><tt>setoption</tt></a>, | ||
156 | <a href=tcp.html#settimeout><tt>settimeout</tt></a> and | ||
157 | <a href=tcp.html#close><tt>close</tt></a>. | ||
158 | </p> | ||
159 | |||
160 | <p> | ||
161 | Client sockets are used to exchange data between two applications over | ||
162 | the Internet. Applications can call the methods | ||
163 | <a href=tcp.html#send><tt>send</tt></a> and | ||
164 | <a href=tcp.html#receive><tt>receive</tt></a> | ||
165 | to send and receive data. The other methods | ||
166 | available for client socket objects are | ||
167 | <a href=tcp.html#getsockname><tt>getsockname</tt></a>, | ||
168 | <a href=tcp.html#getpeername><tt>getpeername</tt></a>, | ||
169 | <a href=tcp.html#setoption><tt>setoption</tt></a>, | ||
170 | <a href=tcp.html#settimeout><tt>settimeout</tt></a> and | ||
171 | <a href=tcp.html#close><tt>close</tt></a>. | ||
172 | </p> | ||
173 | |||
174 | <p> | ||
175 | Example: | ||
176 | </p> | ||
177 | <blockquote> | ||
178 | <p> | ||
179 | A simple echo server, using LuaSocket. The program binds to an ephemeral | ||
180 | port (one that is chosen by the operating system) on the local host and | ||
181 | awaits client connections on that port. When a connection is established, | ||
182 | the program reads a line from the remote end and sends it back, closing | ||
183 | the connection immediately after. You can test it using the telnet | ||
184 | program. | ||
185 | </p> | ||
186 | |||
187 | <pre class=example> | ||
188 | -- create a new TCP object | ||
189 | server, err = socket.tcp() | ||
190 | assert(server, err) | ||
191 | -- bind it to the local host, at any port | ||
192 | ret, err = server:bind("*", 0) | ||
193 | assert(ret, err) | ||
194 | -- find out which port the OS chose for us | ||
195 | ip, port = server:getsockname() | ||
196 | -- print a message informing what's up | ||
197 | print("Please telnet to localhost on port " .. port) | ||
198 | print("After connecting, you have 10s to enter a line to be echoed") | ||
199 | -- loop forever waiting for clients | ||
200 | while 1 do | ||
201 | -- wait for a conection from any client | ||
202 | client, err = server:accept() | ||
203 | -- make sure we don't block waiting for this client's line | ||
204 | client:settimeout(10) | ||
205 | -- receive the line | ||
206 | line, err = client:receive() | ||
207 | -- if there was no error, send it back to the client | ||
208 | if not err then | ||
209 | client:send(line .. "\n") | ||
210 | end | ||
211 | -- done with client, close the object | ||
212 | client:close() | ||
213 | end | ||
214 | </pre> | ||
215 | </blockquote> | ||
216 | |||
217 | <!-- udp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
218 | |||
219 | <h3 id=udp>UDP</h3> | ||
220 | |||
221 | <p> | ||
222 | UDP (User Datagram Protocol) is a non-reliable datagram protocol. In | ||
223 | other words, applications communicating through UDP send and receive | ||
224 | data as independent blocks, which are not guaranteed to reach the other | ||
225 | end. Even when they do reach the other end, they are not guaranteed to be | ||
226 | error free. Data transfers are atomic, one datagram at a time. Reading | ||
227 | only part of a datagram discards the rest, so that the following read | ||
228 | operation will act on the next datagram. The advantages are in | ||
229 | simplicity (no connection setup) and performance (no error checking or | ||
230 | error correction). | ||
231 | </p> | ||
232 | |||
233 | <p> | ||
234 | An UDP socket object is created by the | ||
235 | <a href=udp.html#udp><tt>socket.udp</tt></a> function. UDP | ||
236 | sockets do not need to be connected before use. The method | ||
237 | <a href=udp.html#sendto><tt>sendto</tt></a> | ||
238 | can be used immediately after creation to | ||
239 | send a datagram to IP address and port. Host names are not allowed | ||
240 | because performing name resolution for each packet would be forbiddingly | ||
241 | slow. Methods | ||
242 | <a href=udp.html#receive><tt>receive</tt></a> and | ||
243 | <a href=udp.html#receivefrom><tt>receivefrom</tt></a> | ||
244 | can be used to retrieve datagrams, the latter returning the IP and port of | ||
245 | the sender as extra return values (thus being slightly less | ||
246 | efficient). | ||
247 | </p> | ||
248 | |||
249 | <p> | ||
250 | When communication is performed repeatedly with a single peer, an | ||
251 | application should call the | ||
252 | <a href=udp.html#setpeername><tt>setpeername</tt></a> method to specify a | ||
253 | permanent partner. Methods | ||
254 | <a href=udp.html#sendto><tt>sendto</tt></a> and | ||
255 | <a href=udp.html#receivefrom><tt>receivefrom</tt></a> | ||
256 | can no longer be used, but the method | ||
257 | <a href=udp.html#send><tt>send</tt></a> can be used to send data | ||
258 | directly to the peer, and the method | ||
259 | <a href=udp.html#receive><tt>receive</tt></a> | ||
260 | will only return datagrams originating | ||
261 | from that peer. There is about 30% performance gain due to this practice. | ||
262 | </p> | ||
263 | |||
264 | <p> | ||
265 | To associate an UDP socket with a local address, an application calls the | ||
266 | <a href=udp.html#setsockname><tt>setsockname</tt></a> | ||
267 | method <em>before</em> sending any datagrams. Otherwise, the socket is | ||
268 | automatically bound to an ephemeral address before the first data | ||
269 | transmission and once bound the local address cannot be changed. | ||
270 | The other methods available for UDP sockets are | ||
271 | <a href=udp.html#getpeername><tt>getpeername</tt></a>, | ||
272 | <a href=udp.html#getsockname><tt>getsockname</tt></a>, | ||
273 | <a href=udp.html#settimeout><tt>settimeout</tt></a>, | ||
274 | <a href=udp.html#setoption><tt>setoption</tt></a> and | ||
275 | <a href=udp.html#close><tt>close</tt></a>. | ||
276 | </p> | ||
277 | |||
278 | <p> | ||
279 | Example: | ||
280 | </p> | ||
281 | <blockquote> | ||
282 | <p> | ||
283 | A simple daytime client, using LuaSocket. The program connects to a remote | ||
284 | server and tries to retrieve the daytime, printing the answer it got or an | ||
285 | error message. | ||
286 | </p> | ||
287 | |||
288 | <pre class=example> | ||
289 | host = "localhost" -- change here to the host you want to contact | ||
290 | port = port or 13 | ||
291 | -- convert host name to ip address | ||
292 | ip, err = socket.toip(host) | ||
293 | assert(ip, err) | ||
294 | -- create a new UDP object | ||
295 | udp = socket.udp() | ||
296 | -- contact daytime host | ||
297 | nsent, err = udp:sendto("anything", ip, port) | ||
298 | assert(not err, err) | ||
299 | -- retrieve the answer | ||
300 | dgram, err = udp:receive() | ||
301 | assert(dgram, err) | ||
302 | -- display to user | ||
303 | print(dgram) | ||
304 | </pre> | ||
305 | </blockquote> | ||
306 | |||
307 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
308 | |||
309 | <div class=footer> | ||
310 | <hr> | ||
311 | <center> | ||
312 | <p class=bar> | ||
313 | <a href="home.html">home</a> · | ||
314 | <a href="home.html#down">download</a> · | ||
315 | <a href="introduction.html">introduction</a> · | ||
316 | <a href="reference.html">reference</a> | ||
317 | </p> | ||
318 | <p> | ||
319 | <small> | ||
320 | Last modified by Diego Nehab on <br> | ||
321 | Sat Aug 9 01:00:41 PDT 2003 | ||
322 | </small> | ||
323 | </p> | ||
324 | </center> | ||
325 | </div> | ||
326 | |||
327 | </body> | ||
328 | </html> | ||