// Example 4.6 import java.net.*; class MyAddress6 { public static void main (String args[]) { try { InetAddress thisComputer = InetAddress.getLocalHost(); byte[] address = thisComputer.getAddress(); System.out.print("My address is "); for (int i = 0; i < address.length; i++) { int unsignedByte = address[i] < 0 ? address[i] + 256 : address[i]; System.out.print(unsignedByte + "."); } System.out.println(); } catch (UnknownHostException e) { System.out.println("I'm sorry. I don't know my own address."); } } }
Tuesday, November 25, 2014
Find the IP address of the local machine
Find the hostname of the local machine
import java.net.*; class myName { public static void main (String args[]) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println("Hello. My name is " + address.getHostName()); } catch (UnknownHostException e) { System.out.println("I'm sorry. I don't know my own name."); } } }
Find the address of the local machine
import java.net.*; class myAddress { public static void main (String args[]) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find this computer's address."); } } }
: A program that prints all the addresses of www.apple.com
import java.net.*; class apple { public static void main (String args[]) { try { InetAddress[] addresses = InetAddress.getAllByName("www.apple.com"); for (int i = 0; i < addresses.length; i++) { System.out.println(addresses[i]); } } catch (UnknownHostException e) { System.out.println("Could not find www.apple.com"); } } }
A program that prints the address of 72.167.232.155
import java.net.*; class MyHostName{ public static void main (String args[]) { try { InetAddress address = InetAddress.getByName("72.167.232.155"); System.out.println(address.getHostName()); } catch (UnknownHostException e) { System.out.println("Could not find 72.167.232.155"); } } }
A program that prints the address of www.oreilly.com
import java.net.*; class oreilly { public static void main (String args[]) { try { InetAddress address = InetAddress.getByName("www.oreilly.com"); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find www.oreilly.com"); } } }
Subscribe to:
Posts (Atom)