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インスタンスを開いた状態で実行すると、私のマシンで機能します。ただし、2 台の異なるコンピューター間では機能しません。

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 に変更しただけですが、現在は実行中です........ 私のネットワークでは IPv6 が許可されていなかったため、IPv4 のみを使用することも非常に重要でした。

Matlab で TCP コードを書き込もうとしている人は、そのポート番号で接続しようとしているすべての IP を受け入れるため、誰が接続しているか気にしない場合は、接続に '0.0.0.0' を使用してください。

最初のファイルの現在のコード:

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

2 番目のファイルの現在のコード:

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 に答える