Problem with luasocket

前端 未结 1 1374
醉酒成梦
醉酒成梦 2021-01-13 12:33

I\'m trying to read some (binary) data from a lua socket, but the above code do not terminate the repeat-loop. How can i know that the end of stream has reached ?

         


        
相关标签:
1条回答
  • 2021-01-13 13:12

    Ok, i have found this solution

    client = require("socket")
    client = socket.connect("www.google.com",80)
    client:send("GET / HTTP/1.1\n\n")
    client:settimeout(1)
    repeat
      print "read"
      line,err,rest = client:receive(512)
      print "read done"
      if line then print(line) end
      if rest then print(rest) end
    until err
    
    print "all done"
    

    The drawback is the settimeout, because the request will take at least 1 second and any network delay more than 1 sec will result in an error.

    0 讨论(0)
提交回复
热议问题