Server can't be acessed by different computers

Godot Version

Godot 4.4.1

Question

I’ve been trying to create a multiplayer for a while now, but I’m just a beginner. I cannot understand why my friend, using a computer from his house, is unable to connect to a server that I am hosting. However, all attempts at establishing connection work when I’m in the same computer. Unfortunately, I do not have another computer to confirm if it is only working on LAN.

Code

var peer = ENetMultiplayerPeer.new()
var plr_obj: PackedScene = load("res://obj/plr/plr.tscn")

# SERVER

func _on_host_pressed() -> void:
	# We get this fresh peer and create a server with it!
	peer.create_server(7000)
	# Then, we make this peer the one we use to establish RPC with others!
	multiplayer.multiplayer_peer = peer
	
	# If any peer connects to our server, the connected function is called!
	multiplayer.peer_connected.connect(_add_player)
	# We add our client peer to the server!
	_add_player()
	

func _add_player(id = 1):
	# Create our new player!
	var plr = plr_obj.instantiate()
	# Give it a name based on its peer ID!
	plr.name = str(id)
	# And, when everything is processed, we slip him in the server.
	call_deferred("add_child", plr)

func _on_join_pressed() -> void:
	# We create a client using the same port (since this is an example)
	peer.create_client("", 7000)
	# This becomes the peer that we use to establish RPC with others!
	multiplayer.multiplayer_peer = peer

Did you forward your port 7000 so it is accessable by those outside your network? You can test if “7000” is accessable with this online tool

That is a possible answer. Could you give me some directions on how I would be able to forward the port? I have been searching for a while but to no avail.

Depends on your router, usually you will go to http://192.168.1.1 enter your password and dig around for “port forwarding”, “firewall”, or “NAT”. Enter the port you want to forward and the device’s local ip you want to forward to. Do you know your local ip address? Usually starts with 192.168.x.x for home networks, that’s what you’ll enter as the port’s target/destination address.

Yes, I do. However, isn’t there a way to forward my port only by code? I’ve seen methods on how do it manually but, ideally, I want the player not to have the need for such work. Plus, right now, I’m not able to possess privileged access to the router, so that might make it even more difficult.

I’ve been trying to do so by UPNP, and it seems be working, until I use the site you just sent. When I try to check if the port is open, they always say no. However, I’m only keeping it open for 10 seconds, so maybe that influences it?

Update: Messing with UPNP, I’ve been trying for some time now opening my port by UPNP and, with another device, accessing the connection made by TCP. When I check the ports with my CMD, it tells me that I am using them. However, the sites tell me the port is closed, and the other device is unable to establish any connection.

I was trying to use a mobile connection before, so I figured that was the issue regarding opening ports. But now that I am truly using a proper router, it just seems like my laptop THINKS it is open.

Additionally, sorry for the overflow of info, I just wanted to try making the connections even through mobile internet? I’ve seen some games like Soul Knight and wondered if I could do it like that.

UPnP is blocked by default for many routers, I’ve seen a lot of theory on STUN — a relay server being used to avoid port forwarding or at least help get the two machines talking like a matchmaking server.

This blog by @CabalCrow shows surprisingly basic UDP usage, I still haven’t tested it myself and I’m not sure how it works; Godot’s docs state little about the local_port setting used.

If local_port is specified, the client will also listen to the given port; this is useful for some NAT traversal techniques.

Specifically this Godot sample

# machine1
var peer = ENetMultiplayerPeer.new()
var local_port = 9999 # the port we punched
peer.create_server(port, 1)
multiplayer.multiplayer_peer = peer

# machine2
var peer = ENetMultiplayerPeer.new()
var local_port = 9998 # the port we punched
var machine1_punched_port = 9999
var machine1_ip := "213.213.213.213"
peer.create_client(machine1_ip, machine1_punched_port, 0, 0, 0, local_port)
multiplayer.multiplayer_peer = peer
1 Like

UPnP would be how to request opening the port through code; the router still has to open the port and direct traffic from the outside world to your computer.

Good finding UPnP, I’m not sure how long it would need to be open to take effect. You can open ports for TCP and/or UDP specifically, maybe this site only checks for TCP (seems to be the case), but Godot is only going to use UDP. Hard to find an online checker for UDP specifically.

If you mean like 4g/5g phone networks those are going to be much more locked down, no way you are getting a server public on one of those.

Okay, thank you for the great help! I will attempt to make a working prototype soon.

You could potentially port forward using only code, but this all depends on the router settings and nat types. UPnP works well as long as at least 1 player has it enabled on their router (you can test for that). However many people don’t have that enabled on their router or don’t even have the option to.
You second best option is IPv6 hole punching, if both players have IPv6, you can connect players quite consistently even WITHOUT a signaling server. However not everyone have IPv6.
The other thing is using a signaling server which would work for most people. It would not work for symmetric nat though, which is a non negligible % of people online.

Really you need to combine all methods, test what both players have and connect them based on that. I’ve explained in details, how to set this up in the blog post @gertkeno posted. If there is something you don’t understand and need help setting up hmu and I will help you with the specifics.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.