Networking
Introduction
§ Computer can communicate with
multiple computers transform different types of file and also share resources
among them
§ This is possible using
computer network
§ It can be wired or wireless
§ Distributed application is
popular concept in network
§ Java provides an environment
to develop distributed application
§ In java Java.net package is
available to work in networking.
§ This package provides many
classes, methods and interfaces to develop network application
Components of
network application
§ Server: it provides service to
the client. Request and responds to the client request.
§ Client: client initiate
request and send to the server
§ Protocol: set of rules
§ IP address: unique 32 bit
address
§ Port number: logical address
of program which run on RAM
Java.net package
§ It provides facility to create
network applications.
§ It supports TCP/IP and UDP
protocols
§ To support TCP/IP protocol
ServerSocket and Socket classes are available.
§ To support UDP, DatagramPacket
and DatagramSocket classes are available.
§ Other than that, this package
also provides URL, URLConnection and InetAddress class.
InetAddress
class
This class is used to get the host address and host name in networking
§ As we aware each machine in
network have unique IP address. The IP address is handled by the InetAdress
class of java.
Methods of InetAddress class
§ Static InetAddress
getLocalHost(): this method returns IP address of the localhost
Eg.
InetAddress i1= InetAddress.getLocalHost();
§ Byte[] getAddresses(): this
methods returns the IP address of the calling object in byte format.
Eg.
byte b[] = i1.getAddresses();
§ String getHostName(): this
method returns the host name for particular IP address in string format
§ String getHostAddress(): it
returns the host address for the particular IP address in string format.
§ Static InetAddress[ ]
getAllByName(String host): it returns the IP address for the given host in
array format
/* A Simple example of
InetAdderess Class */
import java.net.*;
class IPAdress_demo
{
public static void main(String args[])
{
try
{
InetAddress
ip_add=InetAddress.getLocalHost();
System.out.println("Local Ip Add is" + ip_add);
System.out.println("Hostname is" +
ip_add.getHostName());
System.out.println("Host Address
is" + ip_add.getHostAddress());
}
catch(UnknownHostException e)
{
System.out.println("problem");
}
}
}
Socket
Programming
§ To communicate between two computers
in network the socket mechanism is used.
§ A socket connects two
different java programs which may be run on same computers or on network
computers.
§ The communication is possible
after the connection is established.
§ Once the connection is
established data can be transferred or received by the machines
§ In socket program one program
acts as server and other acts as client
TCP / IP Socket
§ In TCP/IP the server is
handled by ServerSocket class and the client is handled by Socket class.
§ In TCP/IP the communication is
connection oriented and reliable.
ServerSocket
Class
§ This class acts as the server
and handles the request call comes from the client.
§ When the request arise at the
server, it process it and response to requested client.
Constructor:
§ ServerSocket(int portnumber)
§ ServerSocket(int portnumber,
int num_of_que)
Methods of
ServerSocket class
§ Socket accept(): This mehod
listen for new connection request.
§ InetAddress getInetAddress():
This method returns the Local Address of the ServerSocket.
§ Int getLocalPort(): This
method returns the port number on which the socket is listening.
§ Void close(): This method
close the Socket
Socket Class
§ This class works as client and
send request to the server.
§ The client socket communicates
with the ServerSocket.
Constructors:
§ Socket(String host_nm, int
port_no)
§ Socket(InetAddress obj, int
port_no)
Methods of
Socket class
§ InetAddress getLocalAddress()
This
method returns the local IP address on which the socket is connected.
§ InetAccress getInetAddress()
This
method returns the remote IP Address on which the socket is connected
§ Int getLocalPort()
This
method returns the Local port number on which the Socket is connected.
§ Int getPort(): This method returns the remote port number on
which the socket is connected.
§ void close(): This method
close the Socket.
/* TCP Client socket
program */
import java.io.*;
import java.net.*;
class client_socket_demo
{
public static void main(String args[])
{
try
{
Socket s = new
Socket(InetAddress.getLocalHost(),8);
InputStream is= s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println("Client receive :
" + dis.readUTF());
s.close();
}
catch(Exception e)
{
System.out.println("Exception"
+ e);
}
}
}
/* TCP Server Socket
program */
import java.io.*;
import java.net.*;
class server_socket_demo
{
public static void main(String args[])
{
try
{
System.out.println("\n Server Started
\n");
ServerSocket ss= new ServerSocket(8);
Socket s= ss.accept();
OutputStream os = s.getOutputStream();
DataOutputStream dos = new
DataOutputStream(os);
System.out.println("Server has send
Message ");
dos.writeUTF("Hello !! I am
server");
s.close();
}
catch(Exception e)
{
System.out.println("Exception");
}
}
}
UDP(User
Datagram Protocol)
§ The second way to sending and
receiving data is UDP.
§ Datagram is an independent and
self oriented socket, whose receiving time and content are not sure.
§ DatagramPacket and
DatagramSocket classes are used for sending and receiving packet over network.
DatagramPacket
class
§ The DatagramPacket class
breaks the message(data) into small message packets of certain length.
§ It implements connection less
packet delivery service.
§ The packet transmitted into
network move from source to destination.
Constructor:
§ DatagramPacket(byte[] buf, int
len)
§ DatagramPacket(byte[] buf, int
len,InetAddress add, int port_no)
Methods of
DatagramPacket
§ byte[] getData(): This method
returns the data buffer being send or the data that has been received in byte
format.
§ Int getPort(): it returns the
port number of the host on which data is send or received by the datagram.
§ Int getLength(): This method returns the length of data which
is sent or received by the datagram.
§ InetAddress getAddress(): It
returns the address of computer which send or received data by datagram.
DatagramSocket
class
§ The DatagramSocket class sends
and recieves the datagram packet to and from the network.
§ The main task of DataramSocket
class is send and receive packets.
§ Constructors:
§ DatagramSocket()
§ DatagramSocket(int port)
Methods:
§ void send(DatagramPacket dp):
This method sends the datagram packet using socket.
§ void receive(DatagramPacket
dp): This method receives the datagram packet using socket.
§ void connect(InetAddress add,
int port_no) : this method establish a connection with remote IP address and
port number.
§ void disconnect(): this method
disconnect the socket
§ void close(): this method
close the socket.
/* UDP Client program */
import java.net.*;
import java.io.*;
class
datagram_client_program
{
public static void main(String args[]) throws
IOException
{
DatagramSocket ds = new DatagramSocket(26);
DataInputStream dis = new
DataInputStream(System.in);
DatagramPacket dpout;
InetAddress remote_add
=InetAddress.getLocalHost();
byte[] outbuf = new byte[250];
System.out.println ("Client
program");
while(true)
{
String s= dis.readLine();
outbuf = s.getBytes();
dpout = new DatagramPacket(outbuf,
outbuf.length, remote_add, 25);
ds.send(dpout);
if((s.trim()).equals("quit"))
break;
byte[] inbuf = new byte[250];
DatagramPacket dpin = new
DatagramPacket(inbuf,250);
ds.receive(dpin);
String
s1 = new String(dpin.getData());
s1=s1.trim();
if(s1.equals("quit"))
break;
System.out.println("Client recieved from
server " +s1);
}
ds.close();
}
}
/* UDP Server Program */
import java.net.*;
import java.io.*;
class
datagram_server_program
{
public static void main(String args[]) throws
IOException
{
DatagramSocket ds = new DatagramSocket(25);
DataInputStream dis = new
DataInputStream(System.in);
DatagramPacket dpout;
InetAddress remote_add = null;
int remote_port;
System.out.println ("Server
program");
while(true)
{
byte[] inbuf = new byte[250];
DatagramPacket dpin = new
DatagramPacket(inbuf,250);
ds.receive(dpin);
String s = new String(dpin.getData());
s=s.trim();
if(s.equals("quit"))
break;
System.out.println("Server
recieved from client " +s);
byte[]
outbuf = new byte[2000];
String s1= dis.readLine();
outbuf = s1.getBytes();
remote_add = dpin.getAddress();
remote_port=dpin.getPort();
dpout =
new DatagramPacket(outbuf, outbuf.length, remote_add, remote_port);
ds.send(dpout);
if(s1.equals("quit"))
break;
}
ds.close();
}
}