TCP/2台的计算机之间IP Matlab的同一网络
问题描述:
上我试图在位从一台计算机发送一些数据到另一个是在Matlab使用TCP在同一个网络上。TCP/2台的计算机之间IP Matlab的同一网络
目前这是我已设置打开连接。我试图模拟一个对等连接,因为他们需要互相发送和接收数据。当我使用我的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
任何帮助表示赞赏。
答
它现在正在运行虽然我唯一改变的事情是从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
可以在两台计算机平彼此? – slayton 2013-02-21 00:45:28
“不起作用”是什么样子?你得到什么错误信息?有一件事我注意到了 - 你设置了一个很短的超时时间set(connectionServer,'Timeout',。1);' - 这是个好主意吗?不知道这些单位是什么,但如果这是毫秒,你没有给自己很长的时间......两台电脑可能需要超过100美元才能通过网络找到对方...... – Floris 2013-02-21 02:17:23
@Floris超时更多读/写,甚至不会应用,直到计算机连接后..... – Neppinger 2013-02-21 14:07:13