leafo / lua-enet

Bindings to ENet for Lua
http://leafo.net/lua-enet/
85 stars 25 forks source link

Attempt to index local 'host' (a nil value) #23

Closed erioxis closed 2 years ago

erioxis commented 3 years ago

I am triyng to run server example but get error image

999pingGG commented 2 years ago

This exact same thing happened to me, but I'm using https://github.com/zpl-c/enet. After some debugging, turns out we have undefined behavior with that modification of ENet: an uninitialized variable. In file enet.c, line 219, change ENetAddress address; to ENetAddress address = {0};.

The field sin6_scope_id of the struct ENetAddress does not exist in the original ENet library so it is not being assigned. It may contain garbage and seems like it needs to be zero for enet_host_create to work.

Ismoh commented 2 years ago

It was simpler than I thought. Check your firewall rules. After I allowed everything to the port I used, host wasn't nil anymore!

leafo commented 2 years ago

host is nil because the call to host_create failed and returned nil. In Lua it's typically for functions to return two values on error, nil as the first and a string describing the error. There can be many reasons why host might fail to create, the most common is that something else is already listening on that port.

Hope that helps.