-- 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程序的更多相关文章

  1. Lua的协同程序初探

    Content: 前两天把Lua的协同程序概念看了一下,不是很懂,只能说<Programming In Lua>中把它解释成线程让人很好的理解起来,但是真正去看的时候,收获并不是很大.第一 ...

  2. 使用lua扩展应用程序

    全局变量的操作 void lua_getglobal(lua_State * L ,const char * name) 此函数从lua中取出一个名为name的全局变量并将其压入栈中. 如当lua文件 ...

  3. Lua使用luasocket http请求例子

    local http=require("socket.http"); local request_body = [[login=user&password=123]] lo ...

  4. Lua学习笔记4. coroutine协同程序和文件I/O、错误处理

    Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...

  5. Lua中的协同程序

    [前言] 协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈.局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西.从概念上讲,线程与协同程序的主要区别在于,一个具有多个线程的 ...

  6. 《Lua程序设计》9.1 协同程序基础 学习笔记

    协同程序(coroutine)与线程(thread)差不多,也就是一条执行序列,拥有自己独立的栈.局部变量和指令指针,同时又与其他协同程序共享全局变量和其他大部分东西.从概念上讲线程与协同程序的主要区 ...

  7. lua 15 协程-协同程序

    转自:http://www.runoob.com/lua/lua-coroutine.html 什么是协同(coroutine)? Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈 ...

  8. Lua 协同程序(coroutine)

    什么是协同(coroutine)? Lua 协同程序(coroutine)与线程比较类似:拥有独立的堆栈,独立的局部变量,独立的指令指针,同时又与其它协同程序共享全局变量和其它大部分东西. 协同是非常 ...

  9. LuaSocket http笔记

    LuaSocket 基于Lua平台开发的一套socket的lua接口库程序, 为lua程序的扩展 ,http://w3.impa.br/~diego/software/luasocket/home.h ...

随机推荐

  1. HDU 多校对抗赛 J Time Zone

    Time Zone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. AQS同步组件及ReentrantLock和synchronized的区别

    AQS同步组件 CountDownLatch(只有一个线程对他进行操作): 主线程必须在启动其它线程后立即调用await()方法.这样主线程的操作就会在这个方法上阻塞,直到其它线程完成各自的任务. S ...

  3. sublime text 2 学习(一):快捷键

    初用sublime text 2,还不错,不装任何插件,能很好的编辑javascript,css,html,很赞.整理一下快捷键:常用的比如Ctrl+S就不列了:而且只列举Windows的. Ctrl ...

  4. Codeforces Round #433

    我会4题,写了两题,还提交错误n次,掉了40rating(哭丧脸),又被学长D飞了. 学长:我很心疼你的成绩啊: 我:第四题忘记加特判了... 学长:暴力还能写挂. 我:...... ———————— ...

  5. arm SecurCore 处理器【转】

    转自:http://www.arm.com/zh/products/processors/securcore/index.php SecurCore 处理器 (View Larger SecurCor ...

  6. git 提示 Please move or remove them before you can merge 解决办法

    解决Git冲突造成的Please move or remove them before you can merge git clean -d -fx其中x -----删除忽略文件已经对git来说不识别 ...

  7. windows使用celery遇到的错误

    https://www.jianshu.com/p/e5539d96641c 按照这个教程一步步执行到最后报错了. 运行task_dispatcher.py的时候 ValueError: not en ...

  8. 【 Zabbix 】nginx监控及故障重启服务

    zabbix agentd 端配置: nginx.conf 配置: location /ngx_status { stub_status on; access_log off; allow 10.0. ...

  9. opencv-写入AVI视频文件

    #include <cv.h> #include <highgui.h> int main(int argc, char **argv) { CvCapture* captur ...

  10. Selenium2+python自动化27-查看selenium API【转载】

    前言 前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. p ...