与上述类似的方法:
function tf = haveInet()
tf = false;
try
address = java.net.InetAddress.getByName('www.google.de')
tf = true;
end
end
Run Code Online (Sandbox Code Playgroud)
它的好处是不会产生额外的进程,并且与特定站点目前是否不可用(这可能是一个好的或坏的功能)的事实无关。
ping
使用Google 的 DNS 之一怎么样?
if ispc
C = evalc('!ping -n 1 8.8.8.8');
elseif isunix
C = evalc('!ping -c 1 8.8.8.8');
end
loss = regexp(C, '([0-9]*)%.*loss', 'tokens');
connected = ~isempty(loss) && str2double(loss{1}{1})==0;
Run Code Online (Sandbox Code Playgroud)