Java Network classes Part1
Java Networking Tutorial
Goto Part2
All network related classes are bundled in java.net package in Java Language. It includes various classes and interfaces for implementing networking applications. This tutorial gives you an overview of java.net package.
Classes and Interfaces available in Java.net Package
InetAddress
Inet4Address | are subclasses of InetAddress
Inet6Address |
URL
URLConnection
HttpURLConnection
Socket, |
ServerSocket | These classes uses TCP, to communicate over the network.
DatagramPacket, |
DatagramSocket, | These classes uses UDP to communicate over the network.
MulticastSocket |
Java programs can use both TCP or UDP based classes to communicate over the Internet.
TCP/IP based classes
Socket—Generally used by client program to communicate with the Server. A socket is one end-point
of a two-way communication link between two programs running on the network.
ServerSocket → with the ServerSocket server listens client request.
Datagram based classes
DatagramSocket
DatagramPacket
What is URL?
Uniform Resource Locator(URL) is a reference/address
to a resource on Internet.
Ex: http://java.oracle.com
http:// → is a protocol identifier
java.oracle → a resource name
.com → is domain name
There are different domain names available. ex:
.com
.org
.net
.mil
.in
.edu etc.,
URL Class
To identify a resource java provides a class called URL. which can allows to get various
information about url. to create url the following constructors can be used.
Constructors
URL(String url) throws MalformedURLException
URL(String protocolName,String hostName, int port,String path) throws MalformedURLException
URL(String protocolName,String hostName, String path) throws MalformedURLException
URL(URL urlObj ,String urlspecifier) throws MalformedURLException
syn1:
URL u = new URL("http://www.oracle.com/index.html");
syn2:
URL example = new URL("http", "www.example.com", 80, "pages/example.index.html");
This creates a URL object for the following URL:
www.example.com:80/pages/example.index.html
Methods
getProtocol() → The protocol on which it is communicating.
getPort() → The port number to which to connect (typically optional).
getHost() → The name of the machine on which the resource lives.
getFile() → The path name to the file on the machine.
//Demonstrate URL import java.net.*; import java.io.*; class URLDemo{ public static void main(String args[]) throws MalformedURLException{ URL hp = new URL("http://www.oracle.com/index.html",80); System.out.println("Protocol:"+hp.getProtocol()); System.out.println("Port:"+hp.getPort()); System.out.println("Host:"+hp.getHost()); System.out.println("File:"+hp.getFile()); System.out.println("Port:"+hp.toExternalForm()); } }
output:
H:\JavaNetworking>java URLDemo
Protocol:http
Port:-1
Host:www.oracle.com
File:/index.html
Port:http://www.oracle.com/index.html
InetAddress Class
Internet addresses are represented in Java by the InetAddress class.
InetAddress provides simple methods to convert between domain names, and numbered addresses.
It allows to find Hostname,IPAddress.
domainName IPAddress
ex: localhost ---- 127.0.0.1
To create an InetAddress object, factory methods are used.
Factory methods are merely a convention whereby static methods in a class
returns an instance of that same class.
Static methods
static InetAddress getLocalHost() throws UnknownHostException static InetAddress getByName(String hostName) throws UnknownHostException static InetAddress[] getAllByName(String hostName) throws UnknownHostException
EX1:
// Demonstrate InetAddress. import java.net.*; class InetAddressTest { public static void main(String args[]) throws UnknownHostException { InetAddress Address = InetAddress.getLocalHost(); System.out.println(Address); Address = InetAddress.getByName("google.com"); System.out.println(Address); InetAddress SW[] = InetAddress.getAllByName("http://www.oracle.com"); for (int i=0; i<SW.length; i++) System.out.println(SW[i]); } }
output:
H:\JavaNetworking>javac InetAddressTest.java
H:\JavaNetworking>java InetAddressTest
hpsoma/192.168.1.4
oracle.com/138.1.33.162
www.google.com/142.250.183.196
www.google.com/2404:6800:4009:826:0:0:0:2004
URLConnection Class
URLConnection is a general-purpose class for accessing the attributes of a remote resource.
//Demonstrate URLConnection
import java.net.*; import java.io.*; import java.util.*; class UCDemo{ public static void main(String args[]) throws Exception{ int c; URL hp = new URL("http://www.google.com"); URLConnection hpCon= hp.openConnection(); long d = hpCon.getDate(); if(d==0) System.out.println("Content-Type:"+hpCon.getContentType()); d= hpCon.getExpiration(); if(d==0) System.out.println("No expiration information."); else System.out.println("Expires:"+ new Date(d)); d= hpCon.getLastModified(); if(d==0) System.out.println("No LastModified information." + new Date(d)); int len= hpCon.getContentLength(); if(len == -1) System.out.println("Content length is unavailable"); else System.out.println("Content length : "+len); if(len != 0){ System.out.println("=========Content======="); InputStream input = hpCon.getInputStream(); int i=len; while(((c=input.read())!= -1)){ System.out.println((char)c); } input.close(); } else{ System.out.println("No content is available."); }}}
output:
H:\JavaNetworking>javac UCDemo.java
H:\JavaNetworking>java UCDemo
No expiration information.
No LastModified information.Thu Jan 01 05:30:00 IST 1970
Content length is unavailable
=========Content=======
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2022/2022-world-cup-opening-day-6753651837109999.4-law.gif" itemprop="image"><meta content="2022 World Cup - Opening Day!" property="twitter:title"><meta content="Let the games begin! #GoogleDoodle" pro.......
EX2: import java.net.*; import java.io.*; class ConnectionTest { public static void main(String[] args) { try { URL google = new URL("http://www.google.com/"); URLConnection googleConnection = google.openConnection(); DataInputStream dis = new DataInputStream(googleConnection.getInputStream()); BufferedReader d = new BufferedReader(new InputStreamReader(dis)); String inputLine; while ((inputLine = d.readLine()) != null) { System.out.println(inputLine); } dis.close(); } catch (MalformedURLException me) { System.out.println("MalformedURLException: " + me); } catch (IOException ioe) { System.out.println("IOException: " + ioe); } } }
output:
H:\JavaNetworking>javac ConnectionTest.java
H:\JavaNetworking>java ConnectionTest
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2022/2022-world-cup-opening-day-6753651837109999.4-law.gif" itemprop="image"><meta content="2022 World Cup - Opening Day!" property="twitter:title"><meta content="Let the games begin! #GoogleDoodle" property="....
Comments
Post a Comment