Page 1 of 1

connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 9:17 am
by PGUp
I'm using sock.lua library networking.. https://github.com/camchenry/sock.lua
so from my knowledge, to achieve connection over the internet, the server needs it's public ip adress/localhost and the client need to connect over that ip adress...

this is the server code

Code: Select all

local sock = require 'sock'
function love.load()
    server = sock.newServer("localhost", 22122)
    server:on("connect", function(data, client)
        local msg = "Hello from the server!"
        client:send("hello", msg)
    end)
end

function love.update(dt)
    server:update()
end
and this is the client code

Code: Select all

function love.load()
    local serverPublicIp = "125.162.82.41"
    client = sock.newClient(serverPublicIp, 22122)
    client:on("connect", function(data)
        print("Client connected to the server.")
    end)
    client:connect()
end

function love.update(dt)
    client:update()
end
I tested both code on the same machine simultaneously, there is no error.. nothing.. the message was never transferred
and I tested to change the

Code: Select all

server = sock.newServer("localhost", 22122)
to

Code: Select all

server = sock.newServer("125.162.82.41", 22122)
it still wont work, I have no idea what went wrong, I need to know how to connect to other machine over the internet

Re: connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 9:24 am
by zorg
The github page gives examples for the server, and that has this one line in it:

Code: Select all

server = sock.newServer("*", 22122)
Maybe try it like that? :3

Re: connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 9:33 am
by PGUp
zorg wrote: Tue Sep 24, 2019 9:24 am The github page gives examples for the server, and that has this one line in it:

Code: Select all

server = sock.newServer("*", 22122)
Maybe try it like that? :3
I gave it a try, the server and client never communicate, is it because I am on the same machine testing it though?

Re: connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 9:43 am
by ivan
Check out my humble tutorial using just Lua socket:
https://2dengine.com/?p=networking
By setting its address to "*" the server will be able to communicate with multiple clients
Note that Lua socket won't work over the internet when one or both machines are behind a NAT device such as a home router.

Re: connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 9:58 am
by PGUp
ivan wrote: Tue Sep 24, 2019 9:43 am Check out my humble tutorial using just Lua socket:
https://2dengine.com/?p=networking
By setting its address to "*" the server will be able to communicate with multiple clients
Note that Lua socket won't work over the internet when one or both machines are behind a NAT device such as a home router.
I didnt know that.. so how does multiplayer games work? they connect over the internet, even when you are using a router.

I'm using sock.lua, it's a wrapper around enet. is it possible to connect over the internet using your router with enet though?

Re: connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 10:28 am
by ivan
Routers perform network addresses translation (NAT) so that all of your devices at home can "share" one public IP on the internet.
so how does multiplayer games work?
It depends. For peer-to-peer connections using UDP you have to use a technique called "hole punching". From what I understand there are a few reliable libraries for "hole punching", but none of them in Lua.

Re: connection over the internet using sock.lua

Posted: Tue Sep 24, 2019 10:53 am
by zorg
You can implement a hole-punching algorithm in lua too (you'll need a publicly reachable server though), although in a few cases, that still doesn't guarantee success (Symmetric NAT-s, multi-level NAT without hairpin support) in which cases, comms could only go through a central/mediating server, as far as i know.