看了好多文章.....唉,仍是本身亲自动手用网络监控软件测试吧html
先看这个节安装WEB服务器.....安装好之后就能够用HTTP访问电脑文件了node
其实HTTP就是创建在TCP通讯上,而后本身又封装了一套协议罢了,不过协议也不算多,协议内容都是用字符串发送的,也好理解json
感受要比我之前本身用TCP实现MQTT协议简单多了,MQTT规定的协议就是复杂点,所有用16进制组合......麻烦死了...数组
http://www.javashuo.com/article/p-wvodheue-ho.html服务器
你们学了这个文章,只要本身的模块支持TCP,那么就能够实现用HTTP访问下载文件,网络
废话少说,我就下载我本身云端的这个文件学习
https://blog.csdn.net/runner_diego/article/details/51379116 (这个是我在网上找的介绍http协议的)测试
启动个TCP客户端lua
链接的ip地址选择本身的哈 我测试用的是 47.92.31.46 端口号80 url
GET /hardware/wifi1/updata1.lua HTTP/1.1 Host: 47.92.31.46
先看get的用法
GET,一个空格,访问文件的路径,一个空格,用哪一个版本的HTTP协议
Host,冒号,一个空格,访问的地址
而后咱看看发送和具体接收的数据
3:26:18 发送数据:GET /hardware/wifi1/updata1.lua HTTP/1.1 Host: 47.92.31.46 [1次] 3:26:18 收到数据:HTTP/1.1 200 OK Date: Mon, 29 Apr 2019 19:26:19 GMT Server: Apache/2.4.39 (Win64) Last-Modified: Sat, 20 Apr 2019 15:48:39 GMT ETag: "7ac-586f82b4b7b40" Accept-Ranges: bytes Content-Length: 1964 local model = "wifi1" --product model --[[Do not update the following program !!!!]] local version1 = "0.0.0"; local version2 = "1.0.0"; if file.open("version2.lua", "r") then--local version2 = file.read() file.close(); end print("local version:"..version2) local JsonTable = {}; function UpdataFun(client, topic, data,jsondata) if jsondata["version"] ~= nil and jsondata["url"] ~= nil then if jsondata["version"] ~= version2 then version1 = jsondata["version"] JsonTable["data"] = "updata"; JsonTable["status"] = "unlike"; JsonTable["version"] = version2; if file.open("url.lua", "w+") then file.write((jsondata["url"])) file.close() end print(jsondata["version"],jsondata["url"]) else JsonTable["data"] = "updata"; JsonTable["status"] = "alike"; JsonTable["version"] = version2; end client:publish(PublishTopic,sjson.encode(JsonTable), 0, 0, function(client) end) JsonTable = {} elseif jsondata["cmd"] ~= nil and jsondata["cmd"] == "start" then if file.open("version1.lua", "w+") then file.write(version1) file.close() end JsonTable["data"] = "updata"; JsonTable["status"] = "start"; print(data) client:publish(PublishTopic,sjson.encode(JsonTable), 0, 0, function(client) node.restart(); end) JsonTable = {} elseif jsondata["cmd"] ~= nil and jsondata["cmd"] == "model" then JsonTable["data"] = "updata"; JsonTable["status"] = "model"; JsonTable["model"] = model; print(data) client:publish(PublishTopic,sjson.encode(JsonTable), 0, 0, function(client) end) JsonTable = {} end end
其实就这么简单就能够用HTTP访问下载文件了
其实我学习用TCP实现HTTP功能是为了想用HTTP下载大文件,最终是为了实现远程更新单片机程序,因此我为了让程序稳定可靠,我必须深刻了解HTTP
先看用WIFI模块自带的HTTP API下载大文件
http.get("http://47.92.31.46/hardware/wifi1/Progect.hex", nil, function(code, data) if (code < 0) then print("HTTP request failed") else print(code, data) end end)
直接报错说数据量太大
然而我用TCP调试助手发指令下载的时候发现了个问题
第一 下载下来了
第二 我监听了一下网络数据,发现
其实Apache服务器默认就会把大文件分段传输过来
而后我就作了个WIFI用TCP实现HTTP,而后下载
wifi.setmode(wifi.STATIONAP) apcfg={} apcfg.ssid="qqqqq" apcfg.pwd="11223344" wifi.sta.config(apcfg) wifi.sta.autoconnect(1) ClientConnectedFlage = 0 TcpConnect = nil tmr.alarm(1, 10000, 1, function() if ClientConnectedFlage == 0 then Client = net.createConnection(net.TCP, 0) Client:connect(80,"47.92.31.46") Client:on("receive", function(Client, data) uart.write(0,data) end) Client:on("connection", function(sck, c) ClientConnectedFlage = 1 TcpConnect = Client print("Link OK") tmr.stop(1) Client:on("disconnection", function(sck, c) ClientConnectedFlage = 0 TcpConnect = nil tmr.start(1) end) TcpConnect:send("GET /hardware/wifi1/Progect.hex HTTP/1.1\r\nConnection: keep-alive\r\nHost: 47.92.31.46\r\n\r\n") end) if ClientConnectedFlage == 0 then print("Link Error") end end end) uart.on("data",0,function(data) end, 1) printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP"..T.IP) end printip = 1 end)
毫无压力的所有下载下来了.
因此我才知道,WIFI模块里面写的HTTP是把全部分段过来的数据所有接收到一个数组里面再调用回调....然而就会形成内存不足
用TCP实现HTTP的时候是接收一段打印出来一段,并非把全部的数据所有放到一个数组里面,而后打印.....
通过这一次,我感受我之后用HTTP的时候仍是直接用TCP来实现,主要仍是很简单,并且还能预防再次出现内存问题....