�ɲɾ�����ӯ�����һ��ˣ��������С���˴��ͣ�������P���ҹ��ñ˽��ά�Բ��������˸߸ԣ�������ơ��ҹ��ñ�����ά�Բ���ˡ���˳^�ӣ������ӡ� ���ͯj�ӣ��ƺ���ӣ� ? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!PK ;1]ξf f dns.htmlnu [
IPv4 name resolution functions dns.toip and dns.tohostname return all information obtained from the resolver in a table of the form:
resolved4 = {
name = canonic-name,
alias = alias-list,
ip = ip-address-list
}
Note that the alias list can be empty.
The more general name resolution function dns.getaddrinfo, which supports both IPv6 and IPv4, returns all information obtained from the resolver in a table of the form:
resolved6 = {
[1] = {
family = family-name-1,
addr = address-1
},
...
[n] = {
family = family-name-n,
addr = address-n
}
}
Here, family contains the string "inet" for IPv4 addresses, and "inet6" for IPv6 addresses.
socket.dns.getaddrinfo(address)
Converts from host name to address.
Address can be an IPv4 or IPv6 address or host name.
The function returns a table with all information returned by the resolver. In case of error, the function returns nil followed by an error message.
socket.dns.gethostname()
Returns the standard host name for the machine as a string.
socket.dns.tohostname(address)
Converts from IPv4 address to host name.
Address can be an IP address or host name.
The function returns a string with the canonic host name of the given address, followed by a table with all information returned by the resolver. In case of error, the function returns nil followed by an error message.
socket.dns.toip(address)
Converts from host name to IPv4 address.
Address can be an IP address or host name.
Returns a string with the first IP address found for address, followed by a table with all information returned by the resolver. In case of error, the function returns nil followed by an error message.
PK ;1]>- index.htmlnu [LuaSocket is a Lua extension library that is composed by two parts: a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for functionality commonly needed by applications that deal with the Internet.
The core support has been implemented so that it is both efficient and simple to use. It is available to any Lua application once it has been properly initialized by the interpreter in use. The code has been tested and runs well on several Windows and UNIX platforms.
Among the support modules, the most commonly used implement the SMTP (sending e-mails), HTTP (WWW access) and FTP (uploading and downloading files) client protocols. These provide a very natural and generic interface to the functionality defined by each protocol. In addition, you will find that the MIME (common encodings), URL (anything you could possible want to do with one) and LTN12 (filters, sinks, sources and pumps) modules can be very handy.
The library is available under the same terms and conditions as the Lua language, the MIT license. The idea is that if you can use Lua in a project, you should also be able to use LuaSocket.
Copyright © 1999-2013 Diego Nehab. All rights reserved.
Author: Diego Nehab
LuaSocket version 3.0-rc1 is now available for download! It is compatible with Lua 5.1 and 5.2, and has been tested on Windows XP, Linux, and Mac OS X. Chances are it works well on most UNIX distributions and Windows flavors.
The current version of the library can be found at the LuaSocket project page on GitHub. Besides the full C and Lua source code for the library, the distribution contains several examples, this user's manual and basic test procedures.
Take a look at the installation section of the manual to find out how to properly install the library.
This marks the first release of LuaSocket that wholeheartedly embraces the open-source development philosophy. After a long hiatus, Matthew Wild finally convinced me it was time for a release including IPv6 and Lua 5.2 support. It was more work than we anticipated. Special thanks to Sam Roberts, Florian Zeitz, and Paul Aurich, Liam Devine, Alexey Melnichuk, and everybody else that has helped bring this library back to life.
Main changes for LuaSocket 3.0-rc1 are IPv6 support and Lua 5.2 compatibility.
All previous versions of the LuaSocket library can be downloaded here. Although these versions are no longer supported, they are still available for those that have compatibility issues.
PK ;1]b]% ]% ftp.htmlnu [FTP (File Transfer Protocol) is a protocol used to transfer files between hosts. The ftp namespace offers thorough support to FTP, under a simple interface. The implementation conforms to RFC 959.
High level functions are provided supporting the most common operations. These high level functions are implemented on top of a lower level interface. Using the low-level interface, users can easily create their own functions to access any operation supported by the FTP protocol. For that, check the implementation.
To really benefit from this module, a good understanding of LTN012, Filters sources and sinks is necessary.
To obtain the ftp namespace, run:
-- loads the FTP module and any libraries it requires
local ftp = require("socket.ftp")
URLs MUST conform to RFC 1738, that is, an URL is a string in the form:
[ftp://][<user>[:<password>]@]<host>[:<port>][/<path>][type=a|i]
The following constants in the namespace can be set to control the default behavior of the FTP module:
ftp.get(url)
ftp.get{
host = string,
sink = LTN12 sink,
argument or path = string,
[user = string,]
[password = string]
[command = string,]
[port = number,]
[type = string,]
[step = LTN12 pump step,]
[create = function]
}
The get function has two forms. The simple form has fixed functionality: it downloads the contents of a URL and returns it as a string. The generic form allows a lot more control, as explained below.
If the argument of the get function is a table, the function expects at least the fields host, sink, and one of argument or path (argument takes precedence). Host is the server to connect to. Sink is the simple LTN12 sink that will receive the downloaded data. Argument or path give the target path to the resource in the server. The optional arguments are the following:
If successful, the simple version returns the URL contents as a string, and the generic function returns 1. In case of error, both functions return nil and an error message describing the error.
-- load the ftp support
local ftp = require("socket.ftp")
-- Log as user "anonymous" on server "ftp.tecgraf.puc-rio.br",
-- and get file "lua.tar.gz" from directory "pub/lua" as binary.
f, e = ftp.get("ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz;type=i")
-- load needed modules
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
local url = require("socket.url")
-- a function that returns a directory listing
function nlst(u)
local t = {}
local p = url.parse(u)
p.command = "nlst"
p.sink = ltn12.sink.table(t)
local r, e = ftp.get(p)
return r and table.concat(t), e
end
ftp.put(url, content)
ftp.put{
host = string,
source = LTN12 sink,
argument or path = string,
[user = string,]
[password = string]
[command = string,]
[port = number,]
[type = string,]
[step = LTN12 pump step,]
[create = function]
}
The put function has two forms. The simple form has fixed functionality: it uploads a string of content into a URL. The generic form allows a lot more control, as explained below.
If the argument of the put function is a table, the function expects at least the fields host, source, and one of argument or path (argument takes precedence). Host is the server to connect to. Source is the simple LTN12 source that will provide the contents to be uploaded. Argument or path give the target path to the resource in the server. The optional arguments are the following:
Both functions return 1 if successful, or nil and an error message describing the reason for failure.
-- load the ftp support
local ftp = require("socket.ftp")
-- Log as user "fulano" on server "ftp.example.com",
-- using password "silva", and store a file "README" with contents
-- "wrong password, of course"
f, e = ftp.put("ftp://fulano:silva@ftp.example.com/README",
"wrong password, of course")
-- load the ftp support
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
-- Log as user "fulano" on server "ftp.example.com",
-- using password "silva", and append to the remote file "LOG", sending the
-- contents of the local file "LOCAL-LOG"
f, e = ftp.put{
host = "ftp.example.com",
user = "fulano",
password = "silva",
command = "appe",
argument = "LOG",
source = ltn12.source.file(io.open("LOCAL-LOG", "r"))
}
PK ;1]I0 0 introduction.htmlnu [
LuaSocket is a Lua extension library that is composed by two parts: a C core that provides support for the TCP and UDP transport layers, and a set of Lua modules that add support for the SMTP (sending e-mails), HTTP (WWW access) and FTP (uploading and downloading files) protocols and other functionality commonly needed by applications that deal with the Internet. This introduction is about the C core.
Communication in LuaSocket is performed via I/O objects. These can represent different network domains. Currently, support is provided for TCP and UDP, but nothing prevents other developers from implementing SSL, Local Domain, Pipes, File Descriptors etc. I/O objects provide a standard interface to I/O across different domains and operating systems.
The API design had two goals in mind. First, users experienced with the C API to sockets should feel comfortable using LuaSocket. Second, the simplicity and the feel of the Lua language should be preserved. To achieve these goals, the LuaSocket API keeps the function names and semantics the C API whenever possible, but their usage in Lua has been greatly simplified.
One of the simplifications is the receive pattern capability. Applications can read data from stream domains (such as TCP) line by line, block by block, or until the connection is closed. All I/O reads are buffered and the performance differences between different receive patterns are negligible.
Another advantage is the flexible timeout control mechanism. As in C, all I/O operations are blocking by default. For example, the send, receive and accept methods of the TCP domain will block the caller application until the operation is completed (if ever!). However, with a call to the settimeout method, an application can specify upper limits on the time it can be blocked by LuaSocket (the "total" timeout), on the time LuaSocket can internally be blocked by any OS call (the "block" timeout) or a combination of the two. Each LuaSocket call might perform several OS calls, so that the two timeout values are not equivalent.
Finally, the host name resolution is transparent, meaning that most functions and methods accept both IP addresses and host names. In case a host name is given, the library queries the system's resolver and tries the main IP address returned. Note that direct use of IP addresses is more efficient, of course. The toip and tohostname functions from the DNS module are provided to convert between host names and IP addresses.
Together, these changes make network programming in LuaSocket much simpler than it is in C, as the following sections will show.
TCP (Transfer Control Protocol) is reliable stream protocol. In other words, applications communicating through TCP can send and receive data as an error free stream of bytes. Data is split in one end and reassembled transparently on the other end. There are no boundaries in the data transfers. The library allows users to read data from the sockets in several different granularities: patterns are available for lines, arbitrary sized blocks or "read up to connection closed", all with good performance.
The library distinguishes three types of TCP sockets: master, client and server sockets.
Master sockets are newly created TCP sockets returned by the function socket.tcp. A master socket is transformed into a server socket after it is associated with a local address by a call to the bind method followed by a call to the listen. Conversely, a master socket can be changed into a client socket with the method connect, which associates it with a remote address.
On server sockets, applications can use the accept method to wait for a client connection. Once a connection is established, a client socket object is returned representing this connection. The other methods available for server socket objects are getsockname, setoption, settimeout, and close.
Client sockets are used to exchange data between two applications over the Internet. Applications can call the methods send and receive to send and receive data. The other methods available for client socket objects are getsockname, getpeername, setoption, settimeout, shutdown, and close.
Example:
A simple echo server, using LuaSocket. The program binds to an ephemeral port (one that is chosen by the operating system) on the local host and awaits client connections on that port. When a connection is established, the program reads a line from the remote end and sends it back, closing the connection immediately. You can test it using the telnet program.
-- load namespace local socket = require("socket") -- create a TCP socket and bind it to the local host, at any port local server = assert(socket.bind("*", 0)) -- find out which port the OS chose for us local ip, port = server:getsockname() -- print a message informing what's up print("Please telnet to localhost on port " .. port) print("After connecting, you have 10s to enter a line to be echoed") -- loop forever waiting for clients while 1 do -- wait for a connection from any client local client = server:accept() -- make sure we don't block waiting for this client's line client:settimeout(10) -- receive the line local line, err = client:receive() -- if there was no error, send it back to the client if not err then client:send(line .. "\n") end -- done with client, close the object client:close() end
UDP (User Datagram Protocol) is a non-reliable datagram protocol. In other words, applications communicating through UDP send and receive data as independent blocks, which are not guaranteed to reach the other end. Even when they do reach the other end, they are not guaranteed to be error free. Data transfers are atomic, one datagram at a time. Reading only part of a datagram discards the rest, so that the following read operation will act on the next datagram. The advantages are in simplicity (no connection setup) and performance (no error checking or error correction).
Note that although no guarantees are made, these days networks are so good that, under normal circumstances, few errors happen in practice.
An UDP socket object is created by the socket.udp function. UDP sockets do not need to be connected before use. The method sendto can be used immediately after creation to send a datagram to IP address and port. Host names are not allowed because performing name resolution for each packet would be forbiddingly slow. Methods receive and receivefrom can be used to retrieve datagrams, the latter returning the IP and port of the sender as extra return values (thus being slightly less efficient).
When communication is performed repeatedly with a single peer, an application should call the setpeername method to specify a permanent partner. Methods sendto and receivefrom can no longer be used, but the method send can be used to send data directly to the peer, and the method receive will only return datagrams originating from that peer. There is about 30% performance gain due to this practice.
To associate an UDP socket with a local address, an application calls the setsockname method before sending any datagrams. Otherwise, the socket is automatically bound to an ephemeral address before the first data transmission and once bound the local address cannot be changed. The other methods available for UDP sockets are getpeername, getsockname, settimeout, setoption and close.
Example:
A simple daytime client, using LuaSocket. The program connects to a remote server and tries to retrieve the daytime, printing the answer it got or an error message.
-- change here to the host an port you want to contact local host, port = "localhost", 13 -- load namespace local socket = require("socket") -- convert host name to ip address local ip = assert(socket.dns.toip(host)) -- create a new UDP object local udp = assert(socket.udp()) -- contact daytime host assert(udp:sendto("anything", ip, port)) -- retrieve the answer and print results io.write(assert(udp:receive()))
Although not covered in the introduction, LuaSocket offers much more than TCP and UDP functionality. As the library evolved, support for HTTP, FTP, and SMTP were built on top of these. These modules and many others are covered by the reference manual.
PK ;1]ߝ reference.htmlnu [DNS (in socket)getaddrinfo, gethostname, tohostname, toip.
FTPget, put.
HTTPrequest.
LTN12filter: chain, cycle.pump: all, step.sink: chain, error, file, null, simplify, table.source: cat, chain, empty, error, file, simplify, string.
MIMEhigh-level: decode, encode, normalize, stuff, wrap.low-level: b64, dot, eol, qp, qpwrp, unb64, unqp, wrp.
SMTPmessage, send.
Socketbind, connect, connect4, connect6, _DEBUG, dns, gettime, headers.canonic, newtry, protect, select, sink, skip, sleep, _SETSIZE, source, tcp, tcp6, try, udp, udp6, _VERSION.
TCP (in socket)accept, bind, close, connect, dirty, getfd, getoption, getpeername, getsockname, getstats, listen, receive, send, setfd, setoption, setstats, settimeout, shutdown.
UDP (in socket)close, getoption, getpeername, getsockname, receive, receivefrom, send, sendto, setpeername, setsockname, setoption, settimeout.
URLPK ;1]l- - luasocket.pngnu [ PNG IHDR L\ -SIDATx}ytTEkFJB Q>uqA~:Χ~93Ό0Dp ?$ Q%İ$$BHBJI,^UѝtBH{t^Wު{oݭ~~$@?Ïg ?G3H~;Cp߾ S= Fqw{cO!탽%&absolute, build, build_path, escape, parse, parse_path, unescape.