How to establish a connection to a network peer via an external IP address?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RaebaldKashban

Hello all,

I’ve been mucking around with Godot’s high level networking API and managed to get a simple LAN chat room working. When I tried to connect via an external IP and port forwarding, however, the connection fails. Here’s my current setup:

Server code:

extends Node

var host_ip
var port = 42424

var registered_users = {}

func _ready():
	get_node("ServerConfigWindow/VBoxContainer/Button").connect("pressed",self,"_on_host_button_pressed")
	
func _on_peer_connected(peer_id):
	print(str(peer_id) + " connected.")

func _on_peer_disconnected(peer_id):
	print(str(peer_id) + " disconnected.")

func host_server():
	var network = NetworkedMultiplayerENet.new()
	network.connect("peer_connected",self,"_on_peer_connected")
	network.connect("peer_disconnected",self,"_on_peer_disconnected")
	if network.create_server(port,2) != OK:
		print("Failed to create server. Aborting....")
		yield(get_tree().create_timer(1.2),"timeout")
		get_tree().quit()
	get_tree().set_network_peer(network)
	
	host_ip = get_node("ServerConfigWindow/VBoxContainer/LineEdit").text
	
	network.set_bind_ip(host_ip)
	print("Server initialized.\nIP: " + host_ip + "\nPort: " + str(port))

func _on_host_button_pressed():
	host_server()
	remove_child(get_node("ServerConfigWindow"))
	add_child(load("res://ChatWindow.tscn").instance())
	pass

remote func register_user(peer_id, username):
	registered_users[peer_id] = username
	get_node("ChatWindow").print_text(peer_id, "(has joined the chat)")

func get_registered_users():
	return registered_users

Client code:

extends Node

var host_ip
var port = 42424

func _ready():
	get_node("ConnectWindow/VBoxContainer/Button").connect("pressed",self,"_on_connect_button_pressed")

func _on_connection_succeeded():
	print("Connected to server at " + host_ip + ":" + str(port))
	rpc_id(1,"register_user",str(get_tree().get_network_unique_id()),get_node("ConnectWindow/VBoxContainer/LineEdit2").text)
	remove_child(get_node("ConnectWindow"))
	add_child(load("res://ChatWindow.tscn").instance())

func _on_connection_failed():
	print("Failed to connect to server at " + host_ip + ":" + str(port))

func connect_to_server():
	print("Trying to connect....")
	var client = NetworkedMultiplayerENet.new()
	client.connect("connection_succeeded",self,"_on_connection_succeeded")
	client.connect("connection_failed",self,"_on_connection_failed")
	if client.create_client(host_ip,port) != OK:
		print("Failed to connect to server at " + host_ip + ":" + str(port))
	get_tree().set_network_peer(client)

func _on_connect_button_pressed():
	host_ip = get_node("ConnectWindow/VBoxContainer/LineEdit").text
	connect_to_server()

The IP address gets determined by user input. When I connect to the server using an IP address my LAN can see, everything works fine. When I try to connect with the IP that the external network sees, the connection fails. Note that I do have port forwarding set up for the port 42424. Any ideas why I might be running into this issue?

does seems to be an issue with your entwork. can descripe your setup more detailed? where do your computers are connected to?

klaas | 2020-10-04 09:24

I realize this is a very old question but will offer a possible explanation. You appear to be attempting to connect to your ISP assigned public IP. In many cases, ISPs intentionally (or accidentally) block such attempts in the interest of security. It prevents customers on the same sub-net from launching hack attempts against their neighbors. Most, but not all, modern firewall/routers recognize out-going traffic destined for its own public IP and will loop-back internally. The catch is that the the out-bound and in-bound ports cannot be the same.

tx350z | 2022-06-19 11:08