1

我正在运行一个程序,在该程序中,我使用 Instrument control 工具箱在一台机器上建立了与另一台机器的服务器和客户端连接,因此它最终或多或少地充当对等连接。

该代码第一次在 2 台单独的机器上运行时运行良好,但有时它不会在第二次尝试时运行,或者如果我让 Matlab 保持打开状态并稍后尝试运行它,它不会运行。我得到的只是一个程序试图找到服务器,而另一个程序已经打开了服务器连接,但他们从来没有找到对方。

打开连接的代码(客户端代码非常相似):

function connectionServer = openRecieve(client, port)
t = tcpip(client, 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

关闭连接的代码:

function closeConnection(connection)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

fclose(connection); 
delete(connection); 
clear connection;

end

我认为这与超时没有任何关系,因为它们在第一次连接和运行时没有问题。

我还补充说:

clc
clear all

到我程序的结尾和开头,有时在第二次或第三次运行时仍然无法连接。

任何想法表示赞赏

4

0 回答 0