实例解析lua中的socket(TCP,UDP)
首先,安装lua下的socket库。我在ubuntu下安装测试的。安装完成后会有 /usr/local/lib/lua/5.1/socket/core.so
直接上例子
TCP for lua:
Server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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:
1
2
3
4
5
6
|
Binding to host127.0.0.1
listen ok
waiting connect cli
access success
revbuff:hello 1111
sendcli ok
|
Client:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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:
1
2
3
|
waiting client connect
revbuff:hello 111111111,receip:127.0.0.1,receport:39532
sendcli ok
|
Client:
1
2
|
udpsend ok
recudp data:hello lua cli
|
以上代码就是用lua完成的TCP和UDP通信,用lua脚本写的socket封装解析json数据很方便。欢迎大家尝试。