How to use TCP/IP for the communication between more than two instances of MATLAB on a same computer?

조회 수: 18 (최근 30일)
I have found the below link as an example of using TCP/IP to send and receive data between two instances of MATLAB:
However, I would like to know how can one use TCP/IP for example to send and receive data between three instances of MATLAB? It would be great if someone explain this with a simple example.

답변 (1개)

Ran Chen
Ran Chen 2018년 8월 22일
Hi,
More than one servers can be created in a MATLAB session. You may create two servers(naming it as server1, server2) at the same time in a MATLAB session, and create two client instances of MATLAB(client1, client2). Server1 communicates with Client1, Server2 communicates with Client2. Since Server1 and Server2 are in the same MATLAB session so they can share data or communicate via Workspace.
You may refer to the code in the attached link:
https://www.mathworks.com/matlabcentral/fileexchange/63328-matlab-server-example-communicate-with-two-clients
  댓글 수: 2
RZM
RZM 2018년 9월 5일
편집: RZM 2018년 9월 5일
Thank you Ran. The problem is different with what you mentioned, but probably there is a solution I do not know yet. Say we have the following simplified example:
t3 = tcpip('0.0.0.0',55000,'NetworkRole','Server');
set(t3, 'InputBufferSize', 1024);
fopen(t3);
t5 = tcpip('127.0.0.2', 6000, 'NetworkRole', 'client');
set(t5, 'InputBufferSize', 1024);
fopen(t5);
fwrite(t3,[1 5 0],'double');
label = fread(t5,1,'double');
disp(label)
in one MATLAB instance. And in another MATLAB instance we have this:
t4 = tcpip('127.0.0.1',55000, 'NetworkRole', 'client');
% This is to receive data from t3
set(t4, 'InputBufferSize', 1024,'Timeout',20);
fopen(t4);
t6 = tcpip('0.0.0.0', 6000, 'NetworkRole', 'server');
% This is to send to t5
set(t6, 'InputBufferSize', 1024,'Timeout',20);
fopen(t6);
flg = fread(t4,[1,3],'double');
fwrite(t6,1,'double');
I first run the latter file with t4 and t6 object. Then I run the one with t3 and t5 objects.
The problem is that, first of all I do not know which addresses I should use for this kind of communication between two MATLAB instances on the same computer. In this example the communication is not performed and I get error for t4.
Any help is greatly appreciated.
Souarv De
Souarv De 2022년 4월 5일
편집: Souarv De 2022년 4월 5일
@Ran Chen I have an querry regarding the code you have written. It will be very much helpful for me if you kindly explain it in more details. In the above code I am only interested between single communication between Server (t3) and Client (t4).
Q1) Why do you set buffersize in both t3 and t4? What is it's requirement here?
Q2) In t4 clinet section you have set timeout period of 20. Does it mean client will wait for 20 sec to get response from the server failing which the MATLAB throws some error.
Q3) As I see you are sending 3 data [1 5 0] in an array via server t3 but why do you write fread(t4,[1,3],'double'); in t4 client session. what is this [1 3] signifies. What if I donot write 'double'. And I am not able to guess what would be the output also.
Please let me know.

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by