2013-02-20 127 views
0

上我試圖在位從一臺計算機發送一些數據到另一個是在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 

任何幫助表示讚賞。

+1

可以在兩臺計算機平彼此? – slayton 2013-02-21 00:45:28

+0

「不起作用」是什麼樣子?你得到什麼錯誤信息?有一件事我注意到了 - 你設置了一個很短的超時時間set(connectionServer,'Timeout',。1);' - 這是個好主意嗎?不知道這些單位是什麼,但如果這是毫秒,你沒有給自己很長的時間......兩臺電腦可能需要超過100美元才能通過網絡找到對方...... – Floris 2013-02-21 02:17:23

+0

@Floris超時更多讀/寫,甚至不會應用,直到計算機連接後..... – Neppinger 2013-02-21 14:07:13

回答

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 
相關問題