resolveip function returns either the
name or address of an IP (internet protocol) host. If you pass
host name, it returns the IP address. If you pass
IP address, it returns the host name. The function uses the
which enables you to find an IP address for a host name, or the host
name for a given IP address, without making DNS calls.
resolveip calls a
InetAddress class to obtain an
Then, it calls accessor methods on the
to get the host name and IP address for the input argument. It displays
either the host name or the IP address, depending on the program input
The major tasks performed by
Instead of constructors, the
static methods that return an instance of the
try statement calls one of those methods,
input argument to
The input argument can be either a host name or an IP address. If
catch statement displays an
function resolveip(input) try address = java.net.InetAddress.getByName(input); catch error(sprintf('Unknown host %s.', input)) end
Retrieve the host name and IP address.
The example uses calls to the
functions on the
java.net.InetAddress object, to
obtain the host name and IP address, respectively. These two functions
return objects of class
java.lang.String; use the
to convert them to character arrays.
hostname = char(address.getHostName); ipaddress = char(address.getHostAddress);
Display the host name or IP address.
The example uses the MATLAB®
to compare the
input argument to the resolved IP
address. If it matches, MATLAB displays the host name for the
internet address. If the input does not match, MATLAB displays
the IP address.
if strcmp(input,ipaddress) disp(sprintf('Host name of %s is %s', input, hostname)) else disp(sprintf('IP address of %s is %s', input, ipaddress)) end
function resolveip(input) try address = java.net.InetAddress.getByName(input); catch error(sprintf('Unknown host %s.', input)) end hostname = char(address.getHostName); ipaddress = char(address.getHostAddress); if strcmp(input,ipaddress) disp(sprintf('Host name of %s is %s', input, hostname)); else disp(sprintf('IP address of %s is %s', input, ipaddress)); end; end
Call the function with a host name.
IP address of www.mathworks.com is 22.214.171.124
Call the function with an IP address.
Host name of 126.96.36.199 is www.mathworks.com