package org.kodejava.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class URLDemo {
public static void main(String[] args) {
try {
// Creating a url object by specifying each parameter separately, including
// the protocol, hostname, port number, and the page name
URL url = new URL("https", "kodejava.org", 443, "/index.php");
// We can also specify the address in a single line
url = new URL("https://kodejava.org:443/index.php");
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Category Archives: Networking
How do I read or download web page content?
You want to create a program that read a webpage content of a website page. The example below use the URL
class to create a connection to the website. You create a new URL
object and pass the URL information of a page. After the object created you can open a stream connection using the openStream()
method of the URL
object.
Next, you can read the stream using the BufferedReader
object. This reader allows you to read line by line from the stream. To write it to a file create a writer using the BufferedWriter
object and specify the file name where the downloaded page will be stored.
When all the content are read from the stream and stored in a file close the BufferedReader
object and the BufferedWriter
object at the end of your program.
package org.kodejava.net;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class UrlReadPageDemo {
public static void main(String[] args) {
try {
URL url = new URL("https://kodejava.org");
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(
new FileWriter("data.html", StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
How do I ping a host?
Since Java 1.5 (Tiger) the java.net.InetAddress
class introduces isReachable()
method that can be used to ping or check if the address specified by the InetAddress
is reachable.
package org.kodejava.net;
import java.net.InetAddress;
public class PingExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("172.16.2.0");
// Try to reach the specified address within the timeout
// period. If during this period the address cannot be
// reach then the method returns false.
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e) {
e.printStackTrace();
}
}
}
How do I get a localhost hostname?
package org.kodejava.network;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostNameExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("Hostname: " + address.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
An example of output produce by the code snippet above is:
Hostname: Krakatau
How do I get IP address of localhost?
The example here show you how to get an IP or host address using the java.net.InetAddress
class. To get an instance of InetAddress
we call a static method of this class, the method is getLocalHost()
, which return the local host address. Next, to get the IP address we can call the getHostAddress()
method.
package org.kodejava.net;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LocalHostIpAddress {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
System.out.println("IP Address = " + ip);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
The result of this code snippet:
IP Address = 30.30.30.60