In this Tutorials section we cover and explain Introduction to java Network in details for freshers and experienced
previous | Home | Next |
Introduction of Java Networking
Java Networking is a best topic to introducing in computer operations the twenty first century. Java provides a many role in contain for the networking class objects through its .net, .nio and .rmi packages. it's a provide to http connections and streams to protocol sockets. java.nio, java.nio.charset and java.nio.channels are providing buffers, character sets and channel of multiplexed non-blocked applications that are more tolerate of dropped connections and time delays. java.rmi provides methods for remote method invocation. Some of these objects will be described in the following material.
The Java.net is Providing classes and interfaces that facilitates development of networking application in java. commonly used classes and interface of this package are
- InetAddress
- Socket
- ServerSocket
- Datagram Socket
- Datagram Packet
InetAddress
The InetAddress class have not a constructors, it means no visible constructors. If you want to crate a object or instance then create an InetAddress object, you have to using one of the available factory methods. Factory methods are mainly a convention whereby using static methods in a class and return an instance of that class.
This is done by overloading a constructor and use by with the various parameter lists when it's having unique method names making the results much clear. In the case we use this method getLocalHost(), getByName(), and getAllByName() can be used to create instances or object of InetAddress.
The InetAddress methods are using in the program:
static InetAddress getLocalHost( ) throws UnknownHostException static InetAddress getByName(String hostName) throws UnknownHostException static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
getLocalHost( ) method is returns the InetAddress object or instance it's presented by local host.
getByName( ) method is returns an InetAddress for a host name passed to it.
If any methods are unable to resolve the host name,they are throw an UnknownHostException.
On the Internet, it is common for a single name to be used to represent several machines. In the world of web servers, this is one way to provide some degree of scaling. The getAllByName( ) factory method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. It will also throw an UnknownHostException if it can't resolve the name to at least one address.
The following example prints the addresses and names of the local machine and two well-known Internet web sites
Example
// Demonstrate InetAddress. import java.net.*; class InetAddTest { public static void main(String args[]) throws UnknownHostException { InetAdd address = InetAdd.getLocalHost(); System.out.println(address); address = InetAdd.getByName("rahul@r4r.com"); System.out.println(address); InetAdd j[] = InetAdd.getAllByName("www.r4r.co.in"); for (int i=0; i<j.length; i++) System.out.println(j[i]); } }
Output
default/206.148.209.138 rahul@r4r/204.202.129.90 www.r4r.co.in/204.202.130.223
previous | Home | Next |