实例解析lua中的socket(TCP,UDP)

首先,安装lua下的socket库。我在ubuntu下安装测试的。安装完成后会有 /usr/local/lib/lua/5.1/socket/core.so
直接上例子
TCP for lua:
Server:

  1 local socket = require("socket.core") /*require socket module*/
  2 local tcp = socket.tcp()
  3 local host = host or '127.0.0.1'
  4 local port = '8800'
  5 local tcpbind = tcp:bind(host,port)
  6 if(tcpbind and tcpbind == 1) then
  7     print('Binding to host'..host)
  8 else
  9     print('Binding error')
 10 end
 11 local e = tcp:listen(50)
 12 if(e and e == 1) then
 13     print('listen ok')
 14 else 
 15     print('listen error')
 16 end
 17 print('waiting connect cli')
 18 while 1 do
 19     local client = tcp:accept()
 20     if(client ~= nil) then
 21         print('access success')
 22     else
 23         print('accept err')
 24     end
 25     /*等待有至少10字节*/
 26     local revbuff = client:receive(10)
 27     print('revbuff:'..revbuff)  
 28     if(revbuff == 'hello 1111') then    
 29         local sendcli = client:send('hello lua cli')
 30         if(sendcli) then
 31             print('sendcli ok')
 32         else
 33             print('sendcli error')
 34         end
 35     else
 36         print('rec error , not send msg')
 37     end
 38    
 39 end
 40 tcp:close()

Client:

  1 local socket = require("socket.core")
  2 local tcp = socket.tcp()
  3 local host = host or '127.0.0.1'
  4 local port = '8800'
  5 local clicon = tcp:connect(host,port)
  6 tcp:settimeout(4) /*阻塞4s*/
  7 if(clicon) then
  8     print('connect '..host..'ok')
  9 else
 10     print('connect error')
 11 end
 12 function rec_msg()
 13     --local tcprev = tcp:receive(13)
 14     local tcprev = tcp:receive(13)
 15     if(tcprev) then
 16         print('tcprev:'..tcprev)
 17     else
 18         print('tcp rec err')
 19     end
 20 end
 21 
 22 while 1 do
 23     local tcpsend = tcp:send('hello 111111111')
 24     if(tcpsend) then
 25         print('tcpsend ok')
 26         rec_msg()
 27         --break
 28     else
 29         print('tcpsend err')
 30     end
 31 end
 32 tcp:close()

打印结果:
Server:

Binding to host127.0.0.1
listen ok
waiting connect cli
access success
revbuff:hello 1111
sendcli ok

Client:

connect 127.0.0.1ok
tcpsend ok
tcprev:hello lua cli
tcpsend ok
tcp rec err
tcpsend ok
tcp rec err
tcpsend ok
tcp rec err
tcpsend ok
tcp rec err
.
.
.

UDP for lua:
Server:

local socket = require("socket.core")
  2 local udp = socket.udp()
  3 local host = host or '127.0.0.1'
  4 local port = '8800'
  5 udp:settimeout(10)
  6 --local udpbind = udp:bind(host,port)
  7 udp:setsockname(host, port)
  8 print('waiting client connect')
  9 while 1 do
 10     local revbuff,receip,receport = udp:receivefrom()
 11     if (revbuff and receip and receport) then
 12         print('revbuff:'..revbuff..',receip:'..receip..',receport:'..receport)
 13         local sendcli = udp:sendto('hello lua cli',receip,receport)
 14         if(sendcli) then
 15             print('sendcli ok')
 16         else
 17             print('sendcli error')
 18         end
 19     else
 20         print('waiting client connect')
 21     end
 22 end
 23 udp:close()

Client:

  1 local socket = require("socket.core")
  2 local udp = socket.udp()
  3 local host = host or '127.0.0.1'
  4 local port = '8800'
  5 udp:settimeout(4)
  6 function rec_msg()
  7     local recudp = udp:receive()
  8     if(recudp) then
  9         print('recudp data:'..recudp)
 10     else
 11         print('recudp data nil')
 12     end
 13 end
 14 while 1 do
 15     udp:setpeername(host, port)
 16     local udpsend = udp:send('hello 111111111')
 17     if(udpsend) then
 18         print('udpsend ok')
 19         rec_msg()
 20         break
 21     else
 22         print('udpsend err')
 23     end
 24 end
 25 udp:close()

打印结果:
Server:

waiting client connect
revbuff:hello 111111111,receip:127.0.0.1,receport:39532
sendcli ok

Client:

udpsend ok
recudp data:hello lua cli

以上代码就是用lua完成的TCP和UDP通信,用lua脚本写的socket封装解析json数据很方便。欢迎大家尝试。

  • 4
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值