Blame doc/udp.html

Packit b53373
Packit b53373
    "http://www.w3.org/TR/html4/strict.dtd">
Packit b53373
<html>
Packit b53373
Packit b53373
<head>
Packit b53373
<meta name="description" content="LuaSocket: The UDP support">
Packit b53373
<meta name="keywords" content="Lua, LuaSocket, Socket, UDP, Library, Network, Support"> 
Packit b53373
<title>LuaSocket: UDP support</title>
Packit b53373
<link rel="stylesheet" href="reference.css" type="text/css">
Packit b53373
</head>
Packit b53373
Packit b53373
<body>
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
<center>
Packit b53373
Packit b53373
Packit b53373
LuaSocket
Packit b53373
Packit b53373
Network support for the Lua language
Packit b53373
Packit b53373
Packit b53373

Packit b53373
home ·
Packit b53373
download ·
Packit b53373
installation ·
Packit b53373
introduction ·
Packit b53373
reference 
Packit b53373

Packit b53373
</center>
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

UDP

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
socket.udp()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns an unconnected IPv4 UDP object. 
Packit b53373
Unconnected objects support the 
Packit b53373
<tt>sendto</tt>, 
Packit b53373
<tt>receive</tt>, 
Packit b53373
<tt>receivefrom</tt>, 
Packit b53373
<tt>getoption</tt>, 
Packit b53373
<tt>getsockname</tt>, 
Packit b53373
<tt>setoption</tt>, 
Packit b53373
<tt>settimeout</tt>, 
Packit b53373
<tt>setpeername</tt>, 
Packit b53373
<tt>setsockname</tt>, and 
Packit b53373
<tt>close</tt>. 
Packit b53373
The <tt>setpeername</tt> 
Packit b53373
is used to connect the object.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
In case of success, a new unconnected UDP object
Packit b53373
returned. In case of error, <tt>nil</tt> is returned, followed by
Packit b53373
an error message.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
socket.udp6()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns an unconnected IPv6 UDP object. 
Packit b53373
Unconnected objects support the 
Packit b53373
<tt>sendto</tt>, 
Packit b53373
<tt>receive</tt>, 
Packit b53373
<tt>receivefrom</tt>, 
Packit b53373
<tt>getoption</tt>, 
Packit b53373
<tt>getsockname</tt>, 
Packit b53373
<tt>setoption</tt>, 
Packit b53373
<tt>settimeout</tt>, 
Packit b53373
<tt>setpeername</tt>, 
Packit b53373
<tt>setsockname</tt>, and 
Packit b53373
<tt>close</tt>. 
Packit b53373
The <tt>setpeername</tt> 
Packit b53373
is used to connect the object.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
In case of success, a new unconnected UDP object
Packit b53373
returned. In case of error, <tt>nil</tt> is returned, followed by
Packit b53373
an error message.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Note: The TCP object returned will have the option
Packit b53373
"<tt>ipv6-v6only</tt>" set to <tt>true</tt>.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
connected:close()
Packit b53373
unconnected:close()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Closes a UDP object. The internal socket
Packit b53373
used by the object is closed and the local address to which the
Packit b53373
object was bound is made available to other applications. No
Packit b53373
further operations (except for further calls to the <tt>close</tt>
Packit b53373
method) are allowed on a closed socket.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Note: It is important to close all used sockets
Packit b53373
once they are not needed, since, in many systems, each socket uses
Packit b53373
a file descriptor, which are limited system resources.
Packit b53373
Garbage-collected objects are automatically closed before
Packit b53373
destruction, though.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
connected:getpeername()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Retrieves information about the peer
Packit b53373
associated with a connected UDP object.
Packit b53373

Packit b53373
Packit b53373
Packit b53373

Packit b53373
Returns a string with the IP address of the peer, the 
Packit b53373
port number that peer is using for the connection, 
Packit b53373
and a string with the family ("<tt>inet</tt>" or "<tt>inet6</tt>"). 
Packit b53373
In case of error, the method returns <tt>nil</tt>. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Note: It makes no sense to call this method on unconnected objects.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
connected:getsockname()
Packit b53373
unconnected:getsockname()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Returns the local address information associated to the object.
Packit b53373

Packit b53373
Packit b53373
Packit b53373

Packit b53373
The method returns a string with local IP address, a number with 
Packit b53373
the local port, 
Packit b53373
and a string with the family ("<tt>inet</tt>" or "<tt>inet6</tt>"). 
Packit b53373
In case of error, the method returns <tt>nil</tt>.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Note: UDP sockets are not bound to any address
Packit b53373
until the <tt>setsockname</tt> or the
Packit b53373
<tt>sendto</tt> method is called for the
Packit b53373
first time (in which case it is bound to an ephemeral port and the
Packit b53373
wild-card address).
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
connected:receive([size])
Packit b53373
unconnected:receive([size])
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Receives a datagram from the UDP object. If
Packit b53373
the UDP object is connected, only datagrams coming from the peer
Packit b53373
are accepted. Otherwise, the returned datagram can come from any
Packit b53373
host.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The optional <tt>size</tt> parameter
Packit b53373
specifies the maximum size of the datagram to be retrieved. If
Packit b53373
there are more than <tt>size</tt> bytes available in the datagram,
Packit b53373
the excess bytes are discarded. If there are less then
Packit b53373
<tt>size</tt> bytes available in the current datagram, the
Packit b53373
available bytes are returned. If <tt>size</tt> is omitted, the
Packit b53373
maximum datagram size is used (which is currently limited by the
Packit b53373
implementation to 8192 bytes).
Packit b53373

Packit b53373
Packit b53373

Packit b53373
In case of success, the method returns the
Packit b53373
received datagram. In case of timeout, the method returns
Packit b53373
<tt>nil</tt> followed by the string '<tt>timeout</tt>'.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
unconnected:receivefrom([size])
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Works exactly as the <tt>receive</tt> 
Packit b53373
method, except it returns the IP
Packit b53373
address and port as extra return values (and is therefore slightly less
Packit b53373
efficient).
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
connected:getoption()
Packit b53373
unconnected:getoption()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Gets an option value from the UDP object.
Packit b53373
See <tt>setoption</tt> for
Packit b53373
description of the option names and values.
Packit b53373

Packit b53373
Packit b53373

<tt>Option</tt> is a string with the option name.

Packit b53373
    Packit b53373
  • '<tt>dontroute</tt>'
  • Packit b53373
  • '<tt>broadcast</tt>'
  • Packit b53373
  • '<tt>reuseaddr</tt>'
  • Packit b53373
  • '<tt>reuseport</tt>'
  • Packit b53373
  • '<tt>ip-multicast-loop</tt>'
  • Packit b53373
  • '<tt>ipv6-v6only</tt>'
  • Packit b53373
  • '<tt>ip-multicast-if</tt>'
  • Packit b53373
  • '<tt>ip-multicast-ttl</tt>'
  • Packit b53373
  • '<tt>ip-add-membership</tt>'
  • Packit b53373
  • '<tt>ip-drop-membership</tt>'
  • Packit b53373
     
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    The method returns the option <tt>value</tt> in case of
    Packit b53373
    success, or
    Packit b53373
    <tt>nil</tt> followed by an error message otherwise.
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    connected:send(datagram)
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Sends a datagram to the UDP peer of a connected object.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    <tt>Datagram</tt> is a string with the datagram contents. 
    Packit b53373
    The maximum datagram size for UDP is 64K minus IP layer overhead.
    Packit b53373
    However datagrams larger than the link layer packet size will be
    Packit b53373
    fragmented, which may deteriorate performance and/or reliability.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    If successful, the method returns 1. In case of
    Packit b53373
    error, the method returns <tt>nil</tt> followed by an error message.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: In UDP, the <tt>send</tt> method never blocks
    Packit b53373
    and the only way it can fail is if the underlying transport layer
    Packit b53373
    refuses to send a message to the specified address (i.e. no
    Packit b53373
    interface accepts the address).
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    unconnected:sendto(datagram, ip, port)
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Sends a datagram to the specified IP address and port number.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    <tt>Datagram</tt> is a string with the
    Packit b53373
    datagram contents. 
    Packit b53373
    The maximum datagram size for UDP is 64K minus IP layer overhead.
    Packit b53373
    However datagrams larger than the link layer packet size will be
    Packit b53373
    fragmented, which may deteriorate performance and/or reliability.
    Packit b53373
    <tt>Ip</tt> is the IP address of the recipient. 
    Packit b53373
    Host names are not allowed for performance reasons.
    Packit b53373
    Packit b53373
    <tt>Port</tt> is the port number at the recipient.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    If successful, the method returns 1. In case of
    Packit b53373
    error, the method returns <tt>nil</tt> followed by an error message.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: In UDP, the <tt>send</tt> method never blocks
    Packit b53373
    and the only way it can fail is if the underlying transport layer
    Packit b53373
    refuses to send a message to the specified address (i.e. no
    Packit b53373
    interface accepts the address).
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    connected:setpeername('*')
    Packit b53373
    unconnected:setpeername(address, port)
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Changes the peer of a UDP object. This
    Packit b53373
    method turns an unconnected UDP object into a connected UDP
    Packit b53373
    object or vice versa.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    For connected objects, outgoing datagrams
    Packit b53373
    will be sent to the specified peer, and datagrams received from
    Packit b53373
    other peers will be discarded by the OS. Connected UDP objects must
    Packit b53373
    use the <tt>send</tt> and 
    Packit b53373
    <tt>receive</tt> methods instead of 
    Packit b53373
    <tt>sendto</tt> and 
    Packit b53373
    <tt>receivefrom</tt>.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    <tt>Address</tt> can be an IP address or a
    Packit b53373
    host name. <tt>Port</tt> is the port number. If <tt>address</tt> is
    Packit b53373
    '<tt>*</tt>' and the object is connected, the peer association is
    Packit b53373
    removed and the object becomes an unconnected object again. In that
    Packit b53373
    case, the <tt>port</tt> argument is ignored.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    In case of error the method returns
    Packit b53373
    <tt>nil</tt> followed by an error message. In case of success, the
    Packit b53373
    method returns 1.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: Since the address of the peer does not have
    Packit b53373
    to be passed to and from the OS, the use of connected UDP objects
    Packit b53373
    is recommended when the same peer is used for several transmissions
    Packit b53373
    and can result in up to 30% performance gains.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: Starting with LuaSocket 3.0, the host name resolution
    Packit b53373
    depends on whether the socket was created by 
    Packit b53373
    href=#socket.udp><tt>socket.udp</tt> or 
    Packit b53373
    href=#socket.udp6><tt>socket.udp6</tt>. Addresses from
    Packit b53373
    the appropriate family are tried in succession until the
    Packit b53373
    first success or until the last failure.
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    unconnected:setsockname(address, port)
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Binds the UDP object to a local address.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    <tt>Address</tt> can be an IP address or a
    Packit b53373
    host name. If <tt>address</tt> is '<tt>*</tt>' the system binds to
    Packit b53373
    all local interfaces using the constant <tt>INADDR_ANY</tt>. If
    Packit b53373
    <tt>port</tt> is 0, the system chooses an ephemeral port.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    If successful, the method returns 1. In case of
    Packit b53373
    error, the method returns <tt>nil</tt> followed by an error
    Packit b53373
    message.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: This method can only be called before any
    Packit b53373
    datagram is sent through the UDP object, and only once. Otherwise,
    Packit b53373
    the system automatically binds the object to all local interfaces
    Packit b53373
    and chooses an ephemeral port as soon as the first datagram is
    Packit b53373
    sent. After the local address is set, either automatically by the
    Packit b53373
    system or explicitly by <tt>setsockname</tt>, it cannot be
    Packit b53373
    changed.
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    connected:setoption(option [, value])
    Packit b53373
    unconnected:setoption(option [, value])
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Sets options for the UDP object. Options are
    Packit b53373
    only needed by low-level or time-critical applications. You should
    Packit b53373
    only modify an option if you are sure you need it.

    Packit b53373

    <tt>Option</tt> is a string with the option

    Packit b53373
    name, and <tt>value</tt> depends on the option being set:
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
  • '<tt>dontroute</tt>': Indicates that outgoing
  • Packit b53373
    messages should bypass the standard routing facilities.
    Packit b53373
    Receives a boolean value;
    Packit b53373
  • '<tt>broadcast</tt>': Requests permission to send
  • Packit b53373
    broadcast datagrams on the socket.
    Packit b53373
    Receives a boolean value;
    Packit b53373
  • '<tt>reuseaddr</tt>': Indicates that the rules used in
  • Packit b53373
    validating addresses supplied in a <tt>bind()</tt> call 
    Packit b53373
    should allow reuse of local addresses. 
    Packit b53373
    Receives a boolean value;
    Packit b53373
  • '<tt>reuseport</tt>': Allows completely duplicate
  • Packit b53373
    bindings by multiple processes if they all set
    Packit b53373
    '<tt>reuseport</tt>' before binding the port.
    Packit b53373
    Receives a boolean value;
    Packit b53373
  • '<tt>ip-multicast-loop</tt>':
  • Packit b53373
    Specifies whether or not a copy of an outgoing multicast
    Packit b53373
    datagram is delivered to the sending host as long as it is a
    Packit b53373
    member of the multicast group.
    Packit b53373
    Receives a boolean value;
    Packit b53373
  • '<tt>ipv6-v6only</tt>':
  • Packit b53373
    Specifies whether to restrict <tt>inet6</tt> sockets to 
    Packit b53373
    sending and receiving only IPv6 packets.
    Packit b53373
    Receive a boolean value;
    Packit b53373
  • '<tt>ip-multicast-if</tt>':
  • Packit b53373
    Sets the interface over which outgoing multicast datagrams
    Packit b53373
    are sent.
    Packit b53373
    Receives an IP address;
    Packit b53373
  • '<tt>ip-multicast-ttl</tt>':
  • Packit b53373
    Sets the Time To Live in the IP header for outgoing
    Packit b53373
    multicast datagrams. 
    Packit b53373
    Receives a number;
    Packit b53373
  • '<tt>ip-add-membership</tt>':
  • Packit b53373
    Joins the multicast group specified.
    Packit b53373
    Receives a table with fields
    Packit b53373
    <tt>multiaddr</tt> and <tt>interface</tt>, each containing an
    Packit b53373
    IP address;
    Packit b53373
  • '<tt>ip-drop-membership</tt>': Leaves the multicast
  • Packit b53373
    group specified.
    Packit b53373
    Receives a table with fields
    Packit b53373
    <tt>multiaddr</tt> and <tt>interface</tt>, each containing an
    Packit b53373
    IP address.
    Packit b53373
     
    Packit b53373
    Packit b53373

    Packit b53373
    The method returns 1 in case of success, or
    Packit b53373
    <tt>nil</tt> followed by an error message otherwise.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: The descriptions above come from the man pages.
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    connected:settimeout(value)
    Packit b53373
    unconnected:settimeout(value)
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Changes the timeout values for the object.  By default, the 
    Packit b53373
    <tt>receive</tt> and 
    Packit b53373
    <tt>receivefrom</tt> 
    Packit b53373
    operations are blocking. That is, any call to the methods will block
    Packit b53373
    indefinitely, until data arrives.  The <tt>settimeout</tt> function defines
    Packit b53373
    a limit on the amount of time the functions can block. When a timeout is
    Packit b53373
    set and the specified amount of time has elapsed, the affected methods
    Packit b53373
    give up and fail with an error code.  
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    The amount of time to wait is specified as
    Packit b53373
    the <tt>value</tt> parameter, in seconds. The <tt>nil</tt> timeout
    Packit b53373
    <tt>value</tt> allows operations to block indefinitely. Negative
    Packit b53373
    timeout values have the same effect.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: In UDP, the <tt>send</tt>
    Packit b53373
    and <tt>sendto</tt> methods never block (the
    Packit b53373
    datagram is just passed to the OS and the call returns
    Packit b53373
    immediately). Therefore, the <tt>settimeout</tt> method has no
    Packit b53373
    effect on them.
    Packit b53373

    Packit b53373
    Packit b53373

    Packit b53373
    Note: The old <tt>timeout</tt> method is
    Packit b53373
    deprecated. The name has been changed for sake of uniformity, since
    Packit b53373
    all other method names already contained verbs making their
    Packit b53373
    imperative nature obvious.
    Packit b53373

    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373
    Packit b53373

    Packit b53373
    <center>
    Packit b53373

    Packit b53373
    home ·
    Packit b53373
    download ·
    Packit b53373
    installation ·
    Packit b53373
    introduction ·
    Packit b53373
    reference 
    Packit b53373

    Packit b53373

    Packit b53373
    <small>
    Packit b53373
    Last modified by Diego Nehab on 
    Packit b53373
    Thu Apr 20 00:26:01 EDT 2006
    Packit b53373
    </small>
    Packit b53373

    Packit b53373
    </center>
    Packit b53373
    Packit b53373
    Packit b53373
    </body>
    Packit b53373
    </html>