-
Notifications
You must be signed in to change notification settings - Fork 65
/
4 net_udpclient_demo.lua
34 lines (30 loc) · 1.03 KB
/
4 net_udpclient_demo.lua
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
--net demo
print("------net demo------")
--setup a wifi interface first
ap_ssid="Doit" --change to your ap ssid
ap_pwd="123456789" --change to your ap pwd
cfg={ssid = ap_ssid,pwd = ap_pwd}
wifi.startsta(cfg)
cfg=nil
ap_pwd=nil
--check if connect to ap
stat,_,_,_ = wifi.sta.getlink()
while stat ~= 'connected' do
print("Wating for connecting to AP:"..ap_ssid)
tmr.delayms(1000)
stat,_,_,_ = wifi.sta.getlink()
end
print("WiFiMCU connected to ap successful")
print("udp client started")
skt = net.new(net.UDP,net.CLIENT)
net.on(skt,"dnsfound",function(skt,ip) print("dnsfound: skt:"..skt.." ip:"..ip) end)
net.on(skt,"sent",function(skt) print("sent:skt:"..skt) end)
net.on(skt,"disconnect",function(skt) print("disconnect:skt:"..skt) end)
net.on(skt,"receive",function(skt,d)
print("Receive:"..d)
net.send(skt,'From Client:'..d.."\n")
end)
--connect to server(ip:192.168.1.105) at port 8001
--net.start(skt,8001,"192.168.1.105")
--connect to server(ip:192.168.1.105) at port 9001 with local port 8989
net.start(skt,8001,"192.168.1.105",8001)