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