Getting into networking

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
JoeJoeTV
Prole
Posts: 5
Joined: Fri May 18, 2018 1:41 pm

Getting into networking

Post by JoeJoeTV »

Hello everybody,
I wanted to try networking in lua and i don't know where to start, so i wanted to ask here if anyone can maybe help me or give me any directions.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Getting into networking

Post by raidho36 »

Networking is hard, and the only easy ways of doing it are the ones that don't work half the time. LÖVE ships with luasocket and lua-enet, both of which are quite barebones. The former is a binding for OS socket-type data ports, nowdays mostly used for internet communication. The latter is a basic wrapper over UDP socket functionality. Neither are particularly high level, and neither have anything special about them, they're industry standard.

So you can read up on general networking using sockets, TCP and UDP from any sources. If you intend to use UDP p2p, make sure to read on NAT punching.
JoeJoeTV
Prole
Posts: 5
Joined: Fri May 18, 2018 1:41 pm

Re: Getting into networking

Post by JoeJoeTV »

First, thanks for the answer.
I am just starting and trying things out.
I have wrote a basic client and server with udp(because i didn't get it to work with tcp):

Client:

Code: Select all

local socket = require("socket")

local udp = socket.udp()

print("Please enter address:")
local address = io.read()
print("Please enter port:")
local port = tonumber(io.read())

if address == "" then
    address = "localhost"
end
if port == nil then
    port = 1337
end

local timeout = 0


udp:settimeout(0)
udp:setpeername(address, port)

print("ADDRESS: " .. tostring(address))
print("PORT: " .. tostring(port))
print("TIMEOUT: " .. tostring(timeout))


print("\nSending login data...")

local dg = "LOGIN: TESTUSER"
udp:send(dg)

print("Sent " .. tostring(dg))

local running = true

function receivedata()
    local rdata, rmsg
    repeat
        rdata, rmsg = udp:receive()
        if rdata then
            print("Received Data: " .. rdata)
        elseif rmsg ~= "timeout" then
            print("Network error: " .. tostring(rmsg))
        end
    until not rdata
end

while running do
    print("\nEnter Command(exit to exit):")
    local cmd = io.read()
    if cmd == "exit" then
        running = false
    elseif cmd and type(cmd) == "string" then
        udp:send(cmd)
    end
    
    socket.sleep(0.01)
    receivedata()
end

print("Server stopped!")
Server:

Code: Select all

local socket = require("socket")

local udp = socket.udp()

print("Please enter address:")
local address = io.read()
print("Please enter port:")
local port = tonumber(io.read())

if address == "" then
    address = "localhost"
end
if port == nil then
    port = 1337
end

local timeout = 0

udp:settimeout(timeout)
udp:setsockname(address,port)

local data, msg_or_ip, port_or_nil
local cmd

local running = true

print("Address: " .. tostring(address))
print("Port: " .. tostring(port))
print("Timeout: " .. tostring(timeout))
print("\nStarting main server loop...\n")

while running do
    data, msg_or_ip, port_or_nil = udp:receivefrom()
    if data then
        print("Received Data from ip " .. msg_or_ip .." on port " .. port_or_nil .. ": " .. data)
        if data == "sendreply" then
            udp:sendto("REPLY",msg_or_ip,port_or_nil)
            print("Sent reply to " .. msg_or_ip .. ":" .. port_or_nil)
        elseif data == "quit" then
            running = false
        end
    elseif msg_or_ip ~= "timeout" then
        error("NETWORK ERROR: " .. tostring(msg_or_ip))
    end

    socket.sleep(0.01)
end

print("END")
This code works perfectly on my local machine, but when i put it on a linux vserver it only recieves the "login" message and the other ones sent by the client aren't received. But when i close the client and reopen it, the server receives the login message again.
I tried the same cocept with tcp but i dindn't get it to work right so i tried it with udp and got better results.

P.S. Sorry for my bad english sometimes i am german
JoeJoeTV
Prole
Posts: 5
Joined: Fri May 18, 2018 1:41 pm

Re: Getting into networking

Post by JoeJoeTV »

Can anyone help me with that?
I also tried it with tcp but got simmillar problems.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Getting into networking

Post by raidho36 »

It appears not many people here have such degree of experience to answer this (or they don't care to do it). Because it's a "generic" networking question you can ask it on other, regular programming forums.
JoeJoeTV
Prole
Posts: 5
Joined: Fri May 18, 2018 1:41 pm

Re: Getting into networking

Post by JoeJoeTV »

Yeah, i guess.
But i have now solved many problems myself which i didn't think i could.
I will try asking on stackoverflow or something.
User avatar
wazoowazoo
Prole
Posts: 16
Joined: Sun Jan 22, 2017 2:47 pm

Re: Getting into networking

Post by wazoowazoo »

Could you please post the fix if you found one ?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 48 guests