openresty中使用lua-nginx創(chuàng)建socket實(shí)例
來源:易賢網(wǎng) 閱讀:3862 次 日期:2015-04-24 10:37:35
溫馨提示:易賢網(wǎng)小編為您整理了“openresty中使用lua-nginx創(chuàng)建socket實(shí)例”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了openresty中使用lua-nginx創(chuàng)建socket實(shí)例,本文直接給出代碼實(shí)例和運(yùn)行效果,需要的朋友可以參考下

Lua語言太強(qiáng)大了,至少我是這樣覺得的。原始的Lua沒有Socket功能,需要使用者下載Lua socket組件,require一下才行。而lua-nginx模塊自帶了socket功能,而且是100%的非阻塞模式,再次感謝作者章亦春。

使用socket功能很簡單,只有幾個(gè)簡單的方法即可主要就是有TCP和UDP的區(qū)別。(這里只是lua文件,其他請見Hello world 文章)

代碼如下:

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)

完美運(yùn)行:

名單

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:openresty中使用lua-nginx創(chuàng)建socket實(shí)例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn)

版權(quán)所有:易賢網(wǎng)