lua的luasocket程序
-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
-- wait for a connection from any client
local client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then client:send(line .. "\n") end
-- done with client, close the object
client:close()
end
- 程序会自动生成一个随机端口号例如:54142,
telnet 127.0.0.1 54142,将会返回输入的字符
lua的luasocket程序的更多相关文章
- Lua的协同程序初探
Content: 前两天把Lua的协同程序概念看了一下,不是很懂,只能说<Programming In Lua>中把它解释成线程让人很好的理解起来,但是真正去看的时候,收获并不是很大.第一 ...
- 使用lua扩展应用程序
全局变量的操作 void lua_getglobal(lua_State * L ,const char * name) 此函数从lua中取出一个名为name的全局变量并将其压入栈中. 如当lua文件 ...
- Lua使用luasocket http请求例子
local http=require("socket.http"); local request_body = [[login=user&password=123]] lo ...
- Lua学习笔记4. coroutine协同程序和文件I/O、错误处理
Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...
- Lua中的协同程序
[前言] 协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈.局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西.从概念上讲,线程与协同程序的主要区别在于,一个具有多个线程的 ...
- 《Lua程序设计》9.1 协同程序基础 学习笔记
协同程序(coroutine)与线程(thread)差不多,也就是一条执行序列,拥有自己独立的栈.局部变量和指令指针,同时又与其他协同程序共享全局变量和其他大部分东西.从概念上讲线程与协同程序的主要区 ...
- lua 15 协程-协同程序
转自:http://www.runoob.com/lua/lua-coroutine.html 什么是协同(coroutine)? Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈 ...
- Lua 协同程序(coroutine)
什么是协同(coroutine)? Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈,独立的局部变量,独立的指令指针,同时又与其它协同程序共享全局变量和其它大部分东西. 协同是非常 ...
- LuaSocket http笔记
LuaSocket 基于Lua平台开发的一套socket的lua接口库程序, 为lua程序的扩展 ,http://w3.impa.br/~diego/software/luasocket/home.h ...
随机推荐
- 用spring的@Validated注解和org.hibernate.validator.constraints.*的一些注解在后台完成数据校验
这个demo主要是让spring的@Validated注解和hibernate支持JSR数据校验的一些注解结合起来,完成数据校验.这个demo用的是springboot. 首先domain对象Foo的 ...
- [06] JavaScript 类型
下面对知识点总结: 1.类型分类 a.原始类型:number, string, boolean, null, undefined b.对象类型:除了原始类型都是(例如:object,array, fu ...
- React 开发常见报错解决方法
1. 使用 redux 的异步 action 时浏览器报错: Error: Actions must be plain objects. Use custom middleware for async ...
- MySql数据库学习总结(MySQL入门到精通)
2017.1.24-2.3日(在大兴实验室) 1.数据库存储引擎: (1)MyISAM: 访问速度快,对事物完整性没要求,并以访问为主的适合这个 (2)InnoDB: 更占磁盘空间,需要进行频繁的更新 ...
- import as from import 区别
在python中import或者from…import是用来导入相应的模块.那每一种有什么具体的差别呢? 一.import 只有import,为最简单的引入对应的包.例如: import ...
- 汕头市队赛 SRM16
T3 C-2 SRM 16 描述 给一个数列,给出两种数字, 询问在多少个非空区间中这两种数字出现次数相同. 输入格式 第一行:一个数字n,q,n表示数列长度,q表示q组询问 第二行n个数字表示数列A ...
- python 调用Linux shell
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- 复选框回显、全选、非全选、cookie处理数据、json数组对象转换处理学习笔记参考的页面
<%@include file="/common/head.jsp"%> <%@ page contentType="text/html; charse ...
- select实现斐波那契和超时机制
package main import "fmt" func fib(ch chan <-int, quit <- chan bool){ x, y := 1, 1 f ...
- python面向对象之__new__方法
众所周知,python中定义的类在创建实例对象的时候,会自动执行__init__()方法,但是在执行__init__()方法之前,会执行__new__()方法. __new__()的作用主要有两个. ...