0% found this document useful (0 votes)
40 views

Asic Etworking Oncept: Y Yananjaya Tripathy

Networking concepts in Java include platform independence, support for international characters, and core networking classes and interfaces in the java.net, java.rmi, and javax.mail packages. The NetworkInterface class can obtain details of local network interfaces like name, addresses, and properties. The InetAddress class represents IP addresses and can obtain the local host name and address. URLs represent web resources and the URL and HttpURLConnection classes allow downloading and accessing details of web resources.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Asic Etworking Oncept: Y Yananjaya Tripathy

Networking concepts in Java include platform independence, support for international characters, and core networking classes and interfaces in the java.net, java.rmi, and javax.mail packages. The NetworkInterface class can obtain details of local network interfaces like name, addresses, and properties. The InetAddress class represents IP addresses and can obtain the local host name and address. URLs represent web resources and the URL and HttpURLConnection classes allow downloading and accessing details of web resources.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

BASIC NETWORKING CONCEPT

BY: GYANANJAYA TRIPATHY


JAVA AND NET

 Java has certain advantages.

1. Platform independent
2. Support 16-bit Unicode for international character
JAVA NETWORKING CLASSES AND INTERFACES

 Three packages java.net, java.rmi and javax.mail

 First two are part of JVM and final one is


downloaded from Oracle website.
GETTING NETWORK INTERFACES
 A network interface is usually a Network Interface
Card (NIC)

 Systems often run with multiple network interfaces


such as wired Ethernet (802.3), WiFi (802.11 a/b/g),
Bluetooth (802.15.4) etc

 This class has no public constructor. However, it


has many static methods such as
getNetworkInterfaces(), getByInetAddress(),
getByName()
CONTINUE…
import java.net.*;
import java.util.*;
class GetNetworkInterfaces {
public static void main(String args[]) throws Exception {
Enumeration<NetworkInterface> intfs=NetworkInterface.
getNetworkInterfaces();
while(intfs.hasMoreElements()) {
NetworkInterface intf = intfs.nextElement();
System.out.println("\nInterface: "+intf.getName());
System.out.println("Display name: "+intf.getDisplayName());

}
}
}
CONTINUE
Interface: lo
Display name: Software Loopback Interface 1

Interface: eth3
Display name: Realtek PCIe GBE Family Controller

Interface: net4
Display name: Atheros AR9285 802.11b/g/n WiFi Adapter
GETTING INTERFACE ADDRESSES
import java.net.*;
import java.util.*;
class GetInterfaceAddresses {
public static void main(String args[]) throws Exception {
System.setProperty("java.net.preferIPv4Stack","true");
Enumeration<NetworkInterface> intfs=NetworkInterface.getNetworkInterfaces();
while(intfs.hasMoreElements()) {
NetworkInterface intf = intfs.nextElement();
Enumeration<InetAddress> addresses = intf.getInetAddresses();
if(addresses.hasMoreElements()) {
System.out.println("\nName: "+intf.getName());
System.out.println("Display name: "+intf.getDisplayName());
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
System.out.println("Address: "+addr);

}
}
}
}
}
CONTINUE
Name: lo
Display name: Software Loopback Interface 1
Address: /127.0.0.1

Name: net4
Display name: Atheros AR9285 802.11b/g/n WiFi Adapter
Address: /192.168.43.77

Name: eth4
Display name: VMware Virtual Ethernet Adapter for VMnet1
Address: /192.168.226.1

Name: eth5
Display name: VMware Virtual Ethernet Adapter for VMnet8
Address: /192.168.40.1
HOSTNAME AND ADDRESS OF THE LOCAL
COMPUTER
import java.net.*;
class LocalHost {
public static void main (String args[]) throws
UnknownHostException {
InetAddress ia = InetAddress.getLocalHost();

System.out.println("Name : "+ia.getHostName());
System.out.println("Address : "+ia.getHostAddress());

}
}
CONTINUE
Name : UROY
Address : 192.168.226.1
GETTING INTERFACE PROPERTIES
import java.net.*;
import java.util.*;
class GetInterfaceParameters {
public static void main(String args[]) throws Exception {
NetworkInterface intf = NetworkInterface.getByName(args[0]);
System.out.println("\nName : "+intf.getName());
System.out.println("Display name : "+intf.getDisplayName());
System.out.println("Up : "+ intf.isUp());
System.out.println("Loopback : "+ intf.isLoopback());
System.out.println("PointToPoint : "+intf.isPointToPoint());
System.out.println("Supports multicast :"+intf.supportsMulticast());
System.out.println("Virtual : "+intf.isVirtual());
byte[] mac1 = intf.getHardwareAddress();
if(mac1 != null) {
System.out.print("Hardware Address : ");
for (int k = 0; k < mac1.length; k++)
System.out.format("%02X%s", mac1[k], (k < mac1.length - 1) ? "-" : "");
System.out.println();
}
System.out.println("MTU :"+intf.getMTU());
}
}
SAMPLE OUTPUT
Name : net4
Display name : Atheros AR9285 802.11b/g/n WiFi Adapter
Up : true
Loopback : false
PointToPoint : false
Supports multicast :true
Virtual : false
Hardware Address : D0-DF-9A-01-55-E6
MTU :1500
URL
 Uniform Resource Locator

 The URL class provides mechanisms to download web


resources to the client computer.

 It also has methods to retrieve different parts, such as


method, hostname, port etc. of a URL.

 The primary classes to access the Internet are the java.net.


URL class and the java.net.HttpURLConnection class
CREATING URL
 URL(String url)
 URL url = new URL("http://www.google.com");

 URL(URL baseURL, String relativeURL)


 URL url = new URL(new URL("https://plus.google.com"), "u/0/?
tab=wX");

 MalformedURLException for argument pass error.


 openStream() method returns an InputStream object which is
used to read data from HTTP socket.
URLCONNECTION
import java.util.*;
import java.net.*;
public class URLPropertiesDemo {
public static void main(String args[]) throws Exception {
int c;
URL url = new URL(“http://www.google.com“);
URLConnection con = url.openConnection();
System.out.println("Content-type: " + con.getContentType());
System.out.println("Content Encoding: " + con.getContentEncoding());
System.out.println("Content-length: " + con.getContentLength());
System.out.println("Last-Modified: " + new Date(con.getLastModified()));
System.out.println("Date: " + new Date(con.getDate()));
System.out.println("Expires: " + con.getExpiration());
System.out.println("Connection Timeout: " + con.getConnectTimeout());
}
}
CONTINUE
HTTPURLCONNECTION
import java.io.*;
import java.net.*;
import static java.net.HttpURLConnection.*;
public class HttpRedirectDemo {
public static void main(String[] args) throws Exception {
URL url = new URL(args[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println("Request URL ... " + url);
int code = con.getResponseCode();
if((code != HTTP_OK) && (code == HTTP_MOVED_TEMP || code == HTTP_MOVED_PERM || code == HTTP_SEE_OTHER)) {
System.out.println("Response Code ... " + code);
URL newUrl = new URL(con.getHeaderField("Location"));
con = (HttpURLConnection) newUrl.openConnection();
System.out.println("Redirect to URL : " + newUrl);
}
BufferedReader in = new BufferedReader(new
InputStreamReader(con.getInputStream()));
String line;
StringBuffer htmlStr = new StringBuffer();
while ((line = in.readLine()) != null) htmlStr.append(line);
in.close();
System.out.println("Content... \n" + htmlStr);
}
}

You might also like