TCP Client / Server setup

I’ve been having some trouble getting a python TCP client to connect to a touch designer TCP/IP DAT in Server mode.

Here’s my python code:

[code]#!/usr/bin/env python

import socket

TCP_IP = ‘127.0.0.1’
TCP_PORT = 5005
BUFFER_SIZE = 20 # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print ‘Connection address:’, addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print “received data:”, data
conn.send(data) # echo
conn.close()[/code]

Basically it doesn’t even appear to connect.

The strange thing is that I can create a server in python then use the TCP/IP DAT in client mode to connect no problem.

Any idea what’s going on?

I didn’t read your example (sorry short on time but I had this example so figured I’d just post it for you).
tcp.toe (4.49 KB)

Won’t let me attach .py files :neutral_face:

import socket

#set server variables
TCP_IP = '127.0.0.1'
TCP_PORT = 7001
BUFFER_SIZE  = 512

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()

#print connection address when someone connects
print ('Connection address:', addr)
		
#start main loop
while 1:
	#listen for calls
	data = conn.recv(BUFFER_SIZE)
	data = data.decode('utf-8')
	print('message received:: ', data)
	#if someone calls with hello, respond
	if 'hello' in data:
		delivery = 'Hello from Python!!'
		conn.send(delivery.encode('utf-8'))

conn.close()

Or wait, I didn’t even read your question. Sorry, my example is python server, touch client. :cry:

Yeah I can get that direction to work no problem. It’s the touch server / python client method that I can’t seem to get functioning properly.

Bump.

Has anyone had any success with this?

Hi,

I had a similar problem just a couple of days ago. I wanted a remote RasPi connect to a TD server over TCP/IP but I couldnt get it done. Then I added another TCP operator as client (fake client) to test if the connection was working. I wrote a simple op.send(’’) function in an execute dat, and suddenly I was able to get the raspi messages. I’ve attached the base file, I hope It works for you too.
RaspiNetwork.5.toe (4.78 KB)

Ah ha! Yes I remember something like this before.

This sounds like a bug to me. I’m going to make a bug report.

The issue is that the DAT is waiting on a carriage return before processing any results.
Row/Callback Format = One Per Line.

The .send() method automatically appends a newline, flushing out all the accumulated messages.

Changing the format to One Per Byte will give more immediate feedback, though delineating messages requires more work.

Cheers,
Rob.

Why does touch crashes every time it is a server?

SERVER (TOUCH)

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))
s.listen(5)
clientsocket, address = s.accept()
print(f"Connection from {address} has been established!")
clientsocket.send(bytes(“Welcome to the server!”, “utf-8”))

CLIENT (External Python - Visual Code Studio)

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(),1234))
msg = s.recv(8)
print(msg.decode(“utf-8”))

Use the TCP/IP-DAT instead of python for stuff like that in TouchDesigner.
You are starting a server in the TouchDesigner Thread and keeping the script from finnishing, as it is listening. So, this is not a crash but touch not being able to continue the frame as it is wating.

Also, this Thread is pretty old and your question has nothing to do qith the question originaly asked. So, make a new one next time.