Error occurred when communicating between MATLAB(Server) and Python(Client) via TCP/IP

18 views (last 30 days)
I'm using MATLAB R2021b now and intend to send some data to Python (of another device) via TCP/IP. And it's already be set that my MATLAB should work as a server while that Python treated as a client.
However, when using the newly created tcpserver() function in the instrument control toolbox, it's quite weird to find that the .Connected property is always 0 even though I'm pretty sure the connection has been established since I can receive the test request message sent from Python. And when I try to write the data and send it to Python, the following error occurred:
Failed to write from the server. A TCP/IP client must be connected to the server.
I have looked through all the possible suggestions online and found that someone had recommended to embed the write() function into a callback function
But unfortunately that dosen't meet my special request since I want to send abundant real-time data through a long-term iteration and embed the whole thing in the appdesigner environment, that is to say, I cannot stand the functionality that those data could only be sent when the connection state changes.
Does anyone have better a solution to this problem? I'm waiting for your generous help!
code for MATLAB:
tcp_Server = tcpserver('', 5001);
while true
if tcp_Server.NumBytesAvailable > 0
readData = read(tcp_Server, tcp_Server.NumBytesAvailable);
write(tcp_Server, "hello world");
code for Python:
import socket
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverIPPort = ('', 5001)
# send request
msgSent = input("Request:")
# receive msg
tcpMsgReceived = tcpSocket.recv(1024)

Accepted Answer

Kunal Kandhari
Kunal Kandhari on 3 Nov 2022
As per my understanding from your question, you want to send abundant real-time data between python client and MATLAB server and not only when the connection state changes.
For this, you need to create infinite While loops at both side when connection is established and send data uninterruptedly.
I have create sample code for this and added some comments for better understanding:
MATLAB Server:
[~,hostname] = system('hostname');
hostname = string(strtrim(hostname));
address = resolvehost(hostname,"address");
% Created Server
server = tcpserver(address,portNumber,"ConnectionChangedFcn",@connectionFcn)
function connectionFcn(src, ~)
%Client is connected
if src.Connected
disp("Client connected")
%Rea Write infinitly with connected client
while true
readData = read(src,src.NumBytesAvailable,'string');
write(src,"Bye Bye","string");
%Sleep for 5 millisecond
Python Client:
import socket
host = socket.gethostname()
port = 5004 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Connect to Server
s.connect((host, port))
#Read Write infinitly
while True:
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received',i,": ", repr(data))
For closing the connection after sending data, you can use some terminating conditions like on receiving something, close the connection.
I hope this resolves your query!

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!