Welcome to another java tutorial. In this tutorial will see how we can obtain IP address of server in java. we will use java.net.* package to obtain IP address of server.Below is steps to obtain IP address.
1. In first step we have to create InetAddress Instance
An instance of an InetAddress consists of an IP address and possibly its corresponding host name
2. In second step we will call getHostAddress() method of InetAddress to get IP address.
Below is code to get IP address in java
Below is code to get IP address in java
package com.kodemaker import java.net.InetAddress; import java.net.UnknownHostException; public class GetIPAddressOfServer { public static void main(String[] args) { InetAddress serverIpAddresss; try { serverIpAddresss = InetAddress.getByName("www.google.com"); System.out.println("Google IP address : " + serverIpAddresss.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } } }
Output of above programme
Google IP address : 216.58.208.36
No comments:
Post a Comment