在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
=============================================================== 服务器,main.lua =============================================================== local socket = require ( "socket" ) =============================================================== 客户端程序,main.lua =============================================================== local ui = require("ui")
local socket = require("socket") local tcpClient = nil --接收来自服务器的数据 local function receiveData() if tcpClient then tcpClient:settimeout(0) local msg = tcpClient:receive("*l") if msg then print(msg) end end end --向服务器发送数据 local index = 0 local function btnListener(event) index = index + 1 if tcpClient then tcpClient:send("pack index:" .. index .. ".\n") end end --初始化 local function main() local btn = ui.newButton{ default = "buttonBlue.png", over = "buttonBlueOver.png", onRelease = btnListener, id = "btn" } btn:setReferencePoint(display.TopLeftReferencePoint) btn.x = 10 btn.y = 100 --创建tcp连接 tcpClient = socket.tcp() if tcpClient then local ret = tcpClient:connect("192.168.1.2", 8080) if ret then tcpClient:send("hi, server, i'm client.\n") Runtime:addEventListener("enterFrame", receiveData) end end end main() lua socket相关文档参见:http://w3.impa.br/~diego/software/luasocket/tcp.html |
请发表评论