UDP
socket.udp()
创建并返回一个无连接的UDP对象。该无连接对象支持以下函数:sendto、receive、receivefrom、getsockname、setoption、settimeout、setpeername、setsockname、close。其中的setpeername被用作连接对象(待解释)。
connected:close()
unconnected:close()
关闭一个UDP对象。对象使用的内部套接字被关闭,并且对象绑定的本地地址可被外部程序使用。在套接字被关闭之后,对象不再具有可用的方法。
注意:一旦套接字不再使用,就应该主动地关闭它们,并且这很重要。因为在许多系统中,每个套接字都使用一个文件句柄,而这个文件句柄都是系统的有限资源。
connected:getpeername()
获取已连接的UDP对象的信息。
返回已连接UDP对象的IP地址与端口号。
注意:不能对无连接对象使用该方法。
connected:getsockname()
unconnected:getsockname()
返回对象绑定的本地地址信息。
该方法返回一个本地IP字符串和用数字描述的端口号。若发生错误则返回nil。
注意:UDP套接字在UDP对象使用setsockname或sendto方法之前不会绑定任何地址(而在这种情况下,它将绑定一个临时的端口和0.0.0.0地址)
connected:receive([size])
unconnected:receive([size])
从UDP对象收取数据包。如果UDP对象为已连接对象,则只收取其来自其连接对象的数据包,都收取的数据包可来自任何主机。
可选参数size用于指定收取数据包的最大长度,若数据报的长度大于指定的最大收取长度,则数据报中额外的字节将被丢弃。若数据包的长度小于指定的最大收取长度,则所有的字节均被获取。若size参数缺省,则使用默认的数据包大小(目前该默认大小为8192字节)。
若receive方法执行成功,则返回收取的数据报(字符串表示)。若超时,则返回nil和"timeout"字符串。
unconnected:receivefrom([size])
与receive方法的基本相同,不同的是它还返回额外的数据报来源对象绑定的IP和端口号(因此与receive相比该方法略显低效)
connected:send(datagram)
发送数据报给已连接的UDP对象。
datagram为字符串描述的数据报文。UDP连接的最大的数据报文长度为64K减去IP报头长度。若报文的长度大于链路层允许的数据包大小,则报文会被分段发送,这会降低数据传递的效率与可靠性。
若发送成功,该方法返回1。若发送失败,则返回nil和错误描述信息。
注意:在UDP通信中,可能导致send方法失败的唯一原因是传输层拒绝发送数据到特定的IP地址(若那些没有任何硬件接口接受的IP地址)。
unconnected:sendto(datagram,ip, port)
发送数据报到指定的IP和端口上。
译注:与send不同之处仅在于该方法允许你指定接受者的IP和端口,参考send方法。
connected:setpeername('*')
unconnected:setpeername(address,port)
改变UDP对象的连接状态。该方法将一个已连接的UDP对象转换为未连接对象,反之亦可将一个未连接对象转换为已连接对象。
对于已连接的UDP对象,它发出的数据报仍被送往指定的客户端,而其从其它客户端获取的数据报将被操作系统丢弃。这一准则的前提是UDP对象使用了send和receive方法而非sendto和receivefrom。
address参数可以是IP地址也可以是主机名。port参数为端口号。若address参数为"*"并且UDP对象已建立连接,则UDP对象连接的客户端会被删除且该UDP对象被重置为未连接的UDP对象。在这一情形下,port参数被忽略。
若setpeername方法执行失败,则返回nil和错误描述信息。若执行成功,则返回1。
注意:在UDP建立连接后,便无需重复的向操作系统交换IP地址信息。因此,在想同一客户端多次传送数据时,建议使用已连接的UDP对象,这样可以获得30%的性能提升。
unconnected:setsockname(address,port)
将UDP对象绑定到本地地址。
address参数可以是IP地址或主机名。若address参数为"*",则绑定到所有本地接口。若port参数为0,则使用一个临时的端口号。
若执行成功,则返回1。若失败,则返回nil和错误描述信息。
注意:该方法只可在数据报发送之前有UDP对象调用一次。若不显式的在发送数据报之前调用此函数,则系统在发送首个数据报的同时会自动的将UDP对象绑定到所有本地接口并使用一个临时的端口号。
connected:setoption(option[, value])
unconnected:setoption(option[, value])
为UDP对象设置选项。只有低级别或时序要求严格的的程序才会需要设置选项。只有在明确需要设置选项时才应该使用此方法。
option参数为一个字符串表示的选项名,value由option参数决定:
·"dontroute":将此选项的value值设置为true表明发送的消息应绕过标准路由设备。
·"broadcast":将此选项的value值设置为true表明要求在套接字上发送的广播数据报 的权限。
若执行成功,则返回1。若失败,则返回nil和错误描述信息。
connect:settimteout(value)
unconnected:settimeout(value)
改变对象的超时值。默认情形下,receive和receivefrom方法都是阻塞的。也就是说,该方法在收到数据前将会一直阻塞。settimeout方法使用value值设置了一个最大的阻塞时间。若该时间达到时,receive或receivefrom方法仍未收到数据,则该方法执行失败并返回nil。
最大超时时间由value参数设定,单位为秒。若value为nil或负数,则阻塞时间不定(阻塞到数据到达)。
注意:在UDP通信中,send和sendto方法从不阻塞。因此settimeout方法对这两个方法无效果。
socket.udp()
Creates and returns an unconnected UDP object. Unconnected objects support the sendto, receive, receivefrom,getsockname, setoption, settimeout, setpeername, setsockname, and close. The setpeername is used to connect the object.
In case of success, a new unconnected UDP object returned. In case of error, nil is returned, followed by an error message.
connected:close()
unconnected:close()
Closes a UDP object. The internal socket used by the object is closed and the local address to which the object was bound is made available to other applications. No further operations (except for further calls to the closemethod) are allowed on a closed socket.
Note: It is important to close all used sockets once they are not needed, since, in many systems, each socket uses a file descriptor, which are limited system resources. Garbage-collected objects are automatically closed before destruction, though.
connected:getpeername()
Retrieves information about the peer associated with a connected UDP object.
Returns the IP address and port number of the peer.
Note: It makes no sense to call this method on unconnected objects.
connected:getsockname()
unconnected:getsockname()
Returns the local address information associated to the object.
The method returns a string with local IP address and a number with the port. In case of error, the method returnsnil.
Note: UDP sockets are not bound to any address until the setsockname or the sendto method is called for the first time (in which case it is bound to an ephemeral port and the wild-card address).
connected:receive([size])
unconnected:receive([size])
Receives a datagram from the UDP object. If the UDP object is connected, only datagrams coming from the peer are accepted. Otherwise, the returned datagram can come from any host.
The optional size parameter specifies the maximum size of the datagram to be retrieved. If there are more thansize bytes available in the datagram, the excess bytes are discarded. If there are less then size bytes available in the current datagram, the available bytes are returned. If size is omitted, the maximum datagram size is used (which is currently limited by the implementation to 8192 bytes).
In case of success, the method returns the received datagram. In case of timeout, the method returns nil followed by the string 'timeout'.
unconnected:receivefrom([size])
Works exactly as the receive method, except it returns the IP address and port as extra return values (and is therefore slightly less efficient).
connected:send(datagram)
Sends a datagram to the UDP peer of a connected object.
Datagram is a string with the datagram contents. The maximum datagram size for UDP is 64K minus IP layer overhead. However datagrams larger than the link layer packet size will be fragmented, which may deteriorate performance and/or reliability.
If successful, the method returns 1. In case of error, the method returns nil followed by an error message.
Note: In UDP, the send method never blocks and the only way it can fail is if the underlying transport layer refuses to send a message to the specified address (i.e. no interface accepts the address).
unconnected:sendto(datagram, ip, port)
Sends a datagram to the specified IP address and port number.
Datagram is a string with the datagram contents. The maximum datagram size for UDP is 64K minus IP layer overhead. However datagrams larger than the link layer packet size will be fragmented, which may deteriorate performance and/or reliability. Ip is the IP address of the recipient. Host names are not allowed for performance reasons. Port is the port number at the recipient.
If successful, the method returns 1. In case of error, the method returns nil followed by an error message.
Note: In UDP, the send method never blocks and the only way it can fail is if the underlying transport layer refuses to send a message to the specified address (i.e. no interface accepts the address).
connected:setpeername('*')
unconnected:setpeername(address, port)
Changes the peer of a UDP object. This method turns an unconnected UDP object into a connected UDP object or vice versa.
For connected objects, outgoing datagrams will be sent to the specified peer, and datagrams received from other peers will be discarded by the OS. Connected UDP objects must use the send and receive methods instead ofsendto and receivefrom.
Address can be an IP address or a host name. Port is the port number. If address is '*' and the object is connected, the peer association is removed and the object becomes an unconnected object again. In that case, theport argument is ignored.
In case of error the method returns nil followed by an error message. In case of success, the method returns 1.
Note: Since the address of the peer does not have to be passed to and from the OS, the use of connected UDP objects is recommended when the same peer is used for several transmissions and can result in up to 30% performance gains.
unconnected:setsockname(address, port)
Binds the UDP object to a local address.
Address can be an IP address or a host name. If address is '*' the system binds to all local interfaces using the constant INADDR_ANY. If port is 0, the system chooses an ephemeral port.
If successful, the method returns 1. In case of error, the method returns nil followed by an error message.
Note: This method can only be called before any datagram is sent through the UDP object, and only once. Otherwise, the system automatically binds the object to all local interfaces and chooses an ephemeral port as soon as the first datagram is sent. After the local address is set, either automatically by the system or explicitly bysetsockname, it cannot be changed.
connected:setoption(option [, value])
unconnected:setoption(option [, value])
Sets options for the UDP object. Options are only needed by low-level or time-critical applications. You should only modify an option if you are sure you need it.
Option is a string with the option name, and value depends on the option being set:
The method returns 1 in case of success, or nil followed by an error message otherwise.
Note: The descriptions above come from the man pages.
connected:settimeout(value)
unconnected:settimeout(value)
Changes the timeout values for the object. By default, the receive and receivefrom operations are blocking. That is, any call to the methods will block indefinitely, until data arrives. The settimeout function defines a limit on the amount of time the functions can block. When a timeout is set and the specified amount of time has elapsed, the affected methods give up and fail with an error code.
The amount of time to wait is specified as the value parameter, in seconds. The nil timeout value allows operations to block indefinitely. Negative timeout values have the same effect.
Note: In UDP, the send and sendto methods never block (the datagram is just passed to the OS and the call returns immediately). Therefore, the settimeout method has no effect on them.
Note: The old timeout method is deprecated. The name has been changed for sake of uniformity, since all other method names already contained verbs making their imperative nature obvious.