Documente Academic
Documente Profesional
Documente Cultură
TECHNOLOGY
KALITHEERTHALKUPPAM , MADAGADIPET , PUDUCHERRY-605 107
LAB MANUAL
FOR
COMPUTER NETWORKS
List of Experiments
COMPUTER NETWORKS LAB (2012-13 EVEN SEM)
[IT P63]
Year/Sem: III/VI
Sl.
Proposed
No
Date
1
21.12.12
2
21.12.12
3
4
26.12.12
26.12.12
5
6
02.01.13
02.01.13
7
8
9
09.01.13
09.01.13
23.01.13
10
23.01.13
11
30.01.13
12
13
06.02.13
06.02.13
14
13.02.13
15
16
17
27.02.13
27.02.13
Staff-in-Charge
Actual
Date
Experiment
Batch: 2010-2014
Proposed
Sessions
1
1
1
1
1
1
1
1
1
1
1
HOD
METHOD
Inet Address get
Inet Address()
Inet getport()
Inet getLocalport()
The following I/O Streams related methods are also available in the socket.
InputStream getInput Stream()
Return the InputStream associated with the
invoking socket.
OutputStream get outputstream()
Return the output stream associated with the
invoking socket.
Void close
Close both the inputstream and outputstream.
INET ADDRESSING
Every computer on the internet has an address. An internet is a number that uniquely identifies
each computer on the net. There are 32 bit in an IP Address and we often refer to them has sequence
of four number between 0 and 255 separated by the data. This makes them easier to remember
because they are not randomly assigned, they are hierarchically assigned. The first few bits define
which class of network lettered A,B,C,D or E . The address represents internet or on a class C
network since there are 2 million networks in class C .
Static Inet Address get localhost() throws Return the Inet address object that represents
unknown host Exception
the localhost
Static Inet Address get by name(String host Returns on an InetAddress for a host name
name)throws unknown host Exception
passed to it
Static Inet Address get all by name host Return an array of Inet Address that represent
Exception
all of the address,meta particular name.
SOME OTHER METHOD
Boolean equals(object other)
Byte[]get address()
String get Host Address()
Int hascode()
Boolena is multicost address
FINDING IP ADDRESS
SOURCE CODE
import java.io.*;
import java.net.*;
class address
{
public InetAddress ip;
public static void main(String args[])throws UnknownHostException
{
// To get local machines IP address
InetAddress ip=InetAddress.getLocalHost();
System.out.println("\n IP address is :"+ip);
// Gets the local machines name
String s1=ip.getHostName();
System.out.println("system number is:"+s1);
//To receive the other machines name
InetAddress ip1=InetAddress.getByName("system 10");
System.out.println("\n name of other system is :"+ip1);
}
}
OUTPUT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac address.java
D:\Chitra\Java>java address
IP address is : student1/172.16.11.136
system number is: student1
RESULT
Thus the java program is executed and output is verified successfully.
CLIENT
1. Start the program.
2. Import java.net package and other packages.
3. Create objects for Socket and DataInputStream to receive the server data.
4. Run an loop to receive the data from server and store it in a string using DataInputStream.
5. Display the server data and exit when an end or exit message is encountered.
6. Stop the program.
SOURCE CODE
SERVER
import java.io.*;
import java.net.*;
class tcp1server
{
public static void main(String a[])throws IOException
{
ServerSocket ss=new ServerSocket(8000);
//Opens the socket
Socket s=ss.accept();
DataInputStream in=new DataInputStream(System.in);
PrintStream dos=new PrintStream(s.getOutputStream());
while(true)
{
System.out.println("enter message to send:");
//Reads the input from the input device
String str=in.readLine();
dos.println(str);
//checks for end of message
if(str.equals("end"))
{
//Closes the socket
ss.close();
break;
}
}
}
}
OUTPUT
SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac server.java
D:\Chitra\Java>java server
enter message to send:
GOOD MORNING HAVE A NICE DAY
enter message to send:end
D:\Chitra\Java>
CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java> javac client.java
D:\Chitra\Java> java client
Message Received:GOOD MORNING HAVE A NICE DAY
Message Received:end
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
CLIENT
1. Start the program.
2. Import java.net package and other packages.
3. Create objects for Socket, Socket and PrintStream to transfer the client data.
4. Create objects for Socket and DataInputStream to receive the server data.
5. Using PrintStream transfer the data in OutputStream via a port.
6. Run an loop to send the data from server until an end or exit string is transferred.
7. Using same loop receive the data from server and store it in a string using DataInputStream.
8. Print the received server data and exit when an end or exit message is encountered.
9. Stop the program.
SERVER
import java.io.*;
import java.net.*;
import java.lang.*;
class tcp2server
{
public static void main(String a[])throws IOException
{
ServerSocket ss=new ServerSocket(8000);
//Opens the socket
Socket s=ss.accept();
PrintStream dos=new PrintStream(s.getOutputStream());
DataInputStream in=new DataInputStream(System.in);
DataInputStream inn=new DataInputStream(s.getInputStream());
while(true)
{
System.out.println("enter the msg to send: ");
//Reads the input
String str=in.readLine();
dos.println(str);
//Checks for end of message
if(str.equals("end"))
{
//Closes the socket
ss.close();
break;
}
String str1=inn.readLine();
System.out.println("msg received"+str1);
if(str1.equals("end"))
{
ss.close();
break;
}
}
}
}
OUTPUT
SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac server1.java
D:\Chitra\Java>java server1
enter the msg to send:
HAI FRIENDS
msg received:HELLO
enter the msg to send:end
D:\Chitra\Java>
CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac client1.java
D:\Chitra\Java>java client1
msg received:HAI FRIENDS
enter the msg to send:
HELLO
enter the msg to send:end
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
RECEIVER
import java.io.*;
import java.net.*;
class udp1receiver
{
DatagramSocket ds;
DatagramPacket dp;
byte buff[]=new byte[1024];
String str;
public void receive() throws IOException
{
ds=new DatagramSocket(8000);
while(true)
{
dp=new DatagramPacket(buff,buff.length);
ds.receive(dp);
str=new String (dp.getData(),0,0,dp.getLength());
System.out.println(str);
System.out.println("InetAddress:"+dp.getAddress());
}
}
public static void main(String args[])throws Exception
{
udp1receiver re=new udp1receiver();
re.receive();
}
}
OUTPUT
SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp1sender.java
D:\Chitra\Java>java udp1sender
Enter the msg:
IT KINGS
do u want to continue:yes or no
no
D:\Chitra\Java>
CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp1receiver.java
D:\Chitra\Java>java udp1receiver
IT KINGS
InetAddress:/172.16.11.99
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
CLIENT
import java.io.*;
import java.net.*;
import java.lang.*;
class udp2receiver
{
public static void main(String a[])throws IOException
{
DatagramSocket ds=new DatagramSocket(1234);
byte b[]=new byte[255];
while(true)
{
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String msg=new String(dp.getData());
System.out.println("Msg Received:"+msg);
InetAddress add=dp.getAddress();
int port=dp.getPort();
System.out.println("Enter a line of text to send:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String msg1=br.readLine();
byte b1[]=msg1.getByte();
DatagramPacket dp1=new DatagramPacket(b1,b1.length,add,port);
ds.send(dp1);
if(msg1.equals("exit"))
System.exit(0);
}
}
OUTPUT
SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp2sender.java
D:\Chitra\Java>java udp2sender
enter the msg:
GOOD MORNING
received msg:HAVE A NICE DAY
enter the msg:end
D:\Chitra\Java>
CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp2receiver.java
D:\Chitra\Java>java udp2receiver
Msg Received:GOOD MORNING
Enter a line of text to send:
HAVE A NICE DAY
Msg Received:end
D:\Chitra\Java>
RESULT:
Thus the java program is executed and output is verified successfully.
ECHO
AIM
To write a program in java to demonstrate the ECHO command.
ALGORITHM
ECHOSERVER
1. Start the program.
2. Import java.net and other necessary packages.
3. Create objects for DataInputStream, Socket and PrintWriter to receive client message and
send it back.
4. Store the message in a string and print the message using print() method.
5. Send the same received message to the client using PrintWriter and Socket.
6. When the received character is ., then stop sending the data back.
7. Stop the program.
ECHOCLIENT
1. Start the program.
2. Import java.net and other necessary packages.
3. Create objects for ServerSocket and Socket to send the message.
4. Create objects for PrintStream to write message from client to server.
5. Get the user input and store it in a string.
6. Print the string in the Socket using PrintStream to be received by the server.
7. Using the Print() method, receive the client echo message and print it.
8. If the user input is ., then stop sending the data.
9. Stop the program.
ECHO
SOURCE CODE
ECHO SERVER
import java.io.*;
import java.net.*;
class echoserver
{
public static void main(String args[])
{
try
{
Socket s=null;
ServerSocket ss=new ServerSocket(8000);
s=ss.accept();
System.out.println(s);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter print=new PrintWriter(s.getOutputStream());
int i=1;
while(i>0)
{
String str=br.readLine();
if(str.equals("."))
break;
System.out.println("msg received by client:"+str);
print.println(str);
print.flush();
}}
catch(IOException e)
{
System.out.println("\n error:"+e);
}
}
}
ECHO CLIENT
import java.io.*;
import java.net.*;
class echoclient
{
public static void main(String a[])
{
try
{
Socket s=new Socket("LocalHost",8000);
DataInputStream in=new DataInputStream(System.in);
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter print=new PrintWriter(s.getOutputStream());
System.out.println("\n msg to be echo:");
String str=br1.readLine();
print.println(str);
print.flush();
System.out.println(br2.readLine());
}
catch(UnknownHostException e)
{}
catch(IOException e)
{
System.out.println("\n error:"+e);
}
}
}
OUTPUT
ECHOCLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac echoclient.java
D:\Chitra\Java>java echoclient
msg to be echo:
GOD IS GREAT
GOD IS GREAT
D:\Chitra\Java>
ECHOSERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac echoserver.java
D:\Chitra\Java>java echoserver
Socket[addr=/127.0.0.1,port=1623,localport=8000]
msg received by client:GOD IS GREAT
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
PING COMMAND
AIM
To write a program in java to demonstrate the usage of PING command.
ALGORITHM
PINGSERVER
1.
2.
3.
4.
5.
6.
7.
8.
PINGCLIENT
1. Start the program.
2. Import java.net and other necessary packages.
3. Initialize the ping client with both sockets as null value.
4. Start the socket.
5. Get the IP address of the server.
6. Ping the server.
7. At the receiver end, the server is pinged.
8. Stop the program.
PING COMMAND
SOURCE CODE
PING SERVER
import java.io.*;
import java.net.*;
public class pingserver
{
public static void main(String a[])
{
String line1,line2;
int i;
ServerSocket es;
DataInputStream di;
PrintStream ps;
Socket csoc;
es=null;
csoc=null;
try
{
es=new ServerSocket(9999);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("ping server");
try
{
csoc=es.accept();
di=new DataInputStream(csoc.getInputStream());
ps=new PrintStream(csoc.getOutputStream());
for(i=0;i<4;i++)
{
line1=di.readLine();
System.out.println("pinged by client");
ps.println(line1+"reply from host:bytes=3<time<1ms TT<=128");
}
di.close();
ps.close(); }
catch(Exception e)
{
System.out.println(e);
}}}
PING CLIENT
import java.io.*;
import java.net.*;
public class pingclient
{
public static void main(String args[])
{
PrintWriter out=null;
int i,j,k;
BufferedReader networkIn=null;
try
{
System.out.println("enter the IP address:");
DataInputStream in = new DataInputStream(System.in);
String ip = in.readLine();
Socket thesocket = new Socket(ip, 9999);
networkIn = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(thesocket.getOutputStream());
System.out.println("\npinging" + ip + "with 32 bytes of data\n");
for (i = 0; i < 4; i++)
{
out.println(ip);
out.flush();
String inp = networkIn.readLine();
if (inp != null)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
}
System.out.println("reply from" + inp);
}
else
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
System.out.println("\nrequest time out");
}
}
}
}
}
catch (IOException e)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 1000; j++)
{
for (k = 0; k < 5000; k++)
{
}
}
System.out.println("\nrequest timed out");
}
}
try
{
if(networkIn!=null)
networkIn.close();
if(out!=null)
out.close();
}
catch(Exception e){
System.out.println("\nrequested time out");
}
}
}
OUTPUT
PING CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac pingclient.java
D:\Chitra\Java>java pingclient
enter the IP address:
192.168.1.10
pinging192.168.1.10with 32 bytes of data
5
reply from5
8
reply from8
9
reply from9
4
reply from4
D:\Chitra\Java>
PINGSERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac pingserver.java
D:\Chitra\Java>java pingserver
ping server
pinged by client
pinged by client
pinged by client
pinged by client
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
CLIENT
1. Start the program.
2. Create a client socket that connects to the required host and port.
3. Using input streams read message given by server and print it.
4. Using input streams, get message from user to be given to the server.
5. Use output streams to write message to the server.
6. Stop the program.
SOURCE CODE
TALKSERVER
import java.io.*;
import java.net.*;
public class talkserver
{
public static void main(String args[])throws Exception
{
ServerSocket ecsvr=null;
String line1,line2;
DataInputStream dis=null;
PrintStream pts=null;
Socket clsckt=null;
BufferedReader in1=null;
ecsvr=new ServerSocket(9999);
System.out.println("TALK SERVER");
System.out.println("----------------------");
clsckt=ecsvr.accept();
dis=new DataInputStream(clsckt.getInputStream());
pts=new PrintStream(clsckt.getOutputStream());
System.out.println("Node successfully connected..");
while(true)
{
line1=dis.readLine();
System.out.println("Message Received");
System.out.println("The Message ="+line1);
in1=new BufferedReader(new InputStreamReader(System.in));
line2=in1.readLine();
if(line2.equals("end"))
{
break;
}
pts.println(line2);
pts.flush();
System.out.println("Message sent successfully");
}
dis.close();
pts.close();
}
}
TALK CLIENT
import java.io.*;
import java.net.*;
public class talkclient
{
public static void main(String args[])
{
PrintWriter out=null;
String line1;
BufferedReader networkln=null;
try
{
Socket theSocket=new Socket("LocalHost",9999);
networkln=new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
BufferedReader userln=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(theSocket.getOutputStream());
System.out.println("TALK CLIENT");
System.out.println("--------------------");
while(true)
{
System.out.println("send message to server: ");
String theLine=userln.readLine();
if(theLine.equals("end"))
break;
out.println(theLine);
out.flush();
System.out.println("message sent successfully");
System.out.println(" ");
System.out.println("message received from talkserver: "+networkln.readLine());
} }
catch(IOException e)
{
System.err.println(e);
} try
{
if(networkln!=null)
networkln.close();
if(out!=null)
out.close();
} catch(Exception e)
{
System.err.println(e);
} } }
OUTPUT
TALKSERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac talkerver.java
D:\Chitra\Java>java talkserver
TALK SERVER
---------------------Node successfully connected.
Message Received
The Message =HAI
HI..
Message sent successfully
Message Received
The Message =HOW ARE YOU
I AM FINE
Message sent successfully
Message Received
The Message =K BYE
end
D:\Chitra\Java>
TALKCLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac talkclient.java
D:\Chitra\Java>java talkclient
TALK CLIENT
-------------------send message to server: HAI
message sent successfully
message received from talkserver: HI..
send message to server: HOW ARE YOU
message sent successfully
message received from talkserver: I AM FINE
send message to server: K BYE
message sent successfully
send message to server:
end
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
IMPLEMENTATION OF FTP
AIM:
To write a java program to perform File Transfer Protocol.
ALGORITHM
SERVER SIDE
1. Import the java packages and create class fileserver.
2. String of argument is passed to the args[].
3. Create a new server socket and bind it to the port.
4. Accept the client connection at the requested port.
5. Get the filename and stored into the BufferedReader.
6. Create a new object class file and readline.
7. If File is exists then FileReader read the content until EOF is reached.
8. Else Print FileName doest exits.
9. End of main.
10. End of FileServer class.
CLIENT SIDE
1. Import the java packages and create class fileClient.
2. String of argument is passed to the args[].
3. The connection between the client and server is successfully established.
4. The object of a BufferReader class is used for storing data content which have been
retrieved from socket object s.
5. The content are read and stored in inp until the EOF is reached.
6. The content of file are displayed in displayed in client window and the connection is
closed.
7. End of main.
8. End of Fileclient class.
IMPLEMENTATION OF FTP
SOURCE CODE
SERVER
import java.io.*;
import java.net.*;
public class fileserver1
{
public static void main(String args[])throws IOException
{
ServerSocket s1=null;
try
{
s1=new ServerSocket(1187);
}
catch(IOException u1)
{
System.out.println("could not found port 1187");
System.exit(1);
}
Socket c=null;
try
{
c=s1.accept();
System.out.println("connection frame" +c);
}
catch(IOException e)
{
System.out.println("accept failed");
System.exit(1);
}
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the text file name");
String s=sin.readLine();
File f=new File(s);
if(f.exists())
{
BufferedReader in=new BufferedReader(new FileReader(s));
String v;
while((v=in.readLine())!=null)
{
out.write(v);
out.flush();
}
CLIENT
import java.io.*;
import java.net.*;
public class fileclient1
{
public static void main(String args[])throws IOException
{
Socket s=null;
BufferedReader b=null;
try
{
s=new Socket(InetAddress.getLocalHost(),1187);
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(Exception u)
{
System.out.println("the file is received");
System.out.println("don't know host");
System.exit(1);
}
String inp;
while((inp=b.readLine())!=null)
{
System.out.println("the content of the file is");
System.out.println(inp);
System.out.println("the file is received successfully");
}
b.close();
s.close();
}
}
OUTPUT:
SERVER
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac fileserver1.java
C:\Program Files\Java\jdk1.6.0\bin>java fileserver1
connection frameSocket[addr=/127.0.0.1,port=1056,localport=1187]
enter the text file name
HAI.txt
the file send successfully
C:\Program Files\Java\jdk1.6.0\bin>
CLIENT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac fileclient1.java
C:\Program Files\Java\jdk1.6.0\bin>java fileclient1
the content of the file is
GOD LOVE'S EVERY ONE IN THE WORLD.
the file is received successfully
C:\Program Files\Java\jdk1.6.0\bin>
RESULT:
Thus the java program is executed and output is verified successfully.
AIM:
To develop a java program for implementing the remote method invocation.
ALGORITHM:
1. Start the program.
2. Establish the connection between the client and the server to calculate the factorial
operation.
3. In implement() it calls the inter() which throws remote exception thrown by fact().
4. In the client URL has been specified and declared as string.
5. It calls the inter() to connect with the implement() and get the result.
6. Stop.
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
public class rmiserver
{
public static void main(String args[]) throws RemoteException
{
try
{
implement i=new implement();
Naming.rebind("server",i);
System.out.println("server reasdy");
}
catch(Exception e)
{
System.out.println("exception:"+e);
}
}
}
CLIENT:
import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class rmiclient
{
public static void main(String args[]) throws RemoteException
{
try
{
String host="rmi://LABCSE105/server";
inter a=(inter)Naming.lookup(host);
System.out.println("Enter a number:");
DataInputStream ds=new DataInputStream(System.in);
int n=Integer.parseInt(ds.readLine());
a.fact(n);
}
catch(Exception e)
{
}
}
}
IMPLEMENT:
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class implement extends UnicastRemoteObject implements inter
{
public implement() throws RemoteException
{
}
public void fact(int n) throws RemoteException
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
System.out.println("Factorial of "+n+" is "+f);
}
}
INTERFACE:
import java.rmi.*;
import java.rmi.server.*;
public interface inter extends Remote
{
public void fact(int n) throws RemoteException;
}
OUTPUT
SERVER
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
RESULT:
Thus the java program is executed and output is verified successfully.
AIM:
{
try
{
InetAddress addr=InetAddress.getByName("Localhost");
System.out.println(addr);
Socket connection=new Socket(addr,500);
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
BufferedInputStream in=new BufferedInputStream(connection.getInputStream());
BufferedInputStream inn=new BufferedInputStream(connection.getInputStream());
BufferedReader ki=new BufferedReader(new InputStreamReader(System.in));
int flag=0;
System.out.println("connect");
System.out.println("enter the no of frames to be requested to server:");
int c=Integer.parseInt(ki.readLine());
out.write(c);
out.flush();
int i,jj=0;
while(jj<c)
{
i=in.read();
System.out.println("received frame no"+i);
System.out.println("sending acknowledgement for frame no"+i);
out.write(i);
out.flush();
jj++;
}
out.flush();
in.close();
inn.close();
out.close();
System.out.println("quiting");
}
catch(Exception e)
{
System.out.println(e);}}}
OUTPUT
BITCLIENT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac bitclient.java
C:\Program Files\Java\jdk1.6.0\bin>java bitclient
Localhost/127.0.0.1
connect
RESULT:
Thus the java program is executed and output is verified successfully..
AIM:
To perform remote command execution (ie) command entered by the user at the client should
be executed by the server.
ALGORITHM:
SERVER:
1. Create server and client socket.
2. Create a runtime object using getRuntime() method.
3. Create a process object p.
4. Read the command entered by the user at the client using input streams.
5. Execute the command using the exec().
CLIENT:
1.
2.
3.
4.
5.
SOURCE CODE
SERVER
import java.io.*;
import java.net.*;
import java.lang.*;
class RCEserver
{
public static void main(String a[])throws
IOException,UnknownHostException,InterruptedException
{
ServerSocket ss=new ServerSocket(1000);
Socket cs=ss.accept();
Runtime r=Runtime.getRuntime();
Process p=null;
InputStream is=cs.getInputStream();
DataInputStream dis=new DataInputStream(is);
String s=dis.readLine();
System.out.println(s);
p=r.exec(s);
p.waitFor();
}
}
CLIENT
import java.io.*;
import java.net.*;
import java.lang.*;
OUTPUT
SERVER
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
RESULT:
Thus the java program is executed and output is verified successfully.
BROADCASTING
AIM:
To write a java program to send a single message to multiclient.
ALGORITHM:
SENDER:
1.
2.
3.
4.
5.
6.
7.
8.
CLIENT:
1.
2.
3.
4.
5.
6.
BROADCASTING
SOURCE CODE
SERVER
import java.net.*;
import java.io.*;
CLIENT:
import java.net.*;
import java.io.*;
public class client
{
public static void main(String args[]) throws Exception
{
DatagramSocket socket=new DatagramSocket(5000);
while(true)
{
try
{
byte[] buf=new byte[1025];
DataInputStream in=new DataInputStream(System.in);
String msg=null;
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket.receive(packet);
msg=new String(packet.getData(),0,packet.getLength());
InetAddress addr=packet.getAddress();
System.out.println("From :"+addr+"message"+msg);
if(msg.equalsIgnoreCase("Bye"))
{
System.out.println("Quits");
return;
}
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
}
}
OUTPUT
BROADSERVER
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0_14\bin
C:\Program Files\Java\jdk1.6.0_14\bin>javac broadsender.java
RESULT:
Thus the java program is executed and output is verified successfully.
ALGORITHM:
1.
2.
3.
4.
5.
f[i]=iframe%2;
iframe=iframe/2;
i++;
}
if(iframe==1)
f[i]=iframe;
System.out.println("Transmitted string:");
for(i=flen;i>=0;i--)
System.out.print(f[i]);
System.out.println("\n enter the received string:");
data=in.readLine();
flen=data.length()-1;
for(i=0;i<data.length();i++)
f[i]=((int)(data.charAt(i)))-48;
p=0;
sum=0;
for(i=flen;i>=0;i--)
{
sum=sum+(f[i]*(int)Math.pow(2,p));
p=p+1;
}
iframe=sum;
irem=iframe%igen;
if(irem==0)
System.out.println("Message received with no error");
else
System.out.println("Message received with error");
}
}
OUTPUT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac crc.java
C:\Program Files\Java\jdk1.6.0\bin>java crc
Enter the frame:
10101011
RESULT:
SHOREST PATH
AIM:
To implement the shortest path routing (Dijkstras Algorithm) to find the shortest path between
the nodes.
ALGORITHM:
1. Dijkstras shortest path algorithm computes all shortest path from a single node.
2. It can also be used for the all pairs shortest path problem,by the simple expledient of
applying it N times once to each vertex.
3. Get the number of nodes in the network for which the shortest path is to be calculated.
4. Represent the nodes that are connected by cost value(Number of hopes delay bandwidth,
etc.,) and nodes that are not connected by infinite value in an adjacent matrix.
5. To find the shortest path between node follow the steps as stated below.
a. Initially,T=V, where T= set of nodes and V= nodes for which the
shortest path
is to be found.
b. At each step of the algorithm the vertex in T with the smalled d value is removed
from T.
c. Each neighbour of in T is examined would be shorter than the currently best known
path.
6. The last paths that remain between the nodes are the shortest path between
the source node and the destination nodes.
OUTPUT
SOURCE CODE:
import java.net.*;
import java.io.*;
class spath
{
public static void main(String args[]) throws IOException
{
int n,s,d,i,j,y=0,sd=100;
int[] in=new int[10];
int[][] m=new int[5][5];
int[] dis=new int[10];
int[] path=new int[10];
DataInputStream a=new DataInputStream(System.in);
System.out.println("Enter the no of vertex:");
n=Integer.parseInt(a.readLine());
System.out.println("Enter the source vertex:");
s=Integer.parseInt(a.readLine());
System.out.println("Enter the destination vertex:");
d=Integer.parseInt(a.readLine());
for(i=1;i<n;i++)
{
j=1;
while(j<n)
{
System.out.println("Enter the distance between"+i+ "and" +(j+1));
m[i][j+1]=Integer.parseInt(a.readLine());
m[j+1][i]=m[i][j+1];
j++;
}
}
for(i=1;i<=n;i++)
{
in[i]=0;
dis[i]=m[s][i];
if(m[s][i]!=0)
path[i]=s;
}
in[s]=1;
dis[s]=0;
for(i=2;i<n;i++)
{
for(j=1;j<=n;j++)
{
if(in[j]==0)
{
if(dis[j]<sd)
{
sd=dis[j];
y=j;
}
}
}
in[y]=1;
for(j=1;j<=n;j++)
{
if((in[j]==0)&&(m[y][j]!=0))
{
if((dis[y]+m[y][j])<dis[j])
{
dis[j]=dis[y]+m[y][j];
path[j]=y;
}
}
}
}
System.out.println(" "+d+"<----");
i=d;
while(path[i]!=s)
{
System.out.println(" "+path[i]+"<----");
i=path[i];
}
System.out.println(" "+s);
System.out.println("Distance is "+dis[d]);
}
}
OUTPUT:
C:\Documents and Settings\LABCSE1>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac spath.java
C:\Program Files\Java\jdk1.6.0\bin>java spath
Enter the no of vertex:
3
Enter the source vertex:
1
Enter the destination vertex:
3
Enter the distance between1and2
2
Enter the distance between1and3
1
Enter the distance between2and2
0
Enter the distance between2and3
0
3<---1
Distance is 1
C:\Program Files\Java\jdk1.6.0\bin>java spath
Enter the no of vertex:
4
Enter the source vertex:
1
Enter the destination vertex:
4
Enter the distance between1and2
2
Enter the distance between1and3
3
Enter the distance between1and4
5
Enter the distance between2and2
0
Enter the distance between2and3
0
4<---2<---1
Distance is 4
C:\Program Files\Java\jdk1.6.0\bin>
RESULT:
AIM:
To write a java program to implement client server cryptography.
ALGORITHM:
SERVER:
1.
2.
3.
4.
5.
6.
7.
8.
9.
1.
2.
3.
4.
5.
6.
7.
8.
Send the encrypted string and value to the client using PrintStream an Socket.
After sending the string and key exit the program.
CLIENT:
Start the program.
Import .net and other necessary packages.
Declare objects for Socket and InputStream to receive the string and key from
server.
Store the string and key to different variables.
Read the string character wise to its length.
Request the user to enter the key for decryption.
Compare the key with the received key;
If both keys are equal then,
-Convert the each charater of the string to integer and deduce the key value.
- Convert the integer value to character and form it into a string and display
the decrypted string.
Else
-Print the message as cannot decrypt, wrong message and quit.
9. End the program.
SOURCE CODE
SERVER
import java.io.*;
import java.net.*;
class encrypserver
{
public static void main(String a[])throws Exception
{
try
{
ServerSocket ss=new ServerSocket(1000);
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s2="",s3="";
int i=0,k;
System.out.println("\n Enter a String :");
String s1=br.readLine();
System.out.print("\n Enter the key:");
k=Integer.parseInt(br.readLine());
while(i<s1.length())
{
char c1=s1.charAt(i++);
int n=(int)c1+k+1;
c1=(char)n;
s2+=c1;
n=(int)c1-k-1;
c1=(char)n;
s3+=c1;
}
System.out.print("\n Encrypted String :"+s2);
ps.println(s2);
ps.println(k);
ps.close();
s.close();
ss.close();
}
catch(Exception e)
{
System.out.print("\n Error :"+e);
}
}
}
CLIENT
import java.io.*;
import java.net.*;
class decrypclient
{
public static void main(String args[])throws Exception
{
try
{
OUTPUT
SERVER
C:\Documents and Settings\LABCSE1>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac encrypserver.java
C:\Program Files\Java\jdk1.6.0\bin>java encrypserver
Enter a String :
GOD IS GREAT
Enter the key:8
Encrypted String :PXM)R\)P[NJ]
C:\Program Files\Java\jdk1.6.0\bin>
CLIENT
C:\Documents and Settings\LABCSE1>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac decrypclient.java
C:\Program Files\Java\jdk1.6.0\bin>java decrypclient
Encrypted sender String :PXM)R\)P[NJ]
Enter the key :8
Decrypted String :GOD IS GREAT
C:\Program Files\Java\jdk1.6.0\bin>
RESULT:
Thus the java program is executed and output is verified successfully.