External Interfaces/API | ![]() ![]() |
Description of resolveip
The major tasks performed by resolveip
are:
1. Create an InetAddress Object
Instead of constructors, the java.net.InetAddress
class has static
methods that return an instance of the class. The try
statement calls one of those methods, getByName
, passing the input
argument that the user has passed to resolveip
. The input argument can be either a hostname or an IP address. If getByName
fails, the catch
statement displays an error
message.
function resolveip(input) try address = java.net.InetAddress.getByName(input); catch error(sprintf('Unknown host %s.', input)); end
2. Retrieve the Hostname and IP Address
The example uses calls to the getHostName
and getHostAddress
accessor functions on the java.net.InetAddress
object, to obtain the hostname and IP address, respectively.
hostname = address.getHostName; ipaddress = address.getHostAddress;
3. Display the Hostname or IP Address
The example uses the MATLAB strcmp
function to compare the input
argument to the resolved IP address. If it matches, MATLAB displays the hostname 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;
![]() | Example - Finding an Internet Protocol Address | Running the Example | ![]() |