connection over the internet using sock.lua

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
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

connection over the internet using sock.lua

Post 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
-
User avatar
zorg
Party member
Posts: 3087
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: connection over the internet using sock.lua

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: connection over the internet using sock.lua

Post 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?
-
User avatar
ivan
Party member
Posts: 1722
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: connection over the internet using sock.lua

Post 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.
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: connection over the internet using sock.lua

Post 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?
-
User avatar
ivan
Party member
Posts: 1722
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: connection over the internet using sock.lua

Post 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.
User avatar
zorg
Party member
Posts: 3087
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: connection over the internet using sock.lua

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests