Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1241235
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: Python/Ruby

2012-08-03 13:57:35


 

我的lua是5.1 luasocket是2.0.1 环境是centos5.8
VMware建立3台centos虚拟机,我是把一台的搭建好后,copy了一下,注意启动copy的os时会提示让你选择当前系统是copy or move,一定要选择copy。
 
首先需要确定你的linux是否支持多播,看一下/boot下的config文件,这个网上有许多资料指导。
开启路由:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
写luasocket的哥们是个高手,压缩包里不但有源码,还有很多lua的例子,看得我云里雾里的,还需要进步啊。
代码如下:
client:

点击(此处)折叠或打开

  1. local dispatch = require("dispatch")
  2. local handler = dispatch.newhandler()

  3. local socket = require("socket")
  4. host = host or "224.0.0.89"
  5. port = port or 8899
  6. if arg then
  7.     host = arg[1] or host
  8.     port = arg[2] or port
  9. end
  10. --host = socket.dns.toip(host)
  11. udp = assert(socket.udp())
  12. --assert(udp:setpeername(host, port))
  13. assert(udp:setsockname(host, port))
  14. print("Using remote host '" ..host.. "' and port " .. port .. "...")

  15. --assert(udp:setoption("ip-multicast-loop",true))
  16. assert(udp:setoption("ip-multicast-loop",false))
  17. assert(udp:setoption("ip-add-membership",{multiaddr=host,interface="*"}))

  18. while 1 do
  19.     dgram, pip, pport = udp:receivefrom()
  20.         --dgram = assert(udp:receive())
  21.         print("recv data : " ..dgram.." from " ..pip.." : "..pport)
  22.         print(">> ")
  23. end
server:

点击(此处)折叠或打开

  1. local dispatch = require("dispatch")
  2. local handler = dispatch.newhandler()

  3. local socket = require("socket")
  4. host = host or "224.0.0.89"
  5. port = port or 8899
  6. if arg then
  7.     host = arg[1] or host
  8.     port = arg[2] or port
  9. end
  10. --host = socket.dns.toip(host)
  11. udp = assert(socket.udp())
  12. assert(udp:setpeername(host, port))
  13. print("Using remote host '" ..host.. "' and port " .. port .. "...")

  14. assert(udp:setoption("ip-multicast-loop",true))
  15. assert(udp:setoption("ip-add-membership",{multiaddr=host,interface="*"}))

  16. while 1 do
  17.     print(">> ")
  18.     line = io.read()
  19.     if not line or line == "" then
  20.         assert(udp:setoption("ip-drop-membership",{multiaddr=host,interface="*"}))
  21.         os.exit()
  22.         end
  23.     assert(udp:send(line))            
  24. end
dispatch是luasocket里面自带的一个库,负责调度,代码正在看。
阅读(5984) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~