beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / luasocket / doc / tcp.html
blob151a4c993c12f9f6017533ac389142f252fff62a
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
5 <head>
6 <meta name="description" content="LuaSocket: The TCP/IP support">
7 <meta name="keywords" content="Lua, LuaSocket, Socket, TCP, Library, Network, Support">
8 <title>LuaSocket: TCP/IP support</title>
9 <link rel="stylesheet" href="reference.css" type="text/css">
10 </head>
12 <body>
14 <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
16 <div class=header>
17 <hr>
18 <center>
19 <table summary="LuaSocket logo">
20 <tr><td align=center><a href="http://www.lua.org">
21 <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png">
22 </a></td></tr>
23 <tr><td align=center valign=top>Network support for the Lua language
24 </td></tr>
25 </table>
26 <p class=bar>
27 <a href="index.html">home</a> &middot;
28 <a href="index.html#download">download</a> &middot;
29 <a href="installation.html">installation</a> &middot;
30 <a href="introduction.html">introduction</a> &middot;
31 <a href="reference.html">reference</a>
32 </p>
33 </center>
34 <hr>
35 </div>
37 <!-- tcp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
39 <h2 id="tcp">TCP</h2>
41 <!-- socket.tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
43 <p class=name id="socket.tcp">
44 socket.<b>tcp()</b>
45 </p>
47 <p class=description>
48 Creates and returns an IPv4 TCP master object. A master object can
49 be transformed into a server object with the method
50 <a href=#listen><tt>listen</tt></a> (after a call to <a
51 href=#bind><tt>bind</tt></a>) or into a client object with
52 the method <a href=#connect><tt>connect</tt></a>. The only other
53 method supported by a master object is the
54 <a href=#close><tt>close</tt></a> method.</p>
56 <p class=return>
57 In case of success, a new master object is returned. In case of error,
58 <b><tt>nil</tt></b> is returned, followed by an error message.
59 </p>
61 <!-- socket.tcp6 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
63 <p class=name id="socket.tcp6">
64 socket.<b>tcp6()</b>
65 </p>
67 <p class=description>
68 Creates and returns an IPv6 TCP master object. A master object can
69 be transformed into a server object with the method
70 <a href=#listen><tt>listen</tt></a> (after a call to <a
71 href=#bind><tt>bind</tt></a>) or into a client object with
72 the method <a href=#connect><tt>connect</tt></a>. The only other
73 method supported by a master object is the
74 <a href=#close><tt>close</tt></a> method.</p>
76 <p class=return>
77 In case of success, a new master object is returned. In case of error,
78 <b><tt>nil</tt></b> is returned, followed by an error message.
79 </p>
81 <p class=note>
82 Note: The TCP object returned will have the option
83 "<tt>ipv6-v6only</tt>" set to <tt><b>true</b></tt>.
84 </p>
86 <!-- accept +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
88 <p class=name id="accept">
89 server:<b>accept()</b>
90 </p>
92 <p class=description>
93 Waits for a remote connection on the server
94 object and returns a client object representing that connection.
95 </p>
97 <p class=return>
98 If a connection is successfully initiated, a client object is returned.
99 If a timeout condition is met, the method returns <b><tt>nil</tt></b>
100 followed by the error string '<tt>timeout</tt>'. Other errors are
101 reported by <b><tt>nil</tt></b> followed by a message describing the error.
102 </p>
104 <p class=note>
105 Note: calling <a href=socket.html#select><tt>socket.select</tt></a>
106 with a server object in
107 the <tt>recvt</tt> parameter before a call to <tt>accept</tt> does
108 <em>not</em> guarantee <tt>accept</tt> will return immediately. Use the <a
109 href=#settimeout><tt>settimeout</tt></a> method or <tt>accept</tt>
110 might block until <em>another</em> client shows up.
111 </p>
113 <!-- bind +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
115 <p class=name id="bind">
116 master:<b>bind(</b>address, port<b>)</b>
117 </p>
119 <p class=description>
120 Binds a master object to <tt>address</tt> and <tt>port</tt> on the
121 local host.
123 <p class=parameters>
124 <tt>Address</tt> can be an IP address or a host name.
125 <tt>Port</tt> must be an integer number in the range [0..64K).
126 If <tt>address</tt>
127 is '<tt>*</tt>', the system binds to all local interfaces
128 using the <tt>INADDR_ANY</tt> constant or
129 <tt>IN6ADDR_ANY_INIT</tt>, according to the family.
130 If <tt>port</tt> is 0, the system automatically
131 chooses an ephemeral port.
132 </p>
134 <p class=return>
135 In case of success, the method returns 1. In case of error, the
136 method returns <b><tt>nil</tt></b> followed by an error message.
137 </p>
139 <p class=note>
140 Note: The function <a href=socket.html#bind><tt>socket.bind</tt></a>
141 is available and is a shortcut for the creation of server sockets.
142 </p>
144 <!-- close ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
146 <p class=name id="close">
147 master:<b>close()</b><br>
148 client:<b>close()</b><br>
149 server:<b>close()</b>
150 </p>
152 <p class=description>
153 Closes a TCP object. The internal socket used by the object is closed
154 and the local address to which the object was
155 bound is made available to other applications. No further operations
156 (except for further calls to the <tt>close</tt> method) are allowed on
157 a closed socket.
158 </p>
160 <p class=note>
161 Note: It is important to close all used sockets once they are not
162 needed, since, in many systems, each socket uses a file descriptor,
163 which are limited system resources. Garbage-collected objects are
164 automatically closed before destruction, though.
165 </p>
167 <!-- connect ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
169 <p class=name id="connect">
170 master:<b>connect(</b>address, port<b>)</b>
171 </p>
173 <p class=description>
174 Attempts to connect a master object to a remote host, transforming it into a
175 client object.
176 Client objects support methods
177 <a href=#send><tt>send</tt></a>,
178 <a href=#receive><tt>receive</tt></a>,
179 <a href=#getsockname><tt>getsockname</tt></a>,
180 <a href=#getpeername><tt>getpeername</tt></a>,
181 <a href=#settimeout><tt>settimeout</tt></a>,
182 and <a href=#close><tt>close</tt></a>.
183 </p>
185 <p class=parameters>
186 <tt>Address</tt> can be an IP address or a host name.
187 <tt>Port</tt> must be an integer number in the range [1..64K).
188 </p>
190 <p class=return>
191 In case of error, the method returns <b><tt>nil</tt></b> followed by a string
192 describing the error. In case of success, the method returns 1.
193 </p>
195 <p class=note>
196 Note: The function <a href=socket.html#connect><tt>socket.connect</tt></a>
197 is available and is a shortcut for the creation of client sockets.
198 </p>
200 <p class=note>
201 Note: Starting with LuaSocket 2.0,
202 the <a href=#settimeout><tt>settimeout</tt></a>
203 method affects the behavior of <tt>connect</tt>, causing it to return
204 with an error in case of a timeout. If that happens, you can still call <a
205 href=socket.html#select><tt>socket.select</tt></a> with the socket in the
206 <tt>sendt</tt> table. The socket will be writable when the connection is
207 established.
208 </p>
210 <p class=note>
211 Note: Starting with LuaSocket 2.1, the host name resolution
212 depends on whether the socket was created by <a
213 href=#socket.tcp><tt>socket.tcp</tt></a> or <a
214 href=#socket.tcp6><tt>socket.tcp6</tt></a>. Addresses from
215 the appropriate family are tried in succession until the
216 first success or until the last failure.
217 </p>
219 <!-- getpeername ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
221 <p class=name id="getpeername">
222 client:<b>getpeername()</b>
223 </p>
225 <p class=description>
226 Returns information about the remote side of a connected client object.
227 </p>
229 <p class=return>
230 Returns a string with the IP address of the peer, the
231 port number that peer is using for the connection,
232 and a string with the family ("<tt>inet</tt>" or "<tt>inet6</tt>").
233 In case of error, the method returns <b><tt>nil</tt></b>.
234 </p>
236 <p class=note>
237 Note: It makes no sense to call this method on server objects.
238 </p>
240 <!-- getsockname ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
242 <p class=name id="getsockname">
243 master:<b>getsockname()</b><br>
244 client:<b>getsockname()</b><br>
245 server:<b>getsockname()</b>
246 </p>
248 <p class=description>
249 Returns the local address information associated to the object.
250 </p>
252 <p class=return>
253 The method returns a string with local IP address, a number with
254 the local port,
255 and a string with the family ("<tt>inet</tt>" or "<tt>inet6</tt>").
256 In case of error, the method returns <b><tt>nil</tt></b>.
257 </p>
259 <!-- getstats +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
261 <p class=name id="getstats">
262 master:<b>getstats()</b><br>
263 client:<b>getstats()</b><br>
264 server:<b>getstats()</b><br>
265 </p>
267 <p class=description>
268 Returns accounting information on the socket, useful for throttling
269 of bandwidth.
270 </p>
272 <p class=return>
273 The method returns the number of bytes received, the number of bytes sent,
274 and the age of the socket object in seconds.
275 </p>
277 <!-- listen ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
279 <p class=name id="listen">
280 master:<b>listen(</b>backlog<b>)</b>
281 </p>
283 <p class=description>
284 Specifies the socket is willing to receive connections, transforming the
285 object into a server object. Server objects support the
286 <a href=#accept><tt>accept</tt></a>,
287 <a href=#getsockname><tt>getsockname</tt></a>,
288 <a href=#setoption><tt>setoption</tt></a>,
289 <a href=#settimeout><tt>settimeout</tt></a>,
290 and <a href=#close><tt>close</tt></a> methods.
291 </p>
293 <p class=parameters>
294 The parameter <tt>backlog</tt> specifies the number of client
295 connections that can
296 be queued waiting for service. If the queue is full and another client
297 attempts connection, the connection is refused.
298 </p>
300 <p class=return>
301 In case of success, the method returns 1. In case of error, the
302 method returns <b><tt>nil</tt></b> followed by an error message.
303 </p>
305 <!-- receive ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
307 <p class=name id="receive">
308 client:<b>receive(</b>[pattern [, prefix]]<b>)</b>
309 </p>
311 <p class=description>
312 Reads data from a client object, according to the specified <em>read
313 pattern</em>. Patterns follow the Lua file I/O format, and the difference in performance between all patterns is negligible.
314 </p>
316 <p class=parameters>
317 <tt>Pattern</tt> can be any of the following:
318 </p>
320 <ul>
321 <li> '<tt>*a</tt>': reads from the socket until the connection is
322 closed. No end-of-line translation is performed;
323 <li> '<tt>*l</tt>': reads a line of text from the socket. The line is
324 terminated by a LF character (ASCII&nbsp;10), optionally preceded by a
325 CR character (ASCII&nbsp;13). The CR and LF characters are not included in
326 the returned line. In fact, <em>all</em> CR characters are
327 ignored by the pattern. This is the default pattern;
328 <li> <tt>number</tt>: causes the method to read a specified <tt>number</tt>
329 of bytes from the socket.
330 </ul>
332 <p class=parameters>
333 <tt>Prefix</tt> is an optional string to be concatenated to the beginning
334 of any received data before return.
335 </p>
337 <p class=return>
338 If successful, the method returns the received pattern. In case of error,
339 the method returns <tt><b>nil</b></tt> followed by an error
340 message, followed by a (possibly empty) string containing
341 the partial that was received. The error message can be
342 the string '<tt>closed</tt>' in case the connection was
343 closed before the transmission was completed or the string
344 '<tt>timeout</tt>' in case there was a timeout during the operation.
345 </p>
347 <p class=note>
348 <b>Important note</b>: This function was changed <em>severely</em>. It used
349 to support multiple patterns (but I have never seen this feature used) and
350 now it doesn't anymore. Partial results used to be returned in the same
351 way as successful results. This last feature violated the idea that all
352 functions should return <tt><b>nil</b></tt> on error. Thus it was changed
353 too.
354 </p>
356 <!-- send +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
358 <p class=name id="send">
359 client:<b>send(</b>data [, i [, j]]<b>)</b>
360 </p>
362 <p class=description>
363 Sends <tt>data</tt> through client object.
364 </p>
366 <p class=parameters>
367 <tt>Data</tt> is the string to be sent. The optional arguments
368 <tt>i</tt> and <tt>j</tt> work exactly like the standard
369 <tt>string.sub</tt> Lua function to allow the selection of a
370 substring to be sent.
371 </p>
373 <p class=return>
374 If successful, the method returns the index of the last byte
375 within <tt>[i, j]</tt> that has been sent. Notice that, if
376 <tt>i</tt> is 1 or absent, this is effectively the total
377 number of bytes sent. In case of error, the method returns
378 <b><tt>nil</tt></b>, followed by an error message, followed
379 by the index of the last byte within <tt>[i, j]</tt> that
380 has been sent. You might want to try again from the byte
381 following that. The error message can be '<tt>closed</tt>'
382 in case the connection was closed before the transmission
383 was completed or the string '<tt>timeout</tt>' in case
384 there was a timeout during the operation.
385 </p>
387 <p class=note>
388 Note: Output is <em>not</em> buffered. For small strings,
389 it is always better to concatenate them in Lua
390 (with the '<tt>..</tt>' operator) and send the result in one call
391 instead of calling the method several times.
392 </p>
394 <!-- setoption ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
396 <p class=name id="setoption">
397 client:<b>setoption(</b>option [, value]<b>)</b><br>
398 server:<b>setoption(</b>option [, value]<b>)</b>
399 </p>
401 <p class=description>
402 Sets options for the TCP object. Options are only needed by low-level or
403 time-critical applications. You should only modify an option if you
404 are sure you need it.
405 </p>
407 <p class=parameters>
408 <tt>Option</tt> is a string with the option name, and <tt>value</tt>
409 depends on the option being set:
411 <ul>
413 <li> '<tt>keepalive</tt>': Setting this option to <tt>true</tt> enables
414 the periodic transmission of messages on a connected socket. Should the
415 connected party fail to respond to these messages, the connection is
416 considered broken and processes using the socket are notified;
418 <li> '<tt>linger</tt>': Controls the action taken when unsent data are
419 queued on a socket and a close is performed. The value is a table with a
420 boolean entry '<tt>on</tt>' and a numeric entry for the time interval
421 '<tt>timeout</tt>' in seconds. If the '<tt>on</tt>' field is set to
422 <tt>true</tt>, the system will block the process on the close attempt until
423 it is able to transmit the data or until '<tt>timeout</tt>' has passed. If
424 '<tt>on</tt>' is <tt>false</tt> and a close is issued, the system will
425 process the close in a manner that allows the process to continue as
426 quickly as possible. I do not advise you to set this to anything other than
427 zero;
429 <li> '<tt>reuseaddr</tt>': Setting this option indicates that the rules
430 used in validating addresses supplied in a call to
431 <a href=#bind><tt>bind</tt></a> should allow reuse of local addresses;
433 <li> '<tt>tcp-nodelay</tt>': Setting this option to <tt>true</tt>
434 disables the Nagle's algorithm for the connection;
436 <li> '<tt>ipv6-v6only</tt>':
437 Setting this option to <tt>true</tt> restricts an <tt>inet6</tt> socket to
438 sending and receiving only IPv6 packets.
439 </ul>
441 <p class=return>
442 The method returns 1 in case of success, or <b><tt>nil</tt></b>
443 followed by an error message otherwise.
444 </p>
446 <p class=note>
447 Note: The descriptions above come from the man pages.
448 </p>
450 <!-- getoption ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
452 <p class=name id="getoption">
453 client:<b>getoption(</b>option)</b><br>
454 server:<b>getoption(</b>option)</b>
455 </p>
457 <p class=description>
458 Gets options for the TCP object.
459 See <a href=#setoption><tt>setoption</tt></a> for description of the
460 option names and values.
461 </p>
463 <p class=parameters>
464 <tt>Option</tt> is a string with the option name.
465 <ul>
467 <li> '<tt>keepalive</tt>'
468 <li> '<tt>linger</tt>'
469 <li> '<tt>reuseaddr</tt>'
470 <li> '<tt>tcp-nodelay</tt>'
471 </ul>
473 <p class=return>
474 The method returns the option <tt>value</tt> in case of success, or
475 <b><tt>nil</tt></b> followed by an error message otherwise.
476 </p>
478 <!-- setstats +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
480 <p class=name id="setstats">
481 master:<b>setstats(</b>received, sent, age<b>)</b><br>
482 client:<b>setstats(</b>received, sent, age<b>)</b><br>
483 server:<b>setstats(</b>received, sent, age<b>)</b><br>
484 </p>
486 <p class=description>
487 Resets accounting information on the socket, useful for throttling
488 of bandwidth.
489 </p>
491 <p class=parameters>
492 <tt>Received</tt> is a number with the new number of bytes received.
493 <tt>Sent</tt> is a number with the new number of bytes sent.
494 <tt>Age</tt> is the new age in seconds.
495 </p>
497 <p class=return>
498 The method returns 1 in case of success and <tt><b>nil</b></tt> otherwise.
499 </p>
501 <!-- settimeout +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
503 <p class=name id="settimeout">
504 master:<b>settimeout(</b>value [, mode]<b>)</b><br>
505 client:<b>settimeout(</b>value [, mode]<b>)</b><br>
506 server:<b>settimeout(</b>value [, mode]<b>)</b>
507 </p>
509 <p class=description>
510 Changes the timeout values for the object. By default,
511 all I/O operations are blocking. That is, any call to the methods
512 <a href=#send><tt>send</tt></a>,
513 <a href=#receive><tt>receive</tt></a>, and
514 <a href=#accept><tt>accept</tt></a>
515 will block indefinitely, until the operation completes. The
516 <tt>settimeout</tt> method defines a limit on the amount of time the
517 I/O methods can block. When a timeout is set and the specified amount of
518 time has elapsed, the affected methods give up and fail with an error code.
519 </p>
521 <p class=parameters>
522 The amount of time to wait is specified as the
523 <tt>value</tt> parameter, in seconds. There are two timeout modes and
524 both can be used together for fine tuning:
525 </p>
527 <ul>
528 <li> '<tt>b</tt>': <em>block</em> timeout. Specifies the upper limit on
529 the amount of time LuaSocket can be blocked by the operating system
530 while waiting for completion of any single I/O operation. This is the
531 default mode;</li>
533 <li> '<tt>t</tt>': <em>total</em> timeout. Specifies the upper limit on
534 the amount of time LuaSocket can block a Lua script before returning from
535 a call.</li>
536 </ul>
538 <p class=parameters>
539 The <b><tt>nil</tt></b> timeout <tt>value</tt> allows operations to block
540 indefinitely. Negative timeout values have the same effect.
541 </p>
543 <p class=note>
544 Note: although timeout values have millisecond precision in LuaSocket,
545 large blocks can cause I/O functions not to respect timeout values due
546 to the time the library takes to transfer blocks to and from the OS
547 and to and from the Lua interpreter. Also, function that accept host names
548 and perform automatic name resolution might be blocked by the resolver for
549 longer than the specified timeout value.
550 </p>
552 <p class=note>
553 Note: The old <tt>timeout</tt> method is deprecated. The name has been
554 changed for sake of uniformity, since all other method names already
555 contained verbs making their imperative nature obvious.
556 </p>
558 <!-- shutdown +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
560 <p class=name id="shutdown">
561 client:<b>shutdown(</b>mode<b>)</b><br>
562 </p>
564 <p class=description>
565 Shuts down part of a full-duplex connection.
566 </p>
568 <p class=parameters>
569 Mode tells which way of the connection should be shut down and can
570 take the value:
571 <ul>
572 <li>"<tt>both</tt>": disallow further sends and receives on the object.
573 This is the default mode;
574 <li>"<tt>send</tt>": disallow further sends on the object;
575 <li>"<tt>receive</tt>": disallow further receives on the object.
576 </ul>
578 <p class=return>
579 This function returns 1.
580 </p>
582 <!-- dirty +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
584 <p class=name id="dirty">
585 master:<b>dirty()</b><br>
586 client:<b>dirty()</b><br>
587 server:<b>dirty()</b>
588 </p>
590 <p class=description>
591 Check the read buffer status.
592 </p>
594 <p class=return>
595 Returns <tt>true</tt> if there is any data in the read buffer, <tt>false</tt> otherwise.
596 </p>
598 <p class=note>
599 Note: <b>This is an internal method, any use is unlikely to be portable.</b>
600 </p>
602 <!-- getfd +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
604 <p class=name id="getfd">
605 master:<b>getfd()</b><br>
606 client:<b>getfd()</b><br>
607 server:<b>getfd()</b>
608 </p>
610 <p class=description>
611 Returns the underling socket descriptor or handle associated to the object.
612 </p>
614 <p class=return>
615 The descriptor or handle. In case the object has been closed, the return will be -1.
616 </p>
618 <p class=note>
619 Note: <b>This is an internal method, any use is unlikely to be portable.</b>
620 </p>
622 <!-- setfd +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
624 <p class=name id="setfd">
625 master:<b>setfd(</b>fd<b>)</b><br>
626 client:<b>setfd(</b>fd<b>)</b><br>
627 server:<b>setfd(</b>fd<b>)</b>
628 </p>
630 <p class=description>
631 Sets the underling socket descriptor or handle associated to the object. The current one is simply replaced, not closed, and no other change to the object state is made.
632 </p>
634 <p class=return>
635 No return value.
636 </p>
638 <p class=note>
639 Note: <b>This is an internal method, any use is unlikely to be portable.</b>
640 </p>
642 <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
644 <div class=footer>
645 <hr>
646 <center>
647 <p class=bar>
648 <a href="index.html">home</a> &middot;
649 <a href="index.html#down">download</a> &middot;
650 <a href="installation.html">installation</a> &middot;
651 <a href="introduction.html">introduction</a> &middot;
652 <a href="reference.html">reference</a>
653 </p>
655 <small>
656 Last modified by Diego Nehab on <br>
657 Thu Apr 20 00:25:57 EDT 2006
658 </small>
659 </p>
660 </center>
661 </div>
663 </body>
664 </html>