udp.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 UDP support">
  6. <meta name="keywords" content="Lua, LuaSocket, Socket, UDP, Library, Network, Support">
  7. <title>LuaSocket: UDP support</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. <!-- udp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  33. <h2 id="udp">UDP</h2>
  34. <!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  35. <p class="name" id="close">
  36. connected:<b>close()</b><br>
  37. unconnected:<b>close()</b>
  38. </p>
  39. <p class="description">
  40. Closes a UDP object. The internal socket
  41. used by the object is closed and the local address to which the
  42. object was bound is made available to other applications. No
  43. further operations (except for further calls to the <tt>close</tt>
  44. method) are allowed on a closed socket.
  45. </p>
  46. <p class="note">
  47. Note: It is important to close all used sockets
  48. once they are not needed, since, in many systems, each socket uses
  49. a file descriptor, which are limited system resources.
  50. Garbage-collected objects are automatically closed before
  51. destruction, though.
  52. </p>
  53. <!-- getoption +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  54. <p class="name" id="getoption">
  55. connected:<b>getoption()</b><br>
  56. unconnected:<b>getoption()</b>
  57. </p>
  58. <p class="description">
  59. Gets an option value from the UDP object.
  60. See <a href=#setoption><tt>setoption</tt></a> for
  61. description of the option names and values.
  62. </p>
  63. <p class="parameters"><tt>Option</tt> is a string with the option name.
  64. <ul>
  65. <li> '<tt>dontroute</tt>'
  66. <li> '<tt>broadcast</tt>'
  67. <li> '<tt>reuseaddr</tt>'
  68. <li> '<tt>reuseport</tt>'
  69. <li> '<tt>ip-multicast-loop</tt>'
  70. <li> '<tt>ipv6-v6only</tt>'
  71. <li> '<tt>ip-multicast-if</tt>'
  72. <li> '<tt>ip-multicast-ttl</tt>'
  73. <li> '<tt>ip-add-membership</tt>'
  74. <li> '<tt>ip-drop-membership</tt>'
  75. </ul>
  76. </p>
  77. <p class=return>
  78. The method returns the option <tt>value</tt> in case of
  79. success, or
  80. <b><tt>nil</tt></b> followed by an error message otherwise.
  81. </p>
  82. <!-- getpeername +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  83. <p class="name" id="getpeername">
  84. connected:<b>getpeername()</b>
  85. </p>
  86. <p class="description">
  87. Retrieves information about the peer
  88. associated with a connected UDP object.
  89. </p>
  90. <p class=return>
  91. Returns a string with the IP address of the peer, the
  92. port number that peer is using for the connection,
  93. and a string with the family ("<tt>inet</tt>" or "<tt>inet6</tt>").
  94. In case of error, the method returns <b><tt>nil</tt></b>.
  95. </p>
  96. <p class="note">
  97. Note: It makes no sense to call this method on unconnected objects.
  98. </p>
  99. <!-- getsockname +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  100. <p class="name" id="getsockname">
  101. connected:<b>getsockname()</b><br>
  102. unconnected:<b>getsockname()</b>
  103. </p>
  104. <p class="description">
  105. Returns the local address information associated to the object.
  106. </p>
  107. <p class=return>
  108. The method returns a string with local IP address, a number with
  109. the local port,
  110. and a string with the family ("<tt>inet</tt>" or "<tt>inet6</tt>").
  111. In case of error, the method returns <b><tt>nil</tt></b>.
  112. </p>
  113. <p class="note">
  114. Note: UDP sockets are not bound to any address
  115. until the <a href="#setsockname"><tt>setsockname</tt></a> or the
  116. <a href="#sendto"><tt>sendto</tt></a> method is called for the
  117. first time (in which case it is bound to an ephemeral port and the
  118. wild-card address).
  119. </p>
  120. <!-- gettimeout +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  121. <p class=name id="gettimeout">
  122. connected:<b>settimeout(</b>value<b>)</b><br>
  123. unconnected:<b>settimeout(</b>value<b>)</b>
  124. </p>
  125. <p class=description>
  126. Returns the current timeout value.
  127. </p>
  128. <!-- receive +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  129. <p class="name" id="receive">
  130. connected:<b>receive(</b>[size]<b>)</b><br>
  131. unconnected:<b>receive(</b>[size]<b>)</b>
  132. </p>
  133. <p class="description">
  134. Receives a datagram from the UDP object. If
  135. the UDP object is connected, only datagrams coming from the peer
  136. are accepted. Otherwise, the returned datagram can come from any
  137. host.
  138. </p>
  139. <p class="parameters">
  140. The optional <tt>size</tt> parameter
  141. specifies the maximum size of the datagram to be retrieved. If
  142. there are more than <tt>size</tt> bytes available in the datagram,
  143. the excess bytes are discarded. If there are less then
  144. <tt>size</tt> bytes available in the current datagram, the
  145. available bytes are returned.
  146. If <tt>size</tt> is omitted, the
  147. compile-time constant <a
  148. href=socket.html#datagramsize><tt>socket._DATAGRAMSIZE</tt></a> is used
  149. (it defaults to 8192 bytes). Larger sizes will cause a
  150. temporary buffer to be allocated for the operation.
  151. </p>
  152. <p class="return">
  153. In case of success, the method returns the
  154. received datagram. In case of timeout, the method returns
  155. <b><tt>nil</tt></b> followed by the string '<tt>timeout</tt>'.
  156. </p>
  157. <!-- receivefrom +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  158. <p class="name" id="receivefrom">
  159. unconnected:<b>receivefrom(</b>[size]<b>)</b>
  160. </p>
  161. <p class="description">
  162. Works exactly as the <a href="#receive"><tt>receive</tt></a>
  163. method, except it returns the IP
  164. address and port as extra return values (and is therefore slightly less
  165. efficient).
  166. </p>
  167. <!-- send ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  168. <p class="name" id="send">
  169. connected:<b>send(</b>datagram<b>)</b>
  170. </p>
  171. <p class="description">
  172. Sends a datagram to the UDP peer of a connected object.
  173. </p>
  174. <p class="parameters">
  175. <tt>Datagram</tt> is a string with the datagram contents.
  176. The maximum datagram size for UDP is 64K minus IP layer overhead.
  177. However datagrams larger than the link layer packet size will be
  178. fragmented, which may deteriorate performance and/or reliability.
  179. </p>
  180. <p class="return">
  181. If successful, the method returns 1. In case of
  182. error, the method returns <b><tt>nil</tt></b> followed by an error message.
  183. </p>
  184. <p class="note">
  185. Note: In UDP, the <tt>send</tt> method never blocks
  186. and the only way it can fail is if the underlying transport layer
  187. refuses to send a message to the specified address (i.e. no
  188. interface accepts the address).
  189. </p>
  190. <!-- sendto ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  191. <p class="name" id="sendto">
  192. unconnected:<b>sendto(</b>datagram, ip, port<b>)</b>
  193. </p>
  194. <p class="description">
  195. Sends a datagram to the specified IP address and port number.
  196. </p>
  197. <p class="parameters">
  198. <tt>Datagram</tt> is a string with the
  199. datagram contents.
  200. The maximum datagram size for UDP is 64K minus IP layer overhead.
  201. However datagrams larger than the link layer packet size will be
  202. fragmented, which may deteriorate performance and/or reliability.
  203. <tt>Ip</tt> is the IP address of the recipient.
  204. Host names are <em>not</em> allowed for performance reasons.
  205. <tt>Port</tt> is the port number at the recipient.
  206. </p>
  207. <p class="return">
  208. If successful, the method returns 1. In case of
  209. error, the method returns <b><tt>nil</tt></b> followed by an error message.
  210. </p>
  211. <p class="note">
  212. Note: In UDP, the <tt>send</tt> method never blocks
  213. and the only way it can fail is if the underlying transport layer
  214. refuses to send a message to the specified address (i.e. no
  215. interface accepts the address).
  216. </p>
  217. <!-- setoption +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  218. <p class="name" id="setoption">
  219. connected:<b>setoption(</b>option [, value]<b>)</b><br>
  220. unconnected:<b>setoption(</b>option [, value]<b>)</b>
  221. </p>
  222. <p class="description">
  223. Sets options for the UDP object. Options are
  224. only needed by low-level or time-critical applications. You should
  225. only modify an option if you are sure you need it.</p>
  226. <p class="parameters"><tt>Option</tt> is a string with the option
  227. name, and <tt>value</tt> depends on the option being set:
  228. </p>
  229. <ul>
  230. <li> '<tt>dontroute</tt>': Indicates that outgoing
  231. messages should bypass the standard routing facilities.
  232. Receives a boolean value;
  233. <li> '<tt>broadcast</tt>': Requests permission to send
  234. broadcast datagrams on the socket.
  235. Receives a boolean value;
  236. <li> '<tt>reuseaddr</tt>': Indicates that the rules used in
  237. validating addresses supplied in a <tt>bind()</tt> call
  238. should allow reuse of local addresses.
  239. Receives a boolean value;
  240. <li> '<tt>reuseport</tt>': Allows completely duplicate
  241. bindings by multiple processes if they all set
  242. '<tt>reuseport</tt>' before binding the port.
  243. Receives a boolean value;
  244. <li> '<tt>ip-multicast-loop</tt>':
  245. Specifies whether or not a copy of an outgoing multicast
  246. datagram is delivered to the sending host as long as it is a
  247. member of the multicast group.
  248. Receives a boolean value;
  249. <li> '<tt>ipv6-v6only</tt>':
  250. Specifies whether to restrict <tt>inet6</tt> sockets to
  251. sending and receiving only IPv6 packets.
  252. Receive a boolean value;
  253. <li> '<tt>ip-multicast-if</tt>':
  254. Sets the interface over which outgoing multicast datagrams
  255. are sent.
  256. Receives an IP address;
  257. <li> '<tt>ip-multicast-ttl</tt>':
  258. Sets the Time To Live in the IP header for outgoing
  259. multicast datagrams.
  260. Receives a number;
  261. <li> '<tt>ip-add-membership</tt>':
  262. Joins the multicast group specified.
  263. Receives a table with fields
  264. <tt>multiaddr</tt> and <tt>interface</tt>, each containing an
  265. IP address;
  266. <li> '<tt>ip-drop-membership</tt>': Leaves the multicast
  267. group specified.
  268. Receives a table with fields
  269. <tt>multiaddr</tt> and <tt>interface</tt>, each containing an
  270. IP address.
  271. </ul>
  272. <p class="return">
  273. The method returns 1 in case of success, or
  274. <b><tt>nil</tt></b> followed by an error message otherwise.
  275. </p>
  276. <p class=note>
  277. Note: The descriptions above come from the man pages.
  278. </p>
  279. <!-- setpeername +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  280. <p class="name" id="setpeername">
  281. connected:<b>setpeername(</b>'*'<b>)</b><br>
  282. unconnected:<b>setpeername(</b>address, port<b>)</b>
  283. </p>
  284. <p class="description">
  285. Changes the peer of a UDP object. This
  286. method turns an unconnected UDP object into a connected UDP
  287. object or vice versa.
  288. </p>
  289. <p class="description">
  290. For connected objects, outgoing datagrams
  291. will be sent to the specified peer, and datagrams received from
  292. other peers will be discarded by the OS. Connected UDP objects must
  293. use the <a href="#send"><tt>send</tt></a> and
  294. <a href="#receive"><tt>receive</tt></a> methods instead of
  295. <a href="#sendto"><tt>sendto</tt></a> and
  296. <a href="#receivefrom"><tt>receivefrom</tt></a>.
  297. </p>
  298. <p class="parameters">
  299. <tt>Address</tt> can be an IP address or a
  300. host name. <tt>Port</tt> is the port number. If <tt>address</tt> is
  301. '<tt>*</tt>' and the object is connected, the peer association is
  302. removed and the object becomes an unconnected object again. In that
  303. case, the <tt>port</tt> argument is ignored.
  304. </p>
  305. <p class="return">
  306. In case of error the method returns
  307. <b><tt>nil</tt></b> followed by an error message. In case of success, the
  308. method returns 1.
  309. </p>
  310. <p class="note">
  311. Note: Since the address of the peer does not have
  312. to be passed to and from the OS, the use of connected UDP objects
  313. is recommended when the same peer is used for several transmissions
  314. and can result in up to 30% performance gains.
  315. </p>
  316. <p class=note>
  317. Note: Starting with LuaSocket 3.0, the host name resolution
  318. depends on whether the socket was created by <a
  319. href=#socket.udp><tt>socket.udp</tt></a> or <a
  320. href=#socket.udp6><tt>socket.udp6</tt></a>. Addresses from
  321. the appropriate family are tried in succession until the
  322. first success or until the last failure.
  323. </p>
  324. <!-- setsockname +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  325. <p class="name" id="setsockname">
  326. unconnected:<b>setsockname(</b>address, port<b>)</b>
  327. </p>
  328. <p class="description">
  329. Binds the UDP object to a local address.
  330. </p>
  331. <p class="parameters">
  332. <tt>Address</tt> can be an IP address or a
  333. host name. If <tt>address</tt> is '<tt>*</tt>' the system binds to
  334. all local interfaces using the constant <tt>INADDR_ANY</tt>. If
  335. <tt>port</tt> is 0, the system chooses an ephemeral port.
  336. </p>
  337. <p class="return">
  338. If successful, the method returns 1. In case of
  339. error, the method returns <b><tt>nil</tt></b> followed by an error
  340. message.
  341. </p>
  342. <p class="note">
  343. Note: This method can only be called before any
  344. datagram is sent through the UDP object, and only once. Otherwise,
  345. the system automatically binds the object to all local interfaces
  346. and chooses an ephemeral port as soon as the first datagram is
  347. sent. After the local address is set, either automatically by the
  348. system or explicitly by <tt>setsockname</tt>, it cannot be
  349. changed.
  350. </p>
  351. <!-- settimeout +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  352. <p class="name" id="settimeout">
  353. connected:<b>settimeout(</b>value<b>)</b><br>
  354. unconnected:<b>settimeout(</b>value<b>)</b>
  355. </p>
  356. <p class="description">
  357. Changes the timeout values for the object. By default, the
  358. <a href="#receive"><tt>receive</tt></a> and
  359. <a href="#receivefrom"><tt>receivefrom</tt></a>
  360. operations are blocking. That is, any call to the methods will block
  361. indefinitely, until data arrives. The <tt>settimeout</tt> function defines
  362. a limit on the amount of time the functions can block. When a timeout is
  363. set and the specified amount of time has elapsed, the affected methods
  364. give up and fail with an error code.
  365. </p>
  366. <p class="parameters">
  367. The amount of time to wait is specified as
  368. the <tt>value</tt> parameter, in seconds. The <b><tt>nil</tt></b> timeout
  369. <tt>value</tt> allows operations to block indefinitely. Negative
  370. timeout values have the same effect.
  371. </p>
  372. <p class="note">
  373. Note: In UDP, the <a href="#send"><tt>send</tt></a>
  374. and <a href="#sentdo"><tt>sendto</tt></a> methods never block (the
  375. datagram is just passed to the OS and the call returns
  376. immediately). Therefore, the <tt>settimeout</tt> method has no
  377. effect on them.
  378. </p>
  379. <p class="note">
  380. Note: The old <tt>timeout</tt> method is
  381. deprecated. The name has been changed for sake of uniformity, since
  382. all other method names already contained verbs making their
  383. imperative nature obvious.
  384. </p>
  385. <!-- socket.udp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  386. <p class="name" id="socket.udp">
  387. socket.<b>udp()</b>
  388. </p>
  389. <p class="description">
  390. Creates and returns an unconnected UDP object.
  391. Unconnected objects support the
  392. <a href="#sendto"><tt>sendto</tt></a>,
  393. <a href="#receive"><tt>receive</tt></a>,
  394. <a href="#receivefrom"><tt>receivefrom</tt></a>,
  395. <a href="#getoption"><tt>getoption</tt></a>,
  396. <a href="#getsockname"><tt>getsockname</tt></a>,
  397. <a href="#setoption"><tt>setoption</tt></a>,
  398. <a href="#settimeout"><tt>settimeout</tt></a>,
  399. <a href="#setpeername"><tt>setpeername</tt></a>,
  400. <a href="#setsockname"><tt>setsockname</tt></a>, and
  401. <a href="#close"><tt>close</tt></a>.
  402. The <a href="#setpeername"><tt>setpeername</tt></a>
  403. is used to connect the object.
  404. </p>
  405. <p class="return">
  406. In case of success, a new unconnected UDP object
  407. returned. In case of error, <b><tt>nil</tt></b> is returned, followed by
  408. an error message.
  409. </p>
  410. <p class=note>
  411. Note: The choice between IPv4 and IPv6 happens during a call to
  412. <a href=#sendto><tt>sendto</tt></a>, <a
  413. href=#setpeername><tt>setpeername</tt></a>, or <a
  414. href=#setsockname><tt>sockname</tt></a>, depending on the address
  415. family obtained from the resolver.
  416. </p>
  417. <p class=note>
  418. Note: Before the choice between IPv4 and IPv6 happens,
  419. the internal socket object is invalid and therefore <a
  420. href=#setoption><tt>setoption</tt></a> will fail.
  421. </p>
  422. <!-- socket.udp4 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  423. <p class="name" id="socket.udp">
  424. socket.<b>udp4()</b>
  425. </p>
  426. <p class="description">
  427. Creates and returns an unconnected IPv4 UDP object.
  428. Unconnected objects support the
  429. <a href="#sendto"><tt>sendto</tt></a>,
  430. <a href="#receive"><tt>receive</tt></a>,
  431. <a href="#receivefrom"><tt>receivefrom</tt></a>,
  432. <a href="#getoption"><tt>getoption</tt></a>,
  433. <a href="#getsockname"><tt>getsockname</tt></a>,
  434. <a href="#setoption"><tt>setoption</tt></a>,
  435. <a href="#settimeout"><tt>settimeout</tt></a>,
  436. <a href="#setpeername"><tt>setpeername</tt></a>,
  437. <a href="#setsockname"><tt>setsockname</tt></a>, and
  438. <a href="#close"><tt>close</tt></a>.
  439. The <a href="#setpeername"><tt>setpeername</tt></a>
  440. is used to connect the object.
  441. </p>
  442. <p class="return">
  443. In case of success, a new unconnected UDP object
  444. returned. In case of error, <b><tt>nil</tt></b> is returned, followed by
  445. an error message.
  446. </p>
  447. <!-- socket.udp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  448. <p class="name" id="socket.udp6">
  449. socket.<b>udp6()</b>
  450. </p>
  451. <p class="description">
  452. Creates and returns an unconnected IPv6 UDP object.
  453. Unconnected objects support the
  454. <a href="#sendto"><tt>sendto</tt></a>,
  455. <a href="#receive"><tt>receive</tt></a>,
  456. <a href="#receivefrom"><tt>receivefrom</tt></a>,
  457. <a href="#getoption"><tt>getoption</tt></a>,
  458. <a href="#getsockname"><tt>getsockname</tt></a>,
  459. <a href="#setoption"><tt>setoption</tt></a>,
  460. <a href="#settimeout"><tt>settimeout</tt></a>,
  461. <a href="#setpeername"><tt>setpeername</tt></a>,
  462. <a href="#setsockname"><tt>setsockname</tt></a>, and
  463. <a href="#close"><tt>close</tt></a>.
  464. The <a href="#setpeername"><tt>setpeername</tt></a>
  465. is used to connect the object.
  466. </p>
  467. <p class="return">
  468. In case of success, a new unconnected UDP object
  469. returned. In case of error, <b><tt>nil</tt></b> is returned, followed by
  470. an error message.
  471. </p>
  472. <p class=note>
  473. Note: The TCP object returned will have the option
  474. "<tt>ipv6-v6only</tt>" set to <tt><b>true</b></tt>.
  475. </p>
  476. <!-- footer ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  477. <div class=footer>
  478. <hr>
  479. <center>
  480. <p class=bar>
  481. <a href="index.html">home</a> &middot;
  482. <a href="index.html#download">download</a> &middot;
  483. <a href="installation.html">installation</a> &middot;
  484. <a href="introduction.html">introduction</a> &middot;
  485. <a href="reference.html">reference</a>
  486. </p>
  487. <p>
  488. <small>
  489. Last modified by Diego Nehab on <br>
  490. Thu Apr 20 00:26:01 EDT 2006
  491. </small>
  492. </p>
  493. </center>
  494. </div>
  495. </body>
  496. </html>