0

我正在尝试使用 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

任何帮助表示赞赏。

4

1 回答 1

2

它现在正在运行,尽管我唯一更改的是从 3000 和 3000 到 3000 和 3001 的端口号............同样只使用 IPv4 是非常关键的,因为我的网络不允许使用 IPv6。

对于任何尝试在 Matlab 中编写 TCP 代码的人,如果您不关心谁在连接,只需使用“0.0.0.0”进行连接,因为它将接受所有尝试在该端口上连接的 IP #。

第一个文件的当前代码:

sConec = openSend('10.234.24.124', 3000); %IPv4 Address of comp your trying to connect to
rConec = openRecieve('0.0.0.0', 3001); %Accept all connections

第二个文件的当前代码:

rConec = openRecieve('0.0.0.0', 3000); %Accept all connections
sConec = openSend('10.109.22.142', 3001); %IPv4 Address of computer your trying to connect to
于 2013-02-22T14:33:29.883 回答