TCP stays at "StreamPeerTCP.STATUS_CONNECTING"

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m trying to connect a Godot client to a TCP server. The client script (below) never becomes STATUS_CONNECTED and stays STATUS_CONNECTING

Is there something else (weird) I need to do to get Godot to connect to not-Godot TCP sockets?

GDScript below, gh gist links to the server and a working test client

extends Node

var _status
var _socket := StreamPeerTCP.new()
var _connection_timer := 0.0
var _connection_timeout := 10.0  # 10 seconds timeout

var host: String = "192.168.0.51"
var port: int = 6060

func _ready():
	
	_status = _socket.get_status()
	print("_status = ", _status)
	connect_to_server()

func connect_to_server():
	var err = _socket.connect_to_host(host, port)
	if err == OK:
		print("Connection call was successful, waiting to establish connection...")
		_socket.set_no_delay(true)
	else:
		print("Failed to connect to server. Error: ", err)

	_connection_timer = 0.0  # Reset timer to track the connection duration

func _process(_delta: float):
	
	# check the status and log any change
	var status = _socket.get_status()
	var changed = status != _status
	_status = status
	if changed:
		print("The status is now ", _status)
	
	if StreamPeerTCP.STATUS_CONNECTED != _status:
		_connection_timer += _delta
		assert(
			_connection_timer <= _connection_timeout,
			"Connection attempt timed out after 10 seconds."
		)
	else:
		
		_connection_timer = 0.0
		handle_incoming_data()
	
	


func handle_incoming_data():
	var available = _socket.get_available_bytes()
	while available > 0:
		var was = available
		var byte = _socket.get_u8()
		available = _socket.get_available_bytes()
		if available == was:
			print("No byte consumed - panicking")
			return

… I’ve tried skipping these checks, but, the client doesn’t read data in that case …

server Main.scala Main.scala · GitHub

test client that works Main.scala · GitHub

Can you run your Scala script on the same computer and also with “192.168.0.51”? If it doesn’t work either, then it’s often due to a firewall.


object NoNothingClient {
	def main(args: Array[String]): Unit = {
		val client = new Socket("127.0.0.1", 6060)

yeah - 127.0.0.1 didn’t work with bare sockets, so, i tried my LAN IP before pasting the script. Oops.

yeah it’s all running otherwise happily on my local PC. I have told Windows Firewall to let all traffic in and out on 6060.

i’ve tried using http-connections and that usually works but takes around 50 seconds if I’m trying to reach an http server that’s not 127.0.0.1

It’s so weird. The lag isn’t happening for non-Godot connections.

(all http connections here)

  • slow ; localhost:6060
  • fast ; godotengine.org/
  • fast ; 127.0.0.1:6060
  • slow ; 192.168.0.51:6060

20 years ago a lecturer said “Http is the greatest protocol to send 10x as much data while still using TCP” but given the level of attention I can devote to this … I guess I’ll stick with HTTP.

I found this example but Godot 3. After porting it to Godot 4 I couldn’t get it to work yet. If I find more time, I’ll check with Godot 3 first.

Connecting to 45.79.112.203:4242
Connecting to host.

this public Testserver: https://tcpbin.com/

use StreamPeerTCP.poll() before use StreamPeerTCP.get_status()
It can reflash status