Page 1 of 1

e-net: host not working with external IP adress on port forwarding

Posted: Fri Jun 14, 2019 10:31 am
by ThatBonk
Whenever I try to use my external IP adress to let people from outside connect to my small chat program example, I get the error:

Error

main.lua:20: attempt to index global 'host' (a nil value)

Traceback

main.lua:20: in function 'update'
[C]: in function 'xpcall'

-> I've tried looking for a solution and literally nothing worked that was about the topic of letting other people connect with my external IP into my network.

Everything else works just fine. Using my IPV4 builds a connection, localhost builds a connection, using a star will only let my IPV4 connect and even using my Hamachi IP works just fine with other people.

Summary:

-> Yes, I have port forwarded 11000
-> connecting locally works fine.
-> External IP crashes the server

Code:

main.lua Server:

Code: Select all

function love.load()
  require "serverFunctions"
  enet = require "enet"
  host = enet.host_create"(My external IP goes here):11000"
  
  clients = {}
end

function love.update(dt)
  event = host:service(0)
  while event do 
    if event.type == "receive" then
      print("Someone sent a message: ", event.data, event.peer)
      sendToAllClients(event.data)
    end
    connectionHandler()
    event = host:service()
  end
end

-- Outside of main --

function split(s, delimiter)
	result = {}
	for match in (s..delimiter):gmatch("(.-)"..delimiter) do
		table.insert(result, match)
	end
	return result
end

function tablefind(tab,el)
  for index, value in pairs(tab) do
    if value == el then
      return index
    end
  end
end

function connectionHandler()
    if event.type == "connect" then
      print(event.peer, "connected.")
      table.insert(clients, event.peer)
      for i,v in ipairs(clients) do
        print("Client: " .. i .. " ", v)
      end
    end
      
    if event.type == "disconnect" then
      print(event.peer, " disconnected.")
      table.remove(clients, tablefind(clients, event.peer))
      for i,v in ipairs(clients) do
        print("Client: " .. i .. " ", v)
      end
    end
end

function sendToAllClients(data)
  for i = 1, #clients, 1 do
    clients[i]:send(data)
  end
end

main.lua Client:

Code: Select all

function love.load()
  
  utf8 = require("utf8")
  enet = require "enet"
  host = enet.host_create()
  server = host:connect("ExternalIP:11000")
  
  done = false
  stringpacket = "Nothing"
  inputText = "Type here."
  receiveText = "Nothing yet."
  
  connected = false
  
end

function love.update()
  
  event = host:service(100)
    if event then
      
      if event.type == "connect" then
        print("Connected to", event.peer)
        hostPeer = event.peer
        connected = true
      end
      
      if event.type == "receive" then
        receiveText = event.data
      end
      
    end
end

function love.keypressed(key)
  
  if connected == true then
    if key == "return" then
      hostPeer:send(inputText)
      inputText = ""
      print("Text cleared")
    end
    if key == "s" then
        hostPeer:disconnect()
    end
  end
  
  if key == "backspace" then
    byteoffset = utf8.offset(inputText, -1)
      if byteoffset then
        inputText = string.sub(inputText, 1, byteoffset - 1)
      end
  end
end

function love.textinput(input)
    inputText = inputText .. input
end

function love.draw()
  love.graphics.print(inputText, 10, 10, 0, 1)
  love.graphics.print(receiveText, 10, 30, 0, 1)
end

-- Outside of main --

function split(s, delimiter)
	result = {}
	for match in (s..delimiter):gmatch("(.-)"..delimiter) do
		table.insert(result, match)
	end
	return result
end
Thanks in advance for any help!

Re: e-net: host not working with external IP adress on port forwarding

Posted: Wed Jun 26, 2019 3:31 am
by Мэтю
It's not working because you're using a "invalid" IP to listen to in your machine. Your router must manage NAT, so your router must have a public IP and any device connected to it have a private IP. Your computer have a private IP, so there's no such interface with your public (external) IP. Create your server listening to your private IP, and configure your router to forward the traffic from port 11000 to your private IP, so people can use your public IP to connect to your server.

Later on try searching a bit about NAT.

Re: e-net: host not working with external IP adress on port forwarding

Posted: Tue Jul 09, 2019 1:34 pm
by ThatBonk
Мэтю wrote: Wed Jun 26, 2019 3:31 am It's not working because you're using a "invalid" IP to listen to in your machine. Your router must manage NAT, so your router must have a public IP and any device connected to it have a private IP. Your computer have a private IP, so there's no such interface with your public (external) IP. Create your server listening to your private IP, and configure your router to forward the traffic from port 11000 to your private IP, so people can use your public IP to connect to your server.

Later on try searching a bit about NAT.
I am not sure of what you mean.. I mentioned that I port forwarded my port 11000 on my router. When I insert my IPV4/local IP with Port 11000 as host, I won't receive Data.

Do you mean, that there should be a second rule that says: PublicIP:11000 must give data to IPV4:11000 ?

Re: e-net: host not working with external IP adress on port forwarding

Posted: Tue Jul 09, 2019 2:55 pm
by PGUp
just use this dude, it's an enet wrapper and it's much more simple than enet
https://github.com/camchenry/sock.lua