HOME
J2ee tutorials
Java introduction
Java basic
Java installation
Java Packages Learn
Applets Java Threads Java Gui component

Java Event Handaling Java Streams and Files
 Java Swings Java JDBC
 Java Network Programming Java RMI
 Java Servlets Java javabeans
 EJB Struts

|
Java Networking
In simple word networking can be defines as the process of making two or more computers communicates. Almost all the language have their own specific mechanism for writing network –based programs and java too has its own built in support for network programming with the help of the classes defined in the java.net package.
But before we learn about the various classes and interfaces in java.net package, it would be appropriate if we have a brief overview of the networking
basics. Over the years, many models have been introduced to explain the complex interaction of the various hardware components that built a simple network.
These models were aimed to standardize all the new software and hardware components. But of all the models so far, the open systems interconnect (OSI) refere
nces model is the most accepted and significant
The open systems interconnect (OSI) references model
The OSI references model consists of 7 layer architecture. With each layer supporting a distinct functionality set. Each layer is based on the natural sequence of events that occurs during communications sessions and each layer is capable of communicating to layer above and layer below, through an interface. I have not given the explanation of each layer.
Application layer-------------presentation layer------session layer------transport layer----network layer----data link layer-----physical layer
Networking in java
Java’s view of networking is primarily based On TCP/IP because TCP/IP is the only true “open” networking standard that links together the four corners of our globe via internet
TCP/IP (TRANSMISSION CONTROL PROTOCOL/INTERNET PROTOCOL) is asset of communication protocols for communicating between different types of mechanism and network other protocols in the TCP/IP suit ate the user datagram protocol(UDP) the internet control message protocol(ICMP) and the internet group protocol(IGMP)
These protocol defines a standard format for exchanging information between mechanism (know as hosts) regardless of the physical connections between them. TCP/IP implementation exists for almost every type of hardware and operating system. Software is available to transmit IP datagram over network, hardware ranges from modems to fiber-optical cables.
The InetAaddress class
Whenever you want to establish a connection between two mechanism across the internet or nay other kind of network, address are fundamental and the
InnetAddress class is used to convert the domain name of an internet address into an object, which represents that address.
A sample program that displays the name and address of the local machine
|
Import java.net.*;
Class obtainingIP
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress adr;
adr=InetAddress.getLocalHost();
System.out.Println(“the info about your machine id:” +adr);
}
}
|
The output of the above program show IP address an the host name for the local host
Communicating with remote systems using UDP
By now we know about specifying an internet destination with the help pf the instances of InetAddress class, but the question is how you can communicate with remote systems. The problem is solved in several ways through the java.net package
Let us first see how we can communicate with remote systems using UDP datagram.
-
Writing client/server systems using UDP
-
To write server systems using UDP you have to follow the following basic steps
-
You have to create the datagram socket on a specific post
-
Invoke the receive() methods to wait for incoming pockets
-
According to the agreed protocaol, respond to received packets
-
Repeats step 2 or continue to step 5
-
Close the datagram socket
|
The following program is a simple datagram server which receives and sends the packets from clients
Import java.io.*;
Import java.net.*;
Import java.awt.*;
Import java.applet.*;
Import java.awt.event.*;
Public class UDPserver
{
public static void main(String args[]) throws Exception
{
DatagramSocket ds=new DatagramSocketc(10000);
DatagramPacket p1,p2;
BufferReader br=new BufferReader (new InputStreamReader (System,in));
Byte arr[]=new byte[255];
String input,output;
do
{
arr=new byte[255];
p1=new DatagramPacket(arr,255);
System.out.println(“message for client…………..”);
System.out.println(input);
if(input .charAt(0)==’*’)
{
break;
}
System.out.Println(“enter you message…”);
output=br.readLine();
arr=output.getBytes();
p2=new DatagramPacket(arr,arr.length,pl.getAaddress(),p1.getPort());
ds.send(p2);
}
while(true);
ds.close();
}
}
|
TO WRITE SERVER PROGRAM USING TCP THESE ARE THE STEPS:
Create the server socket and begin listening-
Call the accept () methods and get new listening
-
Create input and output streams for the returned socket
-
Conduct the conversation based on the agreed protocol
-
Close the client streams and socket
-
Go back to step 2 or continue to step 7
-
Close the server socket
|
Sample server system using TCP
Import java.io.*;
Import java.net.*;
Class TCPServer
{
public static void main(String s[]) throws Exception
{
ServerSocket sersoc=new ServerSocket(5000);
Socket soc=sersoc.accept();
InputStream is=soc.getInputStream();
DataIanputStream dis=new DataInputStream(is);
OutputStream os=soc.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
InputStreamReader isr=new InputStreamReader(System.in);
BufferReader br=new BufferReader(isr);
while(true)
{
System.out.println(“message: “+dis.readUTF(dis));
System.out.print(“send:”);
dos.writeUTF(br.readLine());
}
}
}
| |
To write a client program,use the following steps
-
Create the client socket connection
-
Acquire read and write streams for the socket
-
Use the streams according to the server’s protocol
-
Use the stream according to the server’s protocol
-
Close the streams
-
Close the socket
|
|
Inform Friend About This Site |