StreamPeerTCP.connect_to_host never connects

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I tried to connect to a server using the following code:

extends Node

var irc: StreamPeerTCP = StreamPeerTCP.new()
var thread: Thread

func _ready():
	thread = Thread.new()
	thread.start(connect_to_server.bind("irc.libera.chat", "#bottesting", "SparkwavesTestClient"))

func connect_to_server(server: String, channel: String, nickname: String):
	print("Connecting to " + server + ":6667")
	irc.connect_to_host(server, 6667)
	
	while irc.get_status() == StreamPeerTCP.STATUS_CONNECTING:
		pass
		
	print("You will never see this")

The console never prints the last print line, and .get_status() is permanently stuck on StreamPeerTCP.STATUS_CONNECTING. If I do not put it in a thread the entire application freezes instead. I tried connecting using ncat instead, and it connected instantly. It does this no matter what kind of URL I connect to as long as it’s a valid URL, even localhost. An invalid URL exits correctly.

Try putting await get_tree().process_frame in the while loop instead of pass

1 Like

Your while loop does not allow the StreamPeerTCP to advance it’s own logic, so it will always return the same thing, thus it will halt.

I figured out what to do. The solution is to replace the pass with irc.poll(). That fixes this issue.