Advance Java

Learn New things from Rixosys

Networking in Java

Networking in Java

In this section we are exploring the java.net package which provides the support for networking in java with a generic style. All the java classes for developing a network program are defined in the java.net package. 
   
Here we illustrate an example in which a client sends a request (lets say the request is.."POST/index.html  HTTP/1.0\\n\\n" ) to the server for a file named index.html and as the server establishes a connection successfully, it gets the index.html file and sends it to the client.

Server

Client

Listens to port 8080. Connects to port 8080.
Accepts the connection. Writes "POST/index.html HTTP/1.0\\n\\n".
Reads up until it gets the second end-of line (\\n).  
Sees that POST is a known command and that HTTP/1.0 is a valid protocol version.  
Reads a local file called /index.html.  
Writes "HTTP/1.0 8080 OK\\n\\n". "8080" means "here comes the file."

Copies the contents of the file into the socket.

Reads the contents of the file and displays it.

The above process is an actual transaction via which a client and a server talk with each other. Every computer on a network has an address. It is called an Internet address ( IP Address) it is a 12 digit number that uniquely identifies each computer on the Network. There are 32 bits in an IP address having a sequence of four numbers between 0 and 255 separated by dots  (.).  
  
However it is a very cumbersome process to remember the computers on the network through their IP address values so DNS is there to help you out. It has devised a string format to identify an IP address that avoids the user to track so many numbers (IPs) over a network, it is also known as Domain Naming Service (DNS).

  
Now lets quickly move on to the networking part of java and take a look at how it relates to all of these networking concepts. In Java we can build I/O objects across the network by extending the I/O interface. Java supports most of the networking  protocols e.g. TCP and UDP protocol families, TCP is used for reliable stream-based I/O across the network and UDP supports the point-to-point datagram-oriented model.
  
All the java networking classes and interfaces are contained in the java.net package, it is given below:

Authenticator JarURLConnection SocketPermission
ContentHandler MulticastSocket URL
DatagramPacket NetPermission URLClassLoader
DatagramSocket

PasswordAuthentication

URLConnection
DatagramSocketImpl ServerSocket URLDecoder
HttpURLConnection Socket URLEncoder
InetAddress SocketImpl

URLStreamHandler

Including all above the package's interfaces of the java.net are as under :

ContentHandlerFactory SocketImplFactory
URLStreamHandlerFactory FileNameMap
SocketOptions  

 
Apart from all of this the address is the fundamental part in sending mail, or establishing a connection across the Internet. In java the InetAddress class is used to encapsulate both the numerical IP address and the domain name for that address. The InetAddress class has no visible constructors that is why, to create an InetAddress object, we have to use one of the available factory methods like getLocalHost( ), getByName( ), and getAllByName( ) can be used.

 

Some Important Interfaces

INTERFACES
CookiePolicy CookieStore
FileNameMap SocketOption
InetAddress ServerSocket
SocketImplFactory ProtocolFamily

InetAddress

Inet Address encapsulates both numerical IP address and the domain name for that address. Inet address can handle both IPv4 and Ipv6 addresses. Inet Address class has no visible constructor. To create an inet Address object, you have to use Factory methods.

Three commonly used Inet Address factory methods are.

  1. static InetAddress getLocalHost() throws UnknownHostException
  2. static InetAddress getByName (String hostname) throws UnknownHostException
  3. static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException

InetAddress subclasses


Example using InetAddress class

import java.net.*;
class Test
{
 public static void main(String[] args)
 {
  InetAddress address = InetAddress.getLocalHost();
  System.out.println(address);
  address = InetAddress.getByName("www.studytonight.com");
  System.out.println(address);
  InetAddress sw[] = InetAddress.getAllByName("www.google.com");
  for(int i=0; i< sw.length; i++)
  {
   System.out.println(sw[i]);
  }
 }
}

Welcome-PC/59.161.87.227 www.studytonight.com/208.91.198.55 www.google.com/74.125.236.115 www.google.com/74.125.236.116 www.google.com/74.125.236.112 www.google.com/74.125.236.113 www.google.com/74.125.236.114 www.google.com/2404:6800:4009:802:0:0:0:1014


Socket and ServerSocket Class

Socket is foundation of modern networking, a socket allows single computer to serve many different clients at once. Socket establishes connection through the use of port, which is a numbered socket on a particular machine. Socket communication takes place via a protocol. Socket provides communication mechanism between two computers using TCP. There are two kind of TCP sockets in Java. One is for server and other is for client.

  • ServerSocket is for servers.
  • Socket class is for client.

 


URL class

Java URL Class present in java.net package, deals with URL (Uniform Resource Locator) which uniquely identify or locate resources on internet.

url defination


Important Methods of URL class

  • getProtocol() : Returns protocol of URL
  • getHost() : Returns hostname(domain name) of URL
  • getPort() : Returns port number of URL
  • getFile() : Returns filename of URL

Program using URL class

import java.net.*;
class Test
{
 public static void main(String[] arg) throws MalFormedURLException
 {
  URL hp = New URL("http://www.studytonight.com/index");
  system.out.println(hp.getProtocol[]);
  System.out.println(hp.getFile[]);
 }
}

http -1 www.studytonight.com /index