socket.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta name="description" content="LuaSocket: The core namespace">
  6. <meta name="keywords" content="Lua, LuaSocket, Socket, Network, Library, Support">
  7. <title>LuaSocket: The socket namespace</title>
  8. <link rel="stylesheet" href="reference.css" type="text/css">
  9. </head>
  10. <body>
  11. <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  12. <div class=header>
  13. <hr>
  14. <center>
  15. <table summary="LuaSocket logo">
  16. <tr><td align=center><a href="http://www.lua.org">
  17. <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png">
  18. </a></td></tr>
  19. <tr><td align=center valign=top>Network support for the Lua language
  20. </td></tr>
  21. </table>
  22. <p class=bar>
  23. <a href="index.html">home</a> &middot;
  24. <a href="index.html#download">download</a> &middot;
  25. <a href="installation.html">installation</a> &middot;
  26. <a href="introduction.html">introduction</a> &middot;
  27. <a href="reference.html">reference</a>
  28. </p>
  29. </center>
  30. <hr>
  31. </div>
  32. <!-- socket +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  33. <h2 id=socket>The socket namespace</h2>
  34. <p>
  35. The <tt>socket</tt> namespace contains the core functionality of LuaSocket.
  36. </p>
  37. <p>
  38. To obtain the <tt>socket</tt> namespace, run:
  39. </p>
  40. <pre class=example>
  41. -- loads the socket module
  42. local socket = require("socket")
  43. </pre>
  44. <!-- headers.canonic ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  45. <p class=name id="headers.canonic">
  46. socket.headers.<b>canonic</b></p>
  47. <p> The <tt>socket.headers.canonic</tt> table
  48. is used by the HTTP and SMTP modules to translate from
  49. lowercase field names back into their canonic
  50. capitalization. When a lowercase field name exists as a key
  51. in this table, the associated value is substituted in
  52. whenever the field name is sent out.
  53. </p>
  54. <p>
  55. You can obtain the <tt>headers</tt> namespace if case run-time
  56. modifications are required by running:
  57. </p>
  58. <pre class=example>
  59. -- loads the headers module
  60. local headers = require("headers")
  61. </pre>
  62. <!-- bind ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  63. <p class=name id=bind>
  64. socket.<b>bind(</b>address, port [, backlog]<b>)</b>
  65. </p>
  66. <p class=description>
  67. This function is a shortcut that creates and returns a TCP server object
  68. bound to a local <tt>address</tt> and <tt>port</tt>, ready to
  69. accept client connections. Optionally,
  70. user can also specify the <tt>backlog</tt> argument to the
  71. <a href=tcp.html#listen><tt>listen</tt></a> method (defaults to 32).
  72. </p>
  73. <p class=note>
  74. Note: The server object returned will have the option "<tt>reuseaddr</tt>"
  75. set to <tt><b>true</b></tt>.
  76. </p>
  77. <!-- connect ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  78. <p class=name id=connect>
  79. socket.<b>connect[46](</b>address, port [, locaddr] [, locport] [, family]<b>)</b>
  80. </p>
  81. <p class=description>
  82. This function is a shortcut that creates and returns a TCP client object
  83. connected to a remote <tt>address</tt> at a given <tt>port</tt>. Optionally,
  84. the user can also specify the local address and port to bind
  85. (<tt>locaddr</tt> and <tt>locport</tt>), or restrict the socket family
  86. to "<tt>inet</tt>" or "<tt>inet6</tt>".
  87. Without specifying <tt>family</tt> to <tt>connect</tt>, whether a tcp or tcp6
  88. connection is created depends on your system configuration. Two variations
  89. of connect are defined as simple helper functions that restrict the
  90. <tt>family</tt>, <tt>socket.connect4</tt> and <tt>socket.connect6</tt>.
  91. </p>
  92. <!-- debug ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  93. <p class=name id=debug>
  94. socket.<b>_DEBUG</b>
  95. </p>
  96. <p class=description>
  97. This constant is set to <tt><b>true</b></tt> if the library was compiled
  98. with debug support.
  99. </p>
  100. <!-- datagramsize +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  101. <p class=name id=debug>
  102. socket.<b>_DATAGRAMSIZE</b>
  103. </p>
  104. <p class=description>
  105. Default datagram size used by calls to
  106. <a href="udp.html#receive"<tt>receive</tt></a> and
  107. <a href="udp.html#receivefrom"><tt>receivefrom</tt></a>.
  108. (Unless changed in compile time, the value is 8192.)
  109. </p>
  110. <!-- get time +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  111. <p class=name id=gettime>
  112. socket.<b>gettime()</b>
  113. </p>
  114. <p class=description>
  115. Returns the UNIX time in seconds. You should subtract the values returned by this function
  116. to get meaningful values.
  117. </p>
  118. <pre class=example>
  119. t = socket.gettime()
  120. -- do stuff
  121. print(socket.gettime() - t .. " seconds elapsed")
  122. </pre>
  123. <!-- newtry +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  124. <p class=name id=newtry>
  125. socket.<b>newtry(</b>finalizer<b>)</b>
  126. </p>
  127. <p class=description>
  128. Creates and returns a <em>clean</em>
  129. <a href="#try"><tt>try</tt></a>
  130. function that allows for cleanup before the exception
  131. is raised.
  132. </p>
  133. <p class=parameters>
  134. <tt>Finalizer</tt> is a function that will be called before
  135. <tt>try</tt> throws the exception.
  136. </p>
  137. <p class=return>
  138. The function returns your customized <tt>try</tt> function.
  139. </p>
  140. <p class=note>
  141. Note: This idea saved a <em>lot</em> of work with the
  142. implementation of protocols in LuaSocket:
  143. </p>
  144. <pre class=example>
  145. foo = socket.protect(function()
  146. -- connect somewhere
  147. local c = socket.try(socket.connect("somewhere", 42))
  148. -- create a try function that closes 'c' on error
  149. local try = socket.newtry(function() c:close() end)
  150. -- do everything reassured c will be closed
  151. try(c:send("hello there?\r\n"))
  152. local answer = try(c:receive())
  153. ...
  154. try(c:send("good bye\r\n"))
  155. c:close()
  156. end)
  157. </pre>
  158. <!-- protect +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  159. <p class=name id=protect>
  160. socket.<b>protect(</b>func<b>)</b>
  161. </p>
  162. <p class=description>
  163. Converts a function that throws exceptions into a safe function. This
  164. function only catches exceptions thrown by the <a href=#try><tt>try</tt></a>
  165. and <a href=#newtry><tt>newtry</tt></a> functions. It does not catch normal
  166. Lua errors.
  167. </p>
  168. <p class=parameters>
  169. <tt>Func</tt> is a function that calls
  170. <a href=#try><tt>try</tt></a> (or <tt>assert</tt>, or <tt>error</tt>)
  171. to throw exceptions.
  172. </p>
  173. <p class=return>
  174. Returns an equivalent function that instead of throwing exceptions in case of
  175. a failed <a href=#try><tt>try</tt></a> call, returns <tt><b>nil</b></tt>
  176. followed by an error message.
  177. </p>
  178. <!-- select +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  179. <p class=name id=select>
  180. socket.<b>select(</b>recvt, sendt [, timeout]<b>)</b>
  181. </p>
  182. <p class=description>
  183. Waits for a number of sockets to change status.
  184. </p>
  185. <p class=parameters>
  186. <tt>Recvt</tt> is an array with the sockets to test for characters
  187. available for reading. Sockets in the <tt>sendt</tt> array are watched to
  188. see if it is OK to immediately write on them. <tt>Timeout</tt> is the
  189. maximum amount of time (in seconds) to wait for a change in status. A
  190. <tt><b>nil</b></tt>, negative or omitted <tt>timeout</tt> value allows the
  191. function to block indefinitely. <tt>Recvt</tt> and <tt>sendt</tt> can also
  192. be empty tables or <tt><b>nil</b></tt>. Non-socket values (or values with
  193. non-numeric indices) in the arrays will be silently ignored.
  194. </p>
  195. <p class=return> The function returns a list with the sockets ready for
  196. reading, a list with the sockets ready for writing and an error message.
  197. The error message is "<tt>timeout</tt>" if a timeout
  198. condition was met, "<tt>select failed</tt>" if the call
  199. to <tt>select</tt> failed, and
  200. <tt><b>nil</b></tt> otherwise. The returned tables are
  201. doubly keyed both by integers and also by the sockets
  202. themselves, to simplify the test if a specific socket has
  203. changed status.
  204. </p>
  205. <p class=note>
  206. <b>Note:</b> <tt>select</tt> can monitor a limited number
  207. of sockets, as defined by the constant <tt>socket._SETSIZE</tt>. This
  208. number may be as high as 1024 or as low as 64 by default,
  209. depending on the system. It is usually possible to change this
  210. at compile time. Invoking <tt>select</tt> with a larger
  211. number of sockets will raise an error.
  212. </p>
  213. <p class=note>
  214. <b>Important note</b>: a known bug in WinSock causes <tt>select</tt> to fail
  215. on non-blocking TCP sockets. The function may return a socket as
  216. writable even though the socket is <em>not</em> ready for sending.
  217. </p>
  218. <p class=note>
  219. <b>Another important note</b>: calling select with a server socket in the receive parameter before a call to accept does <em>not</em> guarantee
  220. <a href=tcp.html#accept><tt>accept</tt></a> will return immediately.
  221. Use the <a href=tcp.html#settimeout><tt>settimeout</tt></a>
  222. method or <tt>accept</tt> might block forever.
  223. </p>
  224. <p class=note>
  225. <b>Yet another note</b>: If you close a socket and pass
  226. it to <tt>select</tt>, it will be ignored.
  227. </p>
  228. <p class=note>
  229. <b>Using select with non-socket objects</b>: Any object that implements <tt>getfd</tt> and <tt>dirty</tt> can be used with <tt>select</tt>, allowing objects from other libraries to be used within a <tt>socket.select</tt> driven loop.
  230. </p>
  231. <!-- setsize ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  232. <p class=name id=setsize>
  233. socket.<b>_SETSIZE</b>
  234. </p>
  235. <p class=description>
  236. The maximum number of sockets that the <a
  237. href=#select><tt>select</tt></a> function can handle.
  238. </p>
  239. <!-- sink ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  240. <p class=name id=sink>
  241. socket.<b>sink(</b>mode, socket<b>)</b>
  242. </p>
  243. <p class=description>
  244. Creates an
  245. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
  246. sink from a stream socket object.
  247. </p>
  248. <p class=parameters>
  249. <tt>Mode</tt> defines the behavior of the sink. The following
  250. options are available:
  251. </p>
  252. <ul>
  253. <li> <tt>"http-chunked"</tt>: sends data through socket after applying the
  254. <em>chunked transfer coding</em>, closing the socket when done;
  255. <li> <tt>"close-when-done"</tt>: sends all received data through the
  256. socket, closing the socket when done;
  257. <li> <tt>"keep-open"</tt>: sends all received data through the
  258. socket, leaving it open when done.
  259. </ul>
  260. <p>
  261. <tt>Socket</tt> is the stream socket object used to send the data.
  262. </p>
  263. <p class=return>
  264. The function returns a sink with the appropriate behavior.
  265. </p>
  266. <!-- skip ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  267. <p class=name id=skip>
  268. socket.<b>skip(</b>d [, ret<sub>1</sub>, ret<sub>2</sub> ... ret<sub>N</sub>]<b>)</b>
  269. </p>
  270. <p class=description>
  271. Drops a number of arguments and returns the remaining.
  272. </p>
  273. <p class=parameters>
  274. <tt>D</tt> is the number of arguments to drop. <tt>Ret<sub>1</sub></tt> to
  275. <tt>ret<sub>N</sub></tt> are the arguments.
  276. </p>
  277. <p class=return>
  278. The function returns <tt>ret<sub>d+1</sub></tt> to <tt>ret<sub>N</sub></tt>.
  279. </p>
  280. <p class=note>
  281. Note: This function is useful to avoid creation of dummy variables:
  282. </p>
  283. <pre class=example>
  284. -- get the status code and separator from SMTP server reply
  285. local code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)"))
  286. </pre>
  287. <!-- sleep ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  288. <p class=name id=sleep>
  289. socket.<b>sleep(</b>time<b>)</b>
  290. </p>
  291. <p class=description>
  292. Freezes the program execution during a given amount of time.
  293. </p>
  294. <p class=parameters>
  295. <tt>Time</tt> is the number of seconds to sleep for. If
  296. <tt>time</tt> is negative, the function returns immediately.
  297. </p>
  298. <!-- source +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  299. <p class=name id=source>
  300. socket.<b>source(</b>mode, socket [, length]<b>)</b>
  301. </p>
  302. <p class=description>
  303. Creates an
  304. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a>
  305. source from a stream socket object.
  306. </p>
  307. <p class=parameters>
  308. <tt>Mode</tt> defines the behavior of the source. The following
  309. options are available:
  310. </p>
  311. <ul>
  312. <li> <tt>"http-chunked"</tt>: receives data from socket and removes the
  313. <em>chunked transfer coding</em> before returning the data;
  314. <li> <tt>"by-length"</tt>: receives a fixed number of bytes from the
  315. socket. This mode requires the extra argument <tt>length</tt>;
  316. <li> <tt>"until-closed"</tt>: receives data from a socket until the other
  317. side closes the connection.
  318. </ul>
  319. <p>
  320. <tt>Socket</tt> is the stream socket object used to receive the data.
  321. </p>
  322. <p class=return>
  323. The function returns a source with the appropriate behavior.
  324. </p>
  325. <!-- socketinvalid ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  326. <p class=name id=socketinvalid>
  327. socket.<b>_SOCKETINVALID</b>
  328. </p>
  329. <p class=description>
  330. The OS value for an invalid socket.
  331. </p>
  332. <!-- try ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  333. <p class=name id=try>
  334. socket.<b>try(</b>ret<sub>1</sub> [, ret<sub>2</sub> ... ret<sub>N</sub>]<b>)</b>
  335. </p>
  336. <p class=description>
  337. Throws an exception in case <tt>ret<sub>1</sub></tt> is falsy, using
  338. <tt>ret<sub>2</sub></tt> as the error message. The exception is supposed to be caught
  339. by a <a href=#protect><tt>protect</tt></a>ed function only.
  340. </p>
  341. <p class=parameters>
  342. <tt>Ret<sub>1</sub></tt> to <tt>ret<sub>N</sub></tt> can be arbitrary
  343. arguments, but are usually the return values of a function call
  344. nested with <tt>try</tt>.
  345. </p>
  346. <p class=return>
  347. The function returns <tt>ret</tt><sub>1</sub> to <tt>ret</tt><sub>N</sub> if
  348. <tt>ret</tt><sub>1</sub> is not <tt><b>nil</b></tt> or <tt><b>false</b></tt>.
  349. Otherwise, it calls <tt>error</tt> passing <tt>ret</tt><sub>2</sub> wrapped
  350. in a table with metatable used by <a href=#protect><tt>protect</tt></a> to
  351. distinguish exceptions from runtime errors.
  352. </p>
  353. <pre class=example>
  354. -- connects or throws an exception with the appropriate error message
  355. c = socket.try(socket.connect("localhost", 80))
  356. </pre>
  357. <!-- version ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  358. <p class=name id=version>
  359. socket.<b>_VERSION</b>
  360. </p>
  361. <p class=description>
  362. This constant has a string describing the current LuaSocket version.
  363. </p>
  364. <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  365. <div class=footer>
  366. <hr>
  367. <center>
  368. <p class=bar>
  369. <a href="index.html">home</a> &middot;
  370. <a href="index.html#down">download</a> &middot;
  371. <a href="installation.html">installation</a> &middot;
  372. <a href="introduction.html">introduction</a> &middot;
  373. <a href="reference.html">reference</a>
  374. </p>
  375. <p>
  376. <small>
  377. Last modified by Diego Nehab on <br>
  378. Thu Apr 20 00:25:54 EDT 2006
  379. </small>
  380. </p>
  381. </center>
  382. </div>
  383. </body>
  384. </html>