lua socket
2016-03-15 10:10
591 查看
首先要有相应的socket模块
![](http://img.blog.csdn.net/20160315100745781)
server.lua
![](http://img.blog.csdn.net/20160315100359049)
参考学习:
http://blog.163.com/hbu_lijian/blog/static/126129153201422023644730/
server.lua
socket = require("socket"); host = host or "127.0.0.1"; port = port or "8888"; server = assert(socket.bind(host, port)); ack = "I'm server , ack\n"; while 1 do print("server: waiting for client connection..."); control = assert(server:accept()); while 1 do command,status = control:receive(); if status == "closed" then break end print(command); control:send(ack); end endclient.lua
local socket = require("socket") host = "127.0.0.1" port = 8888 --打开一个TCP连接 c = assert (socket.connect (host, port)) c:send ("I'm client,GET \n") i = 0 while (true) do local s, status, partial = c:receive () print(s) if status == "closed" then break end i = i + 1 if i == 10 then print("我主动断开连接了") c:close(); -- 主动关闭连接 end c:send ("GET \n") end c:close ()
参考学习:
http://blog.163.com/hbu_lijian/blog/static/126129153201422023644730/
相关文章推荐
- 有关Lua脚本语言应用
- Sublime -- 配置lua环境
- Lua面向对象程序设计
- lua Date和Time
- lua-协成
- Lua 的元表怎么理解
- Lua 的元表怎么理解
- Lua中的元表与元方法
- Lua中的元表与元方法
- lua-table
- 使用Weka进行数据挖掘(Weka教程八)Weka分类评价Evaluation输出分析
- lua-闭包
- skynet lua加密方法
- lua脚本简单的例子使用
- Computing Classification Evaluation Metrics in R
- 《leetCode》:Evaluate Reverse Polish Notation
- lua文件操作详解
- 多维数组与一维数组的相互转换
- Lua和C++交互详细总结
- lua-队列