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

服务器,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

Lua 服务器与客户端实例的更多相关文章

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

    =============================================================== 服务器,main.lua ======================= ...

  2. Lua 服务器Socket通信实例(转)

    local socket = require"socket" local host = "127.0.0.1"local port = "843&qu ...

  3. Lua 服务器Socket通信实例

    local socket = require"socket" local host = "127.0.0.1"local port = "843&qu ...

  4. linux邮件服务器postfix配置实例

    linux邮件服务器postfix配置实例(超级详细!!!) 2013-03-13 13:30:21 标签:邮件服务器 linux 1. 系统安装:1)centos4.3 选上MAIL组件里的全部.2 ...

  5. Linux系统编程(32)—— socket编程之TCP服务器与客户端

    TCP协议的客户端/服务器程序的一般流程 服务器调用socket().bind().listen()完成初始化后,调用accept()阻塞等待,处于监听端口的状态,客户端调用socket()初始化后, ...

  6. Oracle服务器和客户端的安装和卸载

    Oracle 11g服务器与客户端的完全卸载方式与前些版本有了改变: 一.卸载前准备: 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. 二.批处理卸载 ...

  7. c#Socket服务器与客户端的开发(2)

    上一篇文章我们使用原生的socket分别实现了服务器和客户端, 本篇文章使用SuperSocket来开发实现服务器, 之前也介绍了SuperSocket是一个轻量级, 跨平台而且可扩展的 .Net/M ...

  8. node.js中使用http模块创建服务器和客户端

    node.js中的 http 模块提供了创建服务器和客户端的方法,http 全称是超文本传输协议,基于 tcp 之上,属于应用层协议. 一.创建http服务器 const http = require ...

  9. Node学习HTTP模块(HTTP 服务器与客户端)

    Node学习HTTP模块(HTTP 服务器与客户端) Node.js 标准库提供了 http 模块,其中封装了一个高效的 HTTP 服务器和一个简易的HTTP 客户端.http.Server 是一个基 ...

随机推荐

  1. IntelliJ常用设置及快捷键

    转自: http://www.blogjava.net/rockblue1988/archive/2014/10/25/418994.html 一.黑色主题 Darcula眼睛舒服,最重要的是酷!设置 ...

  2. Python+Flash+NodeJS 接口自动化平台

    一.前端安装步骤# manager-web(1)下载项目 git clone https://github.com/t880216t/manager-web.git (2) 安装依赖 cnpm ins ...

  3. No converter found for return value of type

    springMVC请求接口的时候报500  No converter found for return value of type 原因:这是因为springmvc默认是没有对象转换成json的转换器 ...

  4. hotplug 热拔插机制框架

    框架入口源文件: mdev.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6     硬件平台:JZ2440 以下是驱动框架:

  5. MySQL 5.5 服务器变量详解(二)

    innodb_adaptive_flushing={ON|OFF} 设定是否允许MySQL服务器根据工作负载动态调整刷写InnoDB buffer pool中的脏页的速率.动态调整刷写速率的目的在于避 ...

  6. wc 统计命令

    [root@localhost ~]# wc /etc/passwd // 统计行数.单词数.字符数 /etc/passwd [root@localhost ~]# wc -l /etc/passwd ...

  7. pandas操作速查表

    准备工作 import numpy as np import pandas as pd 倒入文件或创建一个数据表 df = pd.DataFrame(pd.read_csv('name.csv',he ...

  8. 爬虫请求库——selenium

    selenium模块 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题.selenium的缺点是效率会变得很慢. sel ...

  9. FastReport快速入门

    界面及连接关系 frxDBDataset1连接datasource 双击frxReport1控件进入设计界面,选择报表->数据,弹出窗口选择要显示数据的frxdbdataset,frxdbdat ...

  10. ORACLE入门之Linux基础篇

    VIM0 这是数字『0 』:移动到这一行的最前面字符处$    移动到这一行的最后面字符处G    移动到这个档案的最后一行nG   n 为数字.移动到这个档案的第n 行.例如20G 则会移动到这个档 ...