I am currently trying to run an instance of matlab as a tcp server using the tcpserver function from the Instrument Control Toolbox. I want a python script that acts as the TCP client to connect to the matlab server, send it commands and get the results back via TCP. Here is a simple example of this connection, as I am trying to implement it:
server = tcpserver('127.0.0.1', 1234)
data = read(server, 11, "string")
write(server, "Hello back!")
The code for the Python client is the following:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
print("Connection to server established!")
data = sock.recv(1024)
When I run the server and client, although Matlab receives the message from the Python client, I get the following error when it tries to send the answer:
Error using testserver (line 6)
Failed to write from the server. A TCP/IP client must be connected to the server.
Does anyone know why this happens and if there is a way to work around it?