This article mainly introduces the use of Lua-nginx in openresty to create a socket instance, this article directly gives code examples and running effects, need friends can refer to the following
The LUA language is too powerful, at least that's how I feel. The original LUA does not have a socket function and requires the user to download the LUA socket component for a require. and the Lua-nginx module with the socket function, and is 100% non-blocking mode, thanks again to the author Zhang Yichun.
The use of the socket function is very simple, only a few simple methods can be mainly the difference between TCP and UDP. (This is just a LUA file, others see Hello World article)
The code is as follows:
Local sock = Ngx.socket.tcp ()
Local Ok,err = Sock:connect (' whois.cnnic.net.cn ', 43)
If not OK then
Ngx.say (' Failed to connect whois server ', err)
Return
End
Sock:settimeout (5000)
Local OK, err = Sock:send ("Baidu.cnrn")
If not OK then
Ngx.say (' Failed to send data to WHOIS server ', err)
Return
End
Local line, err, partial = sock:receive (' *a ')
If not line then
Ngx.say (' Failed to read a line ', err)
Return
End
Ngx.print (line)
Perfect operation: