Using the LUA extension Library luasocket use case

Source: Internet
Author: User

directory Structure Luasocket is Lua's Network module library, it can easily provide TCP, UDP, DNS, FTP, HTTP, SMTP, MIME and many other network protocol access operations. It consists of two parts: the core written in C, which provides access support for the TCP and UDP transport layers. The other part is written in Lua, which is responsible for the application function of the network interface processing. First, the installation of Luasocket two installation methods are described below: If you have the installation and Deployment Tools Luarocks installed with the Lua module, then a single instruction can be installed luasocket:# luarocks install Luasocket the second method: If you do not have Luarocks installed, you can also install the source code. First download the Luarocks, the currently available version is Luasocket-3.0-RC1 (Luasocket source has hosted in github.com): # git clone https://Github.com/diegonehab/luasocket.gitclone the source code after the local source can be installed, directly into the Luasocket directory to compile and install the # CD luasocket# make&&Make Installluasocket uses the following methods for using the Luasocket extension1, socket method request--socket method Request Local socket= Require ("Socket") Local Host="100.42.237.125"Local File="/"Local sock= Assert (Socket.connect (host, the)--Create a TCP connection, the standard for connecting to an HTTP connection theport on Sock:send ("GET".. File:"http/1.0\r\n\r\n") Repeat local chunk, status,Partial= Sock:receive (1024x768) --The data is received in 1K byte chunks, and the output of the byte block is received to--Print (chunk orPartial) until status~="closed"sock:close ()--To close a TCP connection2, HTTP access requests--HTTP Access Request HTTP=require ("socket.http") Result=http.request ("http://ip.taobao.com/service/getIpInfo.php?ip=123.189.1.100") print (result)3, the SMTP method sends mail--SMTP method sends maillocal SMTP= Require ("SOCKET.SMTP") from="<[email protected]>"--Sender--Send List Rcpt= {    "<[email protected]>",    "<[email protected]>"}mesgt={Headers={ to="[email protected]", --Recipient cc='<[email protected]>', --cc Subject="This is Mail Title"}, Body="This is Mail Content."}r, E=smtp.send{Server="smtp.126.com", the user="[email protected]", Password="******",     from= from, RCPT=rcpt, Source=smtp.message (MESGT)}ifNot R then print (e)ElsePrint ("Send ok!"end using Luasocket is simple, just load it directly with the Require function, in a few examples such as the following1) output A luasocket version information: local socket= Require ("Socket") print (socket._version)2access to the socket to get Baidu home data: Local socket= Require ("Socket") Local Host="www.baidu.com"Local File="/"--Create a TCP connection and connect to the standard port of the HTTP connection-- thePort on local sock= Assert (Socket.connect (host, the)) Sock:send ("GET".. File:"http/1.0\r\n\r\n") Repeat--The data is received in 1K bytes, and the output of the byte block is received to the local chunk, status,Partial= Sock:receive (1024x768) print (chunk orPartial) until status~="closed"--Close TCP Connection sock:close ()3) using the HTTP method built into the module to access: Local HTTP= Require ("socket.http") Local response= Http.request ("http://www.baidu.com/") Print (response) A simple client/The server communication connection originally wanted to be written as a single server multi-client socket chat server, but finally it was stuck on the client's data update, the single process whilePolling (poll), a io.read receives the server data for truncation. The existing Luasocket module is not loaded with other third-party modules, it is also difficult to do a real-time chat, although there are soket.SelectWas struggling to support it, but it was an uneven pit. may use the concurrency-oriented Concurrentlua module will solve this data reception blocking problem, this later to see, the result is: At the end of the client to knock something after the return to the server through the socket to send data, the server received data and then returned to the client terminal. A simple thing, purely practiced hand, the code is as follows: Server Side--server.lualocal Socket= Require ("Socket") Local Host="127.0.0.1"Local Port="12345"Local server= Assert (Socket.bind (host, Port,1024x768)) Server:settimeout (0) Local Client_tab={}local Conn_count=0Print ("Server Start".. Host:":".. port) while 1  DoLocal Conn=server:accept ()ifConn then Conn_count= Conn_count +1Client_tab[conn_count]=Conn Print ("A Client successfully connect!") End forConn_count, ClientinchPairs (Client_tab) Dolocal RECVT, Sendt, status= socket.Select({client}, nil,1)        if#recvt >0Then local receive, Receive_status=client:receive ()ifReceive_status ~="closed" Thenifreceive then assert (Client:send ("Client".. Conn_count."Send:") assert (Client:send (receive) ."\ n")) Print ("Receive Client".. Conn_count." : ", receive) EndElseTable.remove (Client_tab, Conn_count) client:close () print ("Client".. Conn_count."disconnect!") end end Endendclient End--client.lualocal Socket= Require ("Socket") Local Host="127.0.0.1"Local Port=12345Local sock=assert (Socket.connect (host, Port)) Sock:settimeout (0) Print ("press ENTER after input something:") Local input, RECVT, Sendt, status while true  Doinput=Io.read ()if#input >0Then assert (Sock:send (input:"\ n")) End recvt, Sendt, status= socket.Select({sock}, nil,1)     while#recvt >0  Dolocal response, Receive_status=sock:receive ()ifReceive_status ~="closed" ThenifResponse then Print (response) RECVT, Sendt, status= socket.Select({sock}, nil,1) EndElse             BreakEnd EndEnd reprint: D.H.Q's rotten pen

Using the LUA extension Library luasocket use case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.