Lua 服务器与客户端实例(转)

简介: =============================================================== 服务器,main.lua =============================================================== local ...

===============================================================

服务器,main.lua

===============================================================

local socket = require ( "socket" )

local tcpServer = nil

--创建指定端口的tcp server
local function createTCPServer( port )

    -- Create Socket
    local tcpServerSocket , err = socket.tcp()
    local backlog = 5

    -- Check Socket
    if tcpServerSocket == nil then
        return nil , err
    end

    -- Allow Address Reuse
    tcpServerSocket:setoption( "reuseaddr" , true )

    -- Bind Socket
    local res, err = tcpServerSocket:bind( "*" , port )
    if res == nil then
        return nil , err
    end

    -- Check Connection
    res , err = tcpServerSocket:listen( backlog )
    if res == nil then
        return nil , err
    end

    -- Return Server
    return tcpServerSocket

end

--接受来自客户端的连接,并保存在tcpClient
local tcpClient = nil
local function acceptClient()
    tcpServer:settimeout( 0 )
    clientIn , _ = tcpServer:accept()
    if clientIn then
        tcpClient = clientIn
    end
end

--接收已连接的客户端(tcpClient)数据
local function receiveLoop()
    if tcpClient ~= nil then
        local tcpClientMessage , _ = tcpClient:receive('*l')

        if ( tcpClientMessage ~= nil ) then
            print(tcpClientMessage)
            tcpClient:send( "back:" .. tcpClientMessage .. "\n")
        end
    end
end

local function main()
    tcpServer , _ = createTCPServer( 8080 )
    if tcpServer then
        Runtime:addEventListener( "enterFrame" , acceptClient )
        Runtime:addEventListener( "enterFrame" , receiveLoop )
    end
end

main()
 

===============================================================

客户端程序,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

http://blog.csdn.net/chunleixiahe/article/details/41683427

目录
相关文章
|
9月前
|
存储 人工智能 容灾
阿里云服务器2核8G、4核16G、8核32G配置热门实例性能对比与场景化选型指南
2核8G/4核16G/8核32G配置的阿里云服务器在阿里云活动中目前有经济型e、通用算力型u1、通用型g7、通用型g8y和通用型g9i五种实例可选,目前2核8G配置选择u1实例活动价格652.32元1年起,4核16G月付选择经济型e实例最低89元1个月,8核32G配置160元1个月起,本文将为大家解析经济型e、通用算力型u1、通用型g7及通用型g8y实例,帮助用户根据自身需求合理选择最适合的实例规格和配置。
|
8月前
|
存储 弹性计算 安全
阿里云渠道商:新手如何选择阿里云ECS实例?
阿里云ECS凭借弹性扩展、稳定可靠与安全防护,助力企业高效上云。本文系统解析实例规格选择关键因素:业务场景匹配、性能评估、成本优化、地域部署与扩展规划,结合计费模式与实际需求,提供科学选型建议,助您精准匹配资源,提升云上效能。(238字)
|
10月前
|
存储 缓存 安全
阿里云服务器热门实例选择参考:e/u1/c7/c8y/c9i/g7/g8y/g9i/r7/r8y/r9i实例适用场景汇总
通过阿里云的活动购买云服务器时,除了轻量应用服务器之外,活动内的云服务器实例规格主要以e/u1/c7/c8y/c9i/g7/g8y/g9i/r7/r8y/r9i这几种为主。本文主要为介绍阿里云服务器的实例规格是什么,有什么用,并汇总了阿里云轻量应用服务器和这些热门云服务器实例规格的适用场景,以供大家了解和选择适合自己的需求的实例规格。
|
10月前
|
存储 弹性计算 缓存
企业级云服务器租用一年需要多少预算?199元2核4G5M带宽阿里云u1实例评测
阿里云企业用户专享,仅需199元/年即可租用ECS通用算力型u1实例(2核4G、5M带宽、80G云盘),适用于中小企业官网、电商展示、数据分析等场景。支持多地域部署,新老用户均可享受,性价比高,助力企业稳定运营。
678 7
|
10月前
|
存储 弹性计算 安全
阿里云轻量服务器通用型、CPU优化型、多公网IP型、国际型、容量型不同实例区别与选择参考
阿里云轻量应用服务器实例类型分为通用型、CPU优化型、多公网IP型、国际型、容量型,不同规格族的适用场景和特点不同,收费标准也不一样。本文为大家介绍轻量应用服务器通用型、多公网IP型、容量型有何区别?以及选择参考。
|
9月前
|
存储 弹性计算 网络协议
阿里云服务器ECS通用算力型u2a实例,CPU采用AMD EPYC处理器,睿频最高3.7 GHz
阿里云ECS通用算力型u2a实例搭载AMD EPYC处理器,睿频高达3.7GHz,基于CIPU架构,网络与存储突发带宽最高25Gbps,I/O性能强、延迟低。支持多种云盘及IPv4/IPv6,适用于中小型数据库、APP服务等场景,性价比高,官网价降低9%-22%,是中小企业上云优选。
1049 0