Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why udp_recv_start callback call twice? #680

Closed
cc-hng opened this issue Dec 4, 2023 · 2 comments
Closed

Why udp_recv_start callback call twice? #680

cc-hng opened this issue Dec 4, 2023 · 2 comments

Comments

@cc-hng
Copy link

cc-hng commented Dec 4, 2023

I wrote a udp server/client sample as below, the callback of server:recv_start called twice.

local uv = require("luv")
local inspect = require("inspect")

function set_interval(interval, callback)
	local timer = uv.new_timer()
	uv.timer_start(timer, interval, interval, function()
		callback(timer)
	end)
	return timer
end

local server = uv.new_udp()
server:bind("0.0.0.0", 9124, { reuseaddr = true })
server:recv_start(function(err, data, addr, flags)
  -- assert(not err, err)
  -- assert(data == "PING")
  print(inspect { err, data, addr, flags })
  -- uv.close(server)
end)

local client = uv.new_udp()
set_interval(3000, function()
	client:send("PING", "127.0.0.1", 9124, function(err) end)
end)

uv.run()

log

{
  [2] = "PING",
  [3] = {
    family = "inet",
    ip = "127.0.0.1",
    port = 39178
  },
  [4] = {}
}
{
  [4] = {}
}
@SinisterRectus
Copy link
Member

When data is nil, that's to signal that the stream is exhausted and you can/should close the connection. I believe it is because your client connection is not kept alive, but I'm not sure. Look at the luv readme for a working server/client echo.

@cc-hng
Copy link
Author

cc-hng commented Dec 4, 2023

Thanks a lot, I have found an answer from the libuv/help
libuv/help#89 (comment)

@cc-hng cc-hng closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants