Multiplayer not binding to server Fedora Linux

Godot Version

Godot 4.3 Stable official on Fedora Linux

Question

When I try and create a server, either it doesn't correctly bind to IP, or the client fails to connect, but both are giving me OK responses.

So I have a basic 3D game in Godot, and I was trying to add multiplayer. However, something is going wrong when I try and run the multiplayer. This is on top of a 3d game, but the first script uses command line arguments to determine which one of these scripts to load, and they load successfully.
Multiplayer script

extends Node

var server_peer: ENetMultiplayerPeer

func _ready() -> void:
	server_peer = ENetMultiplayerPeer.new()

	var result = server_peer.create_server(1245, 10)

	if result == OK:
		print("Server started successfully on port 12345")

		# Correct way to assign the network_peer in Godot 4
		multiplayer.multiplayer_peer = server_peer
		get_tree().set_multiplayer(multiplayer)

		# Connect the signals to handle peer connections
		server_peer.connect("peer_connected", Callable(self, "_on_peer_connected"))
		server_peer.connect("peer_disconnected", Callable(self, "_on_peer_disconnected"))
	else:
		print("Failed to start server. Error code:", result)

func _on_peer_connected(peer_id: int) -> void:
	print("Peer connected with ID:", peer_id)

func _on_peer_disconnected(peer_id: int) -> void:
	print("Peer disconnected with ID:", peer_id)

func _process(delta: float) -> void:
	if server_peer:
		server_peer.poll()  # Make sure to poll the server for events

Client script:

extends Node

var server_peer: ENetMultiplayerPeer

func _ready() -> void:
	server_peer = ENetMultiplayerPeer.new()

	var result = server_peer.create_server(1245, 10)

	if result == OK:
		print("Server started successfully on port 12345")

		# Correct way to assign the network_peer in Godot 4
		multiplayer.multiplayer_peer = server_peer
		get_tree().set_multiplayer(multiplayer)

		# Connect the signals to handle peer connections
		server_peer.connect("peer_connected", Callable(self, "_on_peer_connected"))
		server_peer.connect("peer_disconnected", Callable(self, "_on_peer_disconnected"))
	else:
		print("Failed to start server. Error code:", result)

func _on_peer_connected(peer_id: int) -> void:
	print("Peer connected with ID:", peer_id)

func _on_peer_disconnected(peer_id: int) -> void:
	print("Peer disconnected with ID:", peer_id)

func _process(delta: float) -> void:
	if server_peer:
		server_peer.poll()  # Make sure to poll the server for events

Output:

Server started successfully on port 12345
-90
0
90
Connected to server

Since both of these run at the same time when I click the run button, I have tried delaying the client connecting to the server, but that proves fruitless.
I have also tried netstating it, and the output shows that something exists, and dissappears when the server stops, but for some reason the client can’t connect? I have also tried changing the port, and I disabled Fedora’s protection, sudo setenforce 0, and let it through the firewall, sudo firewall-cmd --zone=public --add-port=1245/tcp --permanent, and yes, I reloaded the firewall afterwards.
NetStat output:

udp6       0      0 :::1245                 :::*                               

Thank you!

Your client script also creates a server

How does it do that?

Your code says so

2 Likes

I attached the wrong code for the client, sorry.
Here’s the correct code.

extends Node
func _ready() -> void:
	await get_tree().create_timer(2).timeout
	var peer = ENetMultiplayerPeer.new()
	var result = peer.create_client("127.0.0.1", 1245)
	if result == OK:
			get_tree().set_multiplayer(peer)
			print("Connected to server")
	else:
		print("Failed to connect to server")

That all looks fine, but you shouldnt need to pull the server manually.

The defualt behavior is the SceneTree mainloop will do that for you. (Unless otherwise configured)

Also

This doesnt necessarily mean you connected to the server, only that you successfully created the client.

you should connect the signal connected_to_server

How would I go about doing that?

Im not sure exactly what you mean so ill take a guess on polling behavior.

1 Like

I mean actually connect to the server, since you said I was not.

This doesnt necessarily mean you connected to the server, only that you successfully created the client.

you should connect the signal connected_to_server

I mean you already connect the client signals in the server, its the same for connecting the server signals in the client.

Oh haha, you are connecting to signals that dont exist

This should be

#server
# Connect the signals to handle peer connections
		multiplayer.connect("peer_connected", Callable(self, "_on_peer_connected"))
		multiplayer.connect("peer_disconnected", Callable(self, "_on_peer_disconnected"))

I prefer this syntax as you can get auto completion and compile time checking

#client
multiplayer.connected_to_server.connect(func():print("client connected to server"))
1 Like

res://multiplayer/server.gd:20 - Parse Error: Name "connected_to_server" called as a function but is a "Signal"

I’m sorry if I’m just being stupid, I am new to Godot

extends Node

var server_peer: ENetMultiplayerPeer

func _ready() -> void:
	server_peer = ENetMultiplayerPeer.new()

	var result = server_peer.create_server(1245, 10)

	if result == OK:
		print("Server started successfully on port 12345")

		# Correct way to assign the network_peer in Godot 4
		multiplayer.multiplayer_peer = server_peer
		get_tree().set_multiplayer(multiplayer)

		# Connect the signals to handle peer connections
		multiplayer.connect("peer_connected", Callable(self, "_on_peer_connected"))
		multiplayer.connect("peer_disconnected", Callable(self, "_on_peer_disconnected"))
		multiplayer.connected_to_server(func():print("client connected to server"))
	else:
		print("Failed to start server. Error code:", result)

func _on_peer_connected(peer_id: int) -> void:
	print("Peer connected with ID:", peer_id)

func _on_peer_disconnected(peer_id: int) -> void:
	print("Peer disconnected with ID:", peer_id)

func _process(delta: float) -> void:
	if server_peer:
		server_peer.poll()  # Make sure to poll the server for events

Sorry i made a typo check it again i updated it

This^^

1 Like

Still nothing prints to indicate it successfully connected.
I get this error on the client side though,

E 0:00:03:0060   client.gd:8 @ _ready(): Condition "!p_multiplayer.is_valid()" is true.
  <C++ Source>   scene/main/scene_tree.cpp:1540 @ set_multiplayer()
  <Stack Trace>  client.gd:8 @ _ready()

why would multiplayer be disabled? This is the first bit of code it runs.

connected_to_server is only emitted on the client, it’s for when a client connects to a server, you are connecting this signal on the server, not the client.

extends Node
func _ready() -> void:
	await get_tree().create_timer(2).timeout
	var peer = ENetMultiplayerPeer.new()
	var result = peer.create_client("127.0.0.1", 1245)
	if result == OK:
		get_tree().set_multiplayer(peer)
		multiplayer.connected_to_server.connect(print.bind("Client connected to server"))
		multiplayer.connection_failed.connect(print.bind("Client failed to connect"))
		# print("Connected to server")
	else:
		print("Failed to connect to server")

I’m getting the same error

E 0:00:08:0493   client.gd:7 @ _ready(): Condition "!p_multiplayer.is_valid()" is true.
  <C++ Source>   scene/main/scene_tree.cpp:1540 @ set_multiplayer()
  <Stack Trace>  client.gd:7 @ _ready()

I’ll put my current code here.
Client:

extends Node
func _ready() -> void:
	await get_tree().create_timer(2).timeout
	var peer = ENetMultiplayerPeer.new()
	var result = peer.create_client("127.0.0.1", 1245)
	if result == OK:
		get_tree().set_multiplayer(peer)
		multiplayer.connected_to_server.connect(print.bind("Client connected to server"))
		multiplayer.connection_failed.connect(print.bind("Client failed to connect"))
		# print("Connected to server")
	else:
		print("Failed to connect to server")

Server:

extends Node

var server_peer: ENetMultiplayerPeer

func _ready() -> void:
	server_peer = ENetMultiplayerPeer.new()

	var result = server_peer.create_server(1245, 10)

	if result == OK:
		print("Server started successfully on port 12345")

		# Correct way to assign the network_peer in Godot 4
		multiplayer.multiplayer_peer = server_peer
		get_tree().set_multiplayer(multiplayer)

		# Connect the signals to handle peer connections
		#multiplayer.connect("peer_connected", Callable(self, "_on_peer_connected"))
		#multiplayer.connect("peer_disconnected", Callable(self, "_on_peer_disconnected"))
		multiplayer.connected_to_server.connect(func():print("client connected to server"))
	else:
		print("Failed to start server. Error code:", result)

func _on_peer_connected(peer_id: int) -> void:
	print("Peer connected with ID:", peer_id)

func _on_peer_disconnected(peer_id: int) -> void:
	print("Peer disconnected with ID:", peer_id)

func _process(delta: float) -> void:
	if server_peer:
		server_peer.poll()  # Make sure to poll the server for events

Output:

Server started successfully on port 12345
-90
0
90

Why do these defer? Try only setting multiplayer.multiplayer_peer for server and client, I don’t think you need to use set_multiplayer().

extends Node
func _ready() -> void:
	await get_tree().create_timer(2).timeout
	var peer = ENetMultiplayerPeer.new()
	var result = peer.create_client("127.0.0.1", 1245)
	if result == OK:
		multiplayer.multiplayer_peer = peer
		multiplayer.connected_to_server.connect(print.bind("Client connected to server"))
		multiplayer.connection_failed.connect(print.bind("Client failed to connect"))
		# print("Connected to server")
	else:
		print("Failed to connect to server")

That sort of works, I get the message from the client it connected to the server, but the server doesn’t print anything.

Server started successfully on port 12345
-90
0
90
Client connected to server

I would bet that’s because the server’s signals are commented out. Also would recommend using 4.x signal connections

multiplayer.peer_connected.connect(_on_peer_connected)
multiplayer.peer_disconnected.connect(_on_peer_disconnected)

My god I feel stupid right now, I’m really sorry.