فهرست منبع

Because I changed the retrun value of tcp.receive to return an array [data, error] this sample was modified.

mingodad 13 سال پیش
والد
کامیت
06854d99cb
1فایلهای تغییر یافته به همراه8 افزوده شده و 5 حذف شده
  1. 8 5
      samples/test-tcp.nut

+ 8 - 5
samples/test-tcp.nut

@@ -3,7 +3,7 @@ function download(host, file, extra_header=null){
 	sock.connect(host, 80);
 	sock.connect(host, 80);
 	local info = sock.getpeername()
 	local info = sock.getpeername()
 	print(info.address, info.port);
 	print(info.address, info.port);
-	//sock.settimeout(1, "t");
+	//sock.settimeout(1);
 
 
 	local count = 0;    // counts number of bytes read
 	local count = 0;    // counts number of bytes read
 	local req = "GET " + file + " HTTP/1.1\r\nHost: " + host + "\r\n";
 	local req = "GET " + file + " HTTP/1.1\r\nHost: " + host + "\r\n";
@@ -11,21 +11,24 @@ function download(host, file, extra_header=null){
 	req += "\r\n";
 	req += "\r\n";
 	print(req, req.len());
 	print(req, req.len());
 	sock.send(req);
 	sock.send(req);
-	local s
+	local s, rc;
 	local tbl = {};
 	local tbl = {};
 	local len = 0;
 	local len = 0;
 	while (true){
 	while (true){
-		s = sock.receive("*l");
-		print("s", s);
+		rc = sock.receive("*l");
+		s = rc[0];
+		print("s", s, rc[1]);
 		//if err == "closed" then break end
 		//if err == "closed" then break end
 		if (s.len() == 0) break;
 		if (s.len() == 0) break;
+		//if (rc[1] == socket.IO_CLOSED) break;
 		local slen;
 		local slen;
 		s.gmatch("Content%-Length: (%d+)", function(m){ slen=m; return false;});
 		s.gmatch("Content%-Length: (%d+)", function(m){ slen=m; return false;});
 		if (slen) {
 		if (slen) {
 			len = slen.tointeger();
 			len = slen.tointeger();
 		}
 		}
 	}
 	}
-	s = sock.receive(len);
+	rc = sock.receive(len);
+	s = rc[0];
 	sock.close();
 	sock.close();
 	//print(file, count)
 	//print(file, count)
 	return s;
 	return s;