server_demo.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. local libnet = require "libnet"
  2. --下面演示用阻塞方式做自动应答服务器,只适合W5500
  3. local dName = "D2_TASK"
  4. local d1Name = "D1_TASK"
  5. local function netCB(msg)
  6. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  7. end
  8. local function serTask(port)
  9. local tx_buff = zbuff.create(1024)
  10. local rx_buff = zbuff.create(1024)
  11. local netc
  12. local result, param, succ, rIP, rPort, cnt
  13. netc = socket.create(nil, dName)
  14. socket.debug(netc, true)
  15. socket.config(netc, port)
  16. result = libnet.waitLink(dName, 0, netc)
  17. ip, mask, gw, ipv6 = socket.localIP()
  18. if not ipv6 then
  19. log.info("没有IPV6地址,无法演示")
  20. libnet.close(dName, 5000, netc)
  21. socket.release(netc)
  22. sysplus.taskDel(dName)
  23. return
  24. else
  25. log.info("本地IPV6地址", ipv6)
  26. end
  27. while true do
  28. log.info("start server!")
  29. log.info(rtos.meminfo("sys"))
  30. result = libnet.listen(dName, 0, netc)
  31. log.info(result)
  32. if result then
  33. result,_ = socket.accept(netc, nil) --只支持1对1
  34. if result then
  35. log.info("客户端连上了")
  36. end
  37. end
  38. cnt = 0
  39. while result do
  40. succ, param, rIP, rPort = socket.rx(netc, rx_buff)
  41. if not succ then
  42. log.info("客户端断开了", succ, param, ip, port)
  43. break
  44. end
  45. if rx_buff:used() > 0 then
  46. log.info("收到客户端数据,长度", rx_buff:used(), cnt)
  47. tx_buff:copy(nil,rx_buff) --把接收的数据返回给客户端
  48. rx_buff:del()
  49. cnt = cnt + 1
  50. end
  51. if tx_buff:used() > 0 then
  52. result, param = libnet.tx(dName, 5000, netc, tx_buff)
  53. end
  54. if not result then
  55. log.info("发送失败了", result, param)
  56. break
  57. end
  58. tx_buff:del()
  59. if tx_buff:len() > 1024 then
  60. tx_buff:resize(1024)
  61. end
  62. if rx_buff:len() > 1024 then
  63. rx_buff:resize(1024)
  64. end
  65. if cnt < 20 then
  66. log.info(rtos.meminfo("sys"))
  67. result, param = libnet.wait(dName, 30000, netc)
  68. if not result then
  69. log.info("客户端断开了", result, param)
  70. break
  71. end
  72. else
  73. log.info("接收数据20次以上,演示主动断开连接")
  74. result = false
  75. end
  76. log.info(rtos.meminfo("sys"))
  77. end
  78. libnet.close(dName, 5000, netc)
  79. log.info(rtos.meminfo("sys"))
  80. end
  81. end
  82. -- local function UDPTask(port)
  83. -- local tx_buff = zbuff.create(1024)
  84. -- local rx_buff = zbuff.create(1024)
  85. -- local remote_ip = zbuff.create(18)
  86. -- local netc
  87. -- local result, param, succ, rIP, rPort
  88. -- netc = socket.create(socket.ETH0, dName)
  89. -- socket.debug(netc, true)
  90. -- socket.config(netc, port, true)
  91. -- while true do
  92. -- log.info(rtos.meminfo("sys"))
  93. -- result = libnet.waitLink(dName, 0, netc)
  94. -- if result then
  95. -- result,_ = libnet.connect(dName, 5000, netc,"::",0)
  96. -- end:
  97. -- while result do
  98. -- result, param = libnet.wait(dName, 5000, netc)
  99. -- if not result then
  100. -- log.info("客户端断开了", result, param)
  101. -- break
  102. -- end
  103. -- succ, param, rIP, rPort = socket.rx(netc, rx_buff)
  104. -- if not succ then
  105. -- log.info("客户端断开了", succ, paramt)
  106. -- break
  107. -- end
  108. -- if rx_buff:used() > 0 then
  109. -- remote_ip:copy(0, rIP) --用于后续处理,必须的
  110. -- if remote_ip[0] == 0 then
  111. -- rIP = remote_ip[1] .. "." .. remote_ip[2] .. "." .. remote_ip[3] .. "." .. remote_ip[4] --不是必须的,只是打印一下方便看看
  112. -- log.info("收到客户端数据,长度", rx_buff:used(), rIP, rPort)
  113. -- else
  114. -- log.info("收到客户端数据,长度", rx_buff:used()) --w5500暂时支持IPV4,就不处理IPV6的情况了
  115. -- end
  116. -- tx_buff:copy(nil,rx_buff) --把接收的数据返回给客户端
  117. -- rx_buff:del()
  118. -- end
  119. -- if tx_buff:used() > 0 then
  120. -- if remote_ip[0] == 0 then
  121. -- result, param = libnet.tx(dName, 5000, netc, tx_buff, remote_ip:query(1,4,false), rPort)
  122. -- else
  123. -- result = true --w5500暂时支持IPV4,就不处理IPV6的情况了
  124. -- end
  125. -- end
  126. -- if not result then
  127. -- log.info("发送失败了", result, param)
  128. -- break
  129. -- end
  130. -- tx_buff:del()
  131. -- if tx_buff:len() > 1024 then
  132. -- tx_buff:resize(1024)
  133. -- end
  134. -- if rx_buff:len() > 1024 then
  135. -- rx_buff:resize(1024)
  136. -- end
  137. -- log.info(rtos.meminfo("sys"))
  138. -- end
  139. -- libnet.close(dName, 5000, netc)
  140. -- log.info(rtos.meminfo("sys"))
  141. -- sys.wait(1000)
  142. -- end
  143. -- end
  144. local function clientTask(port)
  145. d1Online = false
  146. local rx_buff = zbuff.create(1024)
  147. local netc
  148. local result, param, is_err, ip, mask, gw, ipv6
  149. netc = socket.create(nil, d1Name)
  150. socket.debug(netc, true)
  151. socket.config(netc)
  152. -- socket.config(netc, nil, true)
  153. while true do
  154. result = libnet.waitLink(d1Name, 0, netc)
  155. ip, mask, gw, ipv6 = socket.localIP()
  156. if not ipv6 then
  157. log.info("没有IPV6地址,无法演示")
  158. libnet.close(d1Name, 5000, netc)
  159. socket.release(netc)
  160. sysplus.taskDel(d1Name)
  161. return
  162. end
  163. log.info("start client")
  164. result = libnet.connect(d1Name, 15000, netc, ipv6, port)
  165. d1Online = result
  166. if result then
  167. log.info("服务器连上了")
  168. libnet.tx(d1Name, 15000, netc, "helloworld")
  169. end
  170. cnt = 1
  171. while result do
  172. succ, param, _, _ = socket.rx(netc, rx_buff)
  173. if not succ then
  174. log.info("服务器断开了", succ, param, ip, port)
  175. break
  176. end
  177. if rx_buff:used() > 0 then
  178. log.info("收到服务器数据,长度", rx_buff:used())
  179. rx_buff:del()
  180. end
  181. if rx_buff:len() > 1024 then
  182. rx_buff:resize(1024)
  183. end
  184. result, param = libnet.wait(d1Name, 5000, netc)
  185. if not result then
  186. log.info("服务器断开了", result, param)
  187. break
  188. end
  189. if not param then
  190. result, param = libnet.tx(d1Name, 15000, netc, "helloworld")
  191. if not result then
  192. log.info("服务器断开了", result, param)
  193. break
  194. end
  195. end
  196. end
  197. d1Online = false
  198. libnet.close(d1Name, 5000, netc)
  199. log.info(rtos.meminfo("sys"))
  200. sys.wait(1000)
  201. end
  202. end
  203. function SerDemo(port)
  204. mobile.ipv6(true)
  205. sysplus.taskInitEx(serTask, dName, netCB, port)
  206. -- sysplus.taskInitEx(clientTask, d1Name, netCB, port) --回环测试,如果不需要,可以注释掉
  207. end
  208. -- function UDPSerDemo(port)
  209. -- sysplus.taskInitEx(UDPTask, dName, netCB, port)
  210. -- end