注意:下面代码con 不能放在循环内,否则会一直不断连接,自行更改。
服务器server.lua
socket.bind( ) 创建接口
--服务器端/虚拟机 文件名server.lualocal socket = require("socket")--服务器IP地址local host = "10.0.0.201"--自定义端口local port = "8080"--创建IP接口,设置每段接收1024字节数据local server = socket.bind(host, port, 1024)local i = 0print("服务器已启动监听!" .. host)while true doi = i + 1--监听client.lua的请求,不是阻塞local con = server:accept()--接收到请求连接就会打印i,否则不会打印,不是阻塞print(i)end
con:receive( )—接收字符串
--连接成功con不等于nil值,失败则等于nilif con then--接收字符串local src = con:receive()print(src)end
con:send( ) —发送字符串
--连接成功con不等于nil值,失败则等于nilif con then--con连接成功,才能发送字符串,\n代表数据结尾con:send("hello".."\n")end
socket.select( ) —判断是否有接收字符串
//如果手机端没有运行con:send( )—发送字符串,而服务端运行了con:receive( )—接收字符串,那么就会导致服务端一直处于监听,不会继续执行代码,超过设定时间才会超时继续执行。
完整代码:
--服务器端/虚拟机 文件名server.lualocal socket = require("socket")--服务器IP地址local host = "10.0.0.201"--自定义端口local port = "8080"--创建IP接口,设置每段接收1024字节数据local server = socket.bind(host, port, 1024)local i = 0print("服务器已启动监听!" .. host)while true doi = i + 1--监听client.lua的请求,不是阻塞local con = server:accept()--接收到请求连接就会打印i,否则不会打印,不是阻塞print(i)--连接成功con不等于nil值,失败则等于nilif con then--con连接成功,才能发送字符串,\n代表数据结尾con:send("hello".."\n")--把con丢进去判断是否有接收到字符串数据,1为超时时间,结果返回tablelocal recvt, sendt, status = socket.select({con}, nil, 1)if #recvt > 0 then--接收字符串local src = con:receive()print(src)endendend
手机端client.lua
socket.connect( )—发送请求
--客户端/手机端 文件名client.lualocal socket = require("bblibs.socket.socket")--对应server.lua的IP地址local host = "10.0.0.201"--对应server.lua的端口local port = "8080"local i = 0while true doi = i + 1--发送请求,这里要注意:IP与端口不一致会导致阻塞,导致程序卡死local con = socket.connect(host, port)--如果是正常的IP地址,就会打印i,说明不阻塞print(i)end
con:receive( )—接收字符串
--连接成功con不等于nil值,失败则等于nilif con then--接收字符串local src = con:receive()print(src)end
con:send( ) —发送字符串
--连接成功con不等于nil值,失败则等于nilif con then--con连接成功,才能发送字符串,\n代表数据结尾con:send("hello".."\n")end
socket.select( ) —判断是否有接收字符串
//如果服务端没有运行con:send( )—发送字符串,而手机端运行了con:receive( )—接收字符串,那么就会导致手机端一直处于监听,不会继续执行代码,超过设定时间才会超时继续执行。
完整代码:
--客户端/手机端 文件名client.lualocal socket = require("bblibs.socket.socket")--对应server.lua的IP地址local host = "10.0.0.201"--对应server.lua的端口local port = "8080"local i = 0while true doi = i + 1--发送请求,这里要注意:IP与端口不一致会导致阻塞,导致程序卡死local con = socket.connect(host, port)--如果是正常的IP地址,就会打印i,说明不阻塞print(i)--连接成功con不等于nil值,失败则等于nilif con then--con连接成功,才能发送字符串,\n代表数据结尾con:send("hello".."\n")--把con丢进去判断是否有接收到字符串数据,1为超时时间,结果返回tablelocal recvt, sendt, status = socket.select({con}, nil, 1)if #recvt > 0 then--接收字符串local src = con:receive()print(src)endendend
关于socket里面的其他函数,想了解可以看文档
https://baijiahao.baidu.com/s?id=1611117152652243030&wfr=spider&for=pc
http://w3.impa.br/~diego/software/luasocket/socket.html#bind
local socket = require("socket")local sleep = socket.sleep--延时0.5秒sleep(0.5)
打印socket输出newtry = functionchoose = functiontcp6 = functionsource = functionudp6 = functionprotect = function_SETSIZE = number_DATAGRAMSIZE = numberBLOCKSIZE = number_SOCKETINVALID = numbersourcet = tabledns = tableselect = functionudp = functionsinkt = tabletcp = functionsink = functionconnect4 = functionskip = functionudp4 = functiontry = functiontcp4 = functionconnect6 = function__unload = function_VERSION = string
