Need help with lua-enet

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nuthen224
Citizen
Posts: 50
Joined: Sun Jul 28, 2013 9:40 pm

Need help with lua-enet

Post by Nuthen224 »

Disclaimer: I'm not very experienced with networking, I'm just trying to get a simple network connection based on the tutorials on the lua-enet website (http://leafo.net/lua-enet/)

The code on that website runs using loops, so I modified it to work with love's structure instead. I'm pretty confident that the issue isn't with that, as I'm still able to run both the server on my computer and the client on my computer, and it works just as I had intended. The issue has to do with others trying to connect to the server. In all of the examples I've seen for lua-enet, including examples made in love, the address of the server is marked as "localhost:<port>". I don't see how this allows a client to connect to a server, wouldn't the external IP also need to be provided, at least to the client? I've tried replacing the localhost part with my external IP, and that results in an error when I try to launch the client.

The port I'm using is forwarded properly, and it's a UCP port, which is what I'm assuming ENet uses based on what I've read about it, though I could be totally wrong on that.

Here is the code I'm using for the server:

Code: Select all

require "enet"

function love.load()
    host = enet.host_create "localhost:6789"
    
    hostReceived = 0
end

function love.draw()
    if hostReceived == 1 then
        love.graphics.print("Got message: ", 5, 5)
    end
end

function love.update(dt)
    event = host:service(100)
    if event and event.type == "receive" then
        hostReceived = 1
        event.peer:send(event.data)
    end
end
And here is the code for the client:

Code: Select all

require "enet"

function love.load()
    connected = false
    messageReceived = false
    
    host = enet.host_create()
    
    server = host:connect("localhost:6789")
    
    continue = true
end

function love.draw()
    if connected == true then
        love.graphics.print("Connected to", 5, 5)
    end
    
    if messageReceived == true then
        love.graphics.print("Got message: ", 5, 10)
    end
end

function love.update(dt)
    if continue == true then
        event = host:service(100)
        if event then
            if event.type == "connect" then
                connected = true
                event.peer:send("hello world")
            elseif event.type == "receive" then
                messageReceived = true
                continue = false
                
                server:disconnect()
                host:flush()
            end
        end
    end
end
Any sort of help with this would be appreciated.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Need help with lua-enet

Post by MadByte »

Hi ,

I can't say much about it, but there was a related topic a couple of days ago.
Maybe that help somehow.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Need help with lua-enet

Post by bartbes »

On the server-side you need to listen to "*:6789", meaning "all ip addresses and network interfaces on port 6789", then on client you need to use the ip that routes to the host from the client. If it's over the internet, that means your external ip (if the UDP port has been forwarded correctly), if it's over LAN, that means your LAN ip. (Some routers don't correctly route to the external ip if you're within the same LAN.)
User avatar
Nuthen224
Citizen
Posts: 50
Joined: Sun Jul 28, 2013 9:40 pm

Re: Need help with lua-enet

Post by Nuthen224 »

@MadByte Yeah I saw that thread, but in the example localhost is used for both the server and client, which was my main issue. No doubt there is a lot of nifty things in that example though.

@bartbes Yes that's exactly what I needed, thank you very much! Working like a charm.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 46 guests