server_demo.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. local libnet = require "libnet"
  2. --下面演示用阻塞方式做自动应答服务器,只适合W5500
  3. local dName = "D2_TASK"
  4. local function netCB(msg)
  5. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  6. end
  7. local function serTask(port)
  8. local tx_buff = zbuff.create(1024)
  9. local rx_buff = zbuff.create(1024)
  10. local netc
  11. local result, param, succ, rIP, rPort
  12. netc = socket.create(socket.ETH0, dName)
  13. --socket.debug(netc, true)
  14. socket.config(netc, port)
  15. while true do
  16. log.info(rtos.meminfo("sys"))
  17. result = libnet.waitLink(dName, 0, netc)
  18. result = libnet.listen(dName, 0, netc)
  19. if result then
  20. result,_ = socket.accept(netc, nil) --W5500的硬件协议栈不支持一对多
  21. if result then
  22. log.info("客户端连上了")
  23. libnet.tx(dName, 0, netc, "helloworld")
  24. end
  25. end
  26. while result do
  27. succ, param, rIP, rPort = socket.rx(netc, rx_buff)
  28. if not succ then
  29. log.info("客户端断开了", succ, param, rIP, port)
  30. break
  31. end
  32. if rx_buff:used() > 0 then
  33. log.info("收到客户端数据,长度", rx_buff:used())
  34. tx_buff:copy(nil,rx_buff) --把接收的数据返回给客户端
  35. rx_buff:del()
  36. end
  37. if tx_buff:used() > 0 then
  38. result, param = libnet.tx(dName, 5000, netc, tx_buff)
  39. end
  40. if not result then
  41. log.info("发送失败了", result, param)
  42. break
  43. end
  44. tx_buff:del()
  45. if tx_buff:len() > 1024 then
  46. tx_buff:resize(1024)
  47. end
  48. if rx_buff:len() > 1024 then
  49. rx_buff:resize(1024)
  50. end
  51. log.info(rtos.meminfo("sys"))
  52. result, param = libnet.wait(dName, 5000, netc)
  53. if not result then
  54. log.info("客户端断开了", result, param)
  55. break
  56. end
  57. end
  58. libnet.close(dName, 5000, netc)
  59. log.info(rtos.meminfo("sys"))
  60. sys.wait(1000)
  61. end
  62. end
  63. local function UDPTask(port)
  64. local tx_buff = zbuff.create(1024)
  65. local rx_buff = zbuff.create(1024)
  66. local remote_ip = zbuff.create(18)
  67. local netc
  68. local result, param, succ, rIP, rPort
  69. netc = socket.create(socket.ETH0, dName)
  70. socket.debug(netc, true)
  71. socket.config(netc, port, true)
  72. while true do
  73. log.info(rtos.meminfo("sys"))
  74. result = libnet.waitLink(dName, 0, netc)
  75. if result then
  76. result,_ = libnet.connect(dName, 5000, netc,"255.255.255.255",0) --W5500的硬件协议栈不支持一对多
  77. end
  78. while result do
  79. result, param = libnet.wait(dName, 5000, netc)
  80. if not result then
  81. log.info("客户端断开了", result, param)
  82. break
  83. end
  84. succ, param, rIP, rPort = socket.rx(netc, rx_buff)
  85. if not succ then
  86. log.info("客户端断开了", succ, param)
  87. break
  88. end
  89. if rx_buff:used() > 0 then
  90. remote_ip:copy(0, rIP) --用于后续处理,必须的
  91. if remote_ip[0] == 0 then
  92. rIP = remote_ip[1] .. "." .. remote_ip[2] .. "." .. remote_ip[3] .. "." .. remote_ip[4] --不是必须的,只是打印一下方便看看
  93. log.info("收到客户端数据,长度", rx_buff:used(), rIP, rPort)
  94. else
  95. log.info("收到客户端数据,长度", rx_buff:used()) --w5500暂时支持IPV4,就不处理IPV6的情况了
  96. end
  97. tx_buff:copy(nil,rx_buff) --把接收的数据返回给客户端
  98. rx_buff:del()
  99. end
  100. if tx_buff:used() > 0 then
  101. if remote_ip[0] == 0 then
  102. result, param = libnet.tx(dName, 5000, netc, tx_buff, remote_ip:query(1,4,false), rPort)
  103. else
  104. result = true --w5500暂时支持IPV4,就不处理IPV6的情况了
  105. end
  106. end
  107. if not result then
  108. log.info("发送失败了", result, param)
  109. break
  110. end
  111. tx_buff:del()
  112. if tx_buff:len() > 1024 then
  113. tx_buff:resize(1024)
  114. end
  115. if rx_buff:len() > 1024 then
  116. rx_buff:resize(1024)
  117. end
  118. log.info(rtos.meminfo("sys"))
  119. end
  120. libnet.close(dName, 5000, netc)
  121. log.info(rtos.meminfo("sys"))
  122. sys.wait(1000)
  123. end
  124. end
  125. function SerDemo(port)
  126. sysplus.taskInitEx(serTask, dName, netCB, port)
  127. end
  128. function UDPSerDemo(port)
  129. sysplus.taskInitEx(UDPTask, dName, netCB, port)
  130. end