Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54069
  • 博文数量: 28
  • 博客积分: 84
  • 博客等级: 民兵
  • 技术积分: 162
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-09 17:47
个人简介

try vuuv.github.io or somewhere else.

文章分类

全部博文(28)

文章存档

2013年(1)

2012年(5)

2011年(22)

我的朋友

分类:

2013-01-04 12:01:19


 

我的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里面自带的一个库,负责调度,代码正在看。
阅读(1732) | 评论(0) | 转发(0) |
0

上一篇:gawk 收取邮件测试

下一篇:没有了

给主人留下些什么吧!~~