Skip to content

Instantly share code, notes, and snippets.

@outsinre
Created September 21, 2023 07:21
Show Gist options
  • Save outsinre/8b3987e3b4d6a12aac4962555ef70efd to your computer and use it in GitHub Desktop.
Save outsinre/8b3987e3b4d6a12aac4962555ef70efd to your computer and use it in GitHub Desktop.
The send buffer has data
local socket = require("socket")
local host, port = "180.101.50.242", 80
local sock = socket.connect(host, port)
sock:settimeout(1)
sock:send("GET / HTTP/1.1\r\n\r\n")
sock:close()
@outsinre
Copy link
Author

outsinre commented Sep 21, 2023

sock;close() will cause TCP RST, as the receiving buffer has data.

@outsinre
Copy link
Author

Fix:

local socket = require("socket")

local host, port = "www.baidu.com", 80
local sock = socket.connect(host, port)
sock:settimeout(1)
sock:send("GET / HTTP/1.1\r\n\r\n")

while true do
    local line, err = sock:receive()
    if err == "timeout" then
        break
    end
end
sock:close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment