我正在尝试使用 Matlab 中的 TCP 将一些数据从一台计算机发送到同一网络上的另一台计算机。
目前这是我设置打开连接的方法。我正在尝试模拟点对点连接,因为它们需要相互发送和接收数据。当我使用 IPv4 和 IPv6 运行它时,它在我的本地计算机上运行良好。
%code starts in one file
openRecieve('0.0.0.0', 3000); %accept all connections
openSend('10.32.41.235',3000);
然后我在另一个文件中做同样的事情,我可以在我的机器上并行运行它们:
%code starts in other file
openSend('10.32.41.235',3000); %IPv4 of PC
openRecieve('0.0.0.0', 3000); %accept all connections
IP 是伪造的......当在打开 2 个不同的 matlab 实例的情况下运行时,此代码在我的机器上工作。但是,它在两台不同的计算机之间不起作用。
openReceive 的代码:
function connectionServer = openRecieve(client, port)
t = tcpip('0.0.0.0', port, 'NetworkRole', 'Server');
set(t, 'InputBufferSize', 3000000);
% Open connection to the client.
fopen(t);
fprintf('%s \n','Client Connected');
connectionServer = t;
set(connectionServer,'Timeout',.1);
end
openSend 的代码:
function connectionSend = openSend(host, port)
d = tcpip(host, port, 'NetworkRole', 'Client');
set(d, 'OutputBufferSize', 3000000); % Set size of receiving buffer, if needed.
%Trying to open a connection to the server.
while(1)
try
fopen(d);
break;
catch
fprintf('%s \n','Cant find Server');
end
end
connectionSend = d;
end
任何帮助表示赞赏。