The question of transferring data over TCP/IP from TouchDesigner to Unity

Good evening, I tried to transfer x_position position data via TCP/IP here, I made a code, it does not give errors anywhere, but the movement is not transmitted to Unity

here is the code on the object in unity:

using System;
using System.Net
using System.Net.Sockets;
using System.Text;
using UnityEngine;

public class TcpClientReader : MonoBehaviour
{
public string ipAddress = "IP Computer";
public int port = 12345; 

private TcpListener tcpListener;

void Start()
{
tcpListener = new TcpListener(IPAddress.Parse(ipAddress), port);
tcpListener.Start();
}

void Update()
{
if (tcpListener.Pending())
{
TcpClient client = tcpListener.AcceptTcpClient();
NetworkStream stream = client.GetStream();

byte[] buffer = new byte[1024];
int bytesRead = [stream.Read]
(buffer, 0, buffer.Length);

string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
float xPosition = float.Parse(data);
transform.position = new Vector3(xPosition, transform.position.y, transform.position.z);

stream.Close();
client.Close();
}
}
}

here is the code in TouchDesigner:

import socket

x_position = op('constant6')['move']

def send_pos(x_position):
    
    ip_address = "IP Computer"
    port = 12345

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.sendto(str(x_position).encode(), (ip_address, port))

    sock.close()

Knowledgeable people tell me what I’m doing wrong

Thank you in advance

How do you call the send_pos() method in TD?

A good start would be to add debug printing in sender and receiver code, so you can see what’s happening at each step.

Hi @maximmc83,

could you also use the TCPIP DAT instead of the socket library in python? It comes with it’s own .send Method.

cheers
Markus