network lab record print

45
huuCONTENTS S.No . DATE NAME OF THE EXPERIMENT PAGE NO. REMARKS 1. Retrieving Data with URLs 2a. String Transfer between two applications - TCP 2b. String Transfer between two applications - UDP 3. File Transfer using - TCP 4a. Implementation of Echo 4b. Implementation of Ping 4c. Implementation of Talk 5. File Remote Command Execution 6. ARP – Address Resolution Protocol 7 RARP – Reverse Address Resolution Protocol 8. RMI Implementation for a given function. 9 Implementation of shortest path routing algorithm. 10 Implementation of sliding window protocol.

Upload: baskar-mutiah

Post on 28-Mar-2015

630 views

Category:

Documents


1 download

TRANSCRIPT

huuCONTENTS

S.No. DATE NAME OF THE EXPERIMENTPAGE

NO.REMARKS

1. Retrieving Data with URLs

2a. String Transfer between two applications - TCP

2b. String Transfer between two applications - UDP

3. File Transfer using - TCP

4a. Implementation of Echo

4b. Implementation of Ping

4c. Implementation of Talk

5. File Remote Command Execution

6. ARP – Address Resolution Protocol

7 RARP – Reverse Address Resolution Protocol

8. RMI Implementation for a given function.

9 Implementation of shortest path routing algorithm.

10 Implementation of sliding window protocol.

SOURCE CODE:URL:import java.awt.event.*;import javax.swing.*;import java.awt.*;

public class url extends JFrame implements ActionListener{JTextField tf;

url(){JFrame f = new JFrame();JPanel p= new JPanel();JLabel l1=new JLabel("URL:");tf= new JTextField(10);JButton b = new JButton("click");p.add(l1);p.add(tf);p.add(b);b.addActionListener(this);f.getContentPane().add(p);f.pack();f.setVisible(true);}

public void actionPerformed(ActionEvent ae){BareBonesBrowserLaunch.openURL(tf.getText().trim());}

public static void main(String[] args){url s=new url();}

}

BareBonesBrowserLaunch:

import java.lang.reflect.Method;import java.util.Arrays;import javax.swing.JOptionPane;

public class BareBonesBrowserLaunch{public static void openURL(String url){try{Runtime.getRuntime().exec("rundll32.url.dll,FileProtocolHanler "+url);}catch(Exception e){}}}

OUTPUT:

Source code:Client:import java.io.*;import java.net.*;import java.lang.*;public class ClientTCP{ public static void main(String args[])

{try{ InetAddress id=InetAddress.getLocalHost(); Socket s = new Socket(id,80); DataInputStream dis=new DataInputStream(System.in); DataInputStream sdis=new DataInputStream(s.getInputStream()); PrintStream ps=new PrintStream(s.getOutputStream()); boolean flag=false; do {

System.out.println("\n1.Send\n2.Receive\n3.Exit\n");System.out.println("Your Choice(123):");String c=dis.readLine();int ch=Integer.parseInt(c);switch(ch){ case 1:System.out.println("\nEnter the Message to send");String msg=dis.readLine();ps.println(msg.toString());break;case 2:int a=sdis.available();String msgs=sdis.readLine();if(a!=0){System.out.println("\nThe Message Received is");System.out.println(msgs);}elseSystem.out.println("The Message is not received");break;case 3:s.close();flag=true;}

}while(!flag);

}catch(Exception e){}

}}

ServerTCP:import java.io.*;import java.net.*;import java.lang.*;public class ServerTCP{ public static void main(String args[])

{try{ ServerSocket ss=new ServerSocket(80);

Socket s = ss.accept();

DataInputStream dis=new DataInputStream(System.in); DataInputStream sdis=new DataInputStream(s.getInputStream()); PrintStream ps=new PrintStream(s.getOutputStream()); boolean flag=false; do {

System.out.println("\n1.Send\n2.Receive\n3.Exit\n"); System.out.println("Your Choice(123):");String c=dis.readLine();int ch=Integer.parseInt(c);switch(ch){ case 1:System.out.println("\nEnter the Message to send");String msg=dis.readLine();ps.println(msg.toString());break;case 2:int a=sdis.available();String msgs=sdis.readLine();if(a!=0){System.out.println("\nThe Message Received is");System.out.println(msgs);}elseSystem.out.println("The Message is not received");

break;case 3:s.close();flag=true;}

}while(!flag);}catch(Exception e){}

}}

OUTPUT:

SOURCE CODE:SERVER:package mynetapp;import java.io.*;import java.net.* ;import java.lang.*;

public class server {public static int serverport=998;public static int clientport=999;public static int buffersize=1024;public static byte buffer[]=new byte[buffersize];public static DatagramSocket ds;

public static void serve() throws Exception{

int pos=0;System.out.println(" I AM WAITING FOE YOUR INPUT");

while(true){

int c = System.in.read();switch(c){case 1:System.out.println("server");return;case '\r':break;case '\n':ds.send(new

DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientport));pos=0;break;default:buffer[pos++]=(byte)c;break;}

}}

public static void main(String arg[])throws Exception {

ds=new DatagramSocket();serve();

}}

CLIENT:package mynetapp;import java.net.*;class client {public static int serverport=998;public static int clientport=999;public static int buffersize=1024;public static byte buffer[]=new byte[buffersize];public static DatagramSocket ds;

public static void serve() throws Exception{

ds = new DatagramSocket(clientport);

while(true){

DatagramPacket p = new DatagramPacket(buffer, buffer.length);ds.receive(p);System.out.println(new String(p.getData(),0,p.getLength()));

} }}

OUTPUT:

D:\java\MEPRACTICE>java server

I AM WAITING FOR YOUR INPUT

CRICKET WORLD CUP

D:\java\MEPRACTICE>java client

CRICKET WORLD CUP

SOURCE CODE:FCLIENT:import java.net.*;import java.io.*;class Fclient{public static void main(String args[])throws Exception{Socket echosocket=null;BufferedReader in = null;

try{echosocket = new Socket(InetAddress.getLocalHost(),1092);in= new BufferedReader(new InputStreamReader(echosocket.getInputStream()));}catch(UnknownHostException e){System.err.println("could not get io for the connection");System.exit(1);}String UserInput;while((UserInput=in.readLine())!=null){System.out.println(UserInput);}in.close();echosocket.close();}}

FSERVER:import java.net.*;import java.io.*;class Fserver{public static void main(String args[])throws Exception{ServerSocket serversock=null;

try{

serversock = new ServerSocket(1092);

}catch(IOException e){System.err.println("could not listen on port 1092");System.exit(1);}Socket clientsock=null;try{clientsock=serversock.accept();System.out.println("connected to client sock");}catch(IOException e){System.err.println("accept failed");System.exit(1);}PrintWriter outprint=new PrintWriter(clientsock.getOutputStream(),true);String inputline,outputline;BufferedReader b=new BufferedReader(new InputStreamReader(System.in));System.out.println("enter the name of the file");String s=b.readLine();File f=new File(s);if(f.exists()){BufferedReader d=new BufferedReader(new FileReader(s));String line;while((line=d.readLine())!=null){outprint.write(line);outprint.flush();}d.close();}b.close();outprint.close();serversock.close();clientsock.close();}}

OUTPUT:

Source Code:EchoServerimport java.io.*;import java.net.*;

public class EchoServer{ public static void main(String args[]) {

String echoin;ServerSocket ssock;Socket csok;BufferedReader br;

try{

ssock = new ServerSocket(2000);csok = ssock.accept();

br = new BufferedReader(new InputStreamReader(csok.getInputStream()));

PrintStream ps = new PrintStream(csok.getOutputStream());System.out.println("Connected......for echo");while((echoin = br.readLine()) != null){

if(echoin.equals("end")){System.out.println("Client disconnected");br.close();break;}elseps.println(echoin);

}}catch(UnknownHostException e){ System.out.println(e.toString());}catch(IOException ioe){ System.out.println(ioe);}

}}

EchoClient

import java.io.*;import java.net.*;

public class EchoClient{ public static void main(String args[]) {

String sockin;try{

Socket csok = new Socket(InetAddress.getLocalHost(),2000);BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));BufferedReader br_sock = new BufferedReader(new

InputStreamReader(csok.getInputStream()));PrintStream ps = new PrintStream(csok.getOutputStream());System.out.println("Start Echoing...Type 'end' to terminate");while((sockin = br.readLine())!=null){ps.println(sockin);if(sockin.equals("end"))break;elseSystem.out.println("Reflected from Server:"+br_sock.readLine());}

}catch(UnknownHostException e){ System.out.println(e.toString());}catch(IOException ioe){ System.out.println(ioe);}

}}

OUTPUT:

Source Code:import java.io.*;import java.net.*;class pingdemo{ public static void main(String args[]) {

BufferedReader in;try{

Runtime r=Runtime.getRuntime();Process p=r.exec("Ping 127.0.0.1");

if(p==null)System.out.println("could not connect");in=new BufferedReader(new InputStreamReader(p.getInputStream()));String line;

while((line=in.readLine())!=null){

if(line.startsWith("reply"))System.out.println("this is reply");else if(line.startsWith("request"))System.out.println("there is no reply");else if(line.startsWith("destinator"))System.out.println("destinator host unreachabl");elseSystem.out.println(line);

}System.out.println(in.readLine());in.close();

} catch(IOException e){System.out.println(e.toString());}

}}

OUTPUT:

SOURCE CODETalkServer:import java.io.*;import java.net.*;

public class TalkServer{ public static void main(String args[]) {

ServerSocket ssock;Socket csok;BufferedReader br_local,br_sock;String sysin,sockin;

try{

ssock = new ServerSocket(2000);csok = ssock.accept();br_local = new BufferedReader(new InputStreamReader(System.in));br_sock = new BufferedReader(new

InputStreamReader(csok.getInputStream()));PrintStream ps = new PrintStream(csok.getOutputStream());

System.out.println("Connected......start conversation...type end to terminate");while((sockin = br_sock.readLine()) != null){

System.out.println(sockin);if(sockin.equalsIgnoreCase("end")){System.out.println("Client disconnected");break;}sysin = br_local.readLine();if(sysin.equalsIgnoreCase("end")){System.out.println("Server says stop conversation");

break;}else{ ps.println(sysin);}

}}catch(UnknownHostException uhe){ System.out.println(uhe.toString());}catch(IOException ioe)

{ System.out.println(ioe);} }}

TalkClient:

import java.io.*;import java.net.*;

public class TalkClient{ public static void main(String args[]) {

String sysin,sockin;try{

Socket csok = new Socket(InetAddress.getLocalHost(),2000);BufferedReader br_local = new BufferedReader(new

InputStreamReader(System.in));BufferedReader br_sock = new BufferedReader(new

InputStreamReader(csok.getInputStream()));PrintStream ps = new PrintStream(csok.getOutputStream());System.out.println("Start Talking..............");while((sysin = br_local.readLine())!="end"){ps.println(sysin);sockin = br_local.readLine();if(sockin.equalsIgnoreCase("end")){System.out.println("Server Disconnected ...");

break;}elseSystem.out.println(sockin);}

}catch(UnknownHostException e){ System.out.println("net"+e.toString());}catch(IOException ioe){ System.out.println("Connection terminated");}

}}

OUTPUT:

REMOTESERVER:import java.io.*;import java.net.*;import java.util.*;

class remoteserver{

public static void main(String args[])throws Exception{

String com=new String();ServerSocket ss=new ServerSocket(100);Socket s=ss.accept();BufferedReader nin=new BufferedReader(new

InputStreamReader(s.getInputStream()));PrintWriter nout=new PrintWriter(s.getOutputStream(),true);do{

com=nin.readLine();if(com.equalsIgnoreCase("date")){Calendar c=Calendar.getInstance();

nout.println(c.get(Calendar.DAY_OF_MONTH)+","+(c.get(Calendar.MONTH)+","+(c.get(Calendar.MONTH)+1)+","+c.get(Calendar.YEAR)));

System.out.println("Date is sent.connection terminated with client");}else{if(com.equalsIgnoreCase("time")){

Calendar c=new GregorianCalendar();c.setTime(new Date());

nout.println(c.get(Calendar.HOUR)+","+(c.get(Calendar.MINUTE)+1)+","+c.get(Calendar.SECOND));

System.out.println("Time is sent.connection terminated with client");

}else{

nout.println("your request is invalid");System.out.println("Proper response is sent to your client");}}

}

while(!com.equalsIgnoreCase("bye"));System.out.println("connection terminated with client");}

}

REMOTECLIENT2:import java.io.*;import java.net.*;

class remoteclient2{

public static void main(String args[])throws IOException{

String rec=new String();String res=new String();

try{

Socket s=new Socket(InetAddress.getLocalHost(),100);BufferedReader cin=new BufferedReader(new

InputStreamReader(System.in));BufferedReader sin=new BufferedReader(new

InputStreamReader(s.getInputStream()));PrintWriter nout=new PrintWriter(s.getOutputStream(),true);

do{

rec=cin.readLine();nout.println(rec);res=sin.readLine();System.out.println(res);

}while(rec.equalsIgnoreCase("bye"));

}

catch(Exception e){

System.out.println("error:"+e);}}

}

OUTPUT:

SOURCE CODE:ARP:import java.sql.*;import java.io.*;import java.net.*;class arp{

public static void main(String args[])throws IOException{

String log_user, phy_add=" ";int a=0;DataInputStream d=new DataInputStream(System.in);System.out.println("Enter Your Logical Address");log_user=d.readLine();

try{

Class.forName("sun.jdbc.odbcOdbcDriver");Connection cn=DriverManager.getConnection("jdbc:odbc:jafer");Statement s=cn.createStatement();ResultSet rs=s.executeQuery("select * from address");

while(rs.next()){

String log_add=rs.getString(1);if(log_user.equals(log_add.trim())){a=1;phy_add=rs.getString(2);}

}if(a==1){

System.out.println();System.out.println("The given logical address exists");System.out.println("The physical address is :"+phy_add);

}elseSystem.out.println("Your logical address does not exist");

}catch(Exception e){}}

}

OUTPUT:

SOURCE CODE:RARP:import java.sql.*;import java.io.*;import java.net.*;class rarp{

public static void main(String args[])throws IOException{

String log_add="", phy_user;int a=0;DataInputStream d=new DataInputStream(System.in);System.out.println("Enter Your Physical Address");phy_user=d.readLine();

try{

Class.forName("sun.jdbc.odbcOdbcDriver");Connection cn=DriverManager.getConnection("jdbc:odbc:jafer");Statement s=cn.createStatement();ResultSet rs=s.executeQuery("select * from address");

while(rs.next()){

String phy_add=rs.getString(2);if(phy_user.equals(phy_add.trim())){a=1;log_add=rs.getString(1);}

}if(a==1){

System.out.println();System.out.println("The given physical address exists");System.out.println("The logical address is :"+log_add);

}elseSystem.out.println("Your physical address does not exist");

}catch(Exception e){}}

}

OUTPUT:

SOURCE CODE:

RMI Client Program:import java.io.*;import java.rmi.Naming;import java.rmi.*;import java.lang.*;

public class RMIClient{

public static void main(String args[]){

try{ DataInputStream in = new DataInputStream(System.in); RMIIntf ref=(RMIIntf)Naming.lookup("impl"); boolean flag=false;

do{System.out.println("1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\

n4.DIVISION\n5.QUIT");System.out.println("Enter your Choice(1,2,3,4,5):");int ch=Integer.parseInt(in.readLine());

switch(ch){case 1:

System.out.println("Enter two nos to ADD..");int a1=Integer.parseInt(in.readLine());int b1=Integer.parseInt(in.readLine());System.out.println("ADDITION RESULTS:"+(ref.add(a1,b1)));break;

case 2:System.out.println("Enter two nos to Subtract..");int a2=Integer.parseInt(in.readLine());int b2=Integer.parseInt(in.readLine());System.out.println("SUBTRACTION RESULTS:"+(ref.sub(a2,b2)));break;

case 3:System.out.println("Enter two nos to Mul..");int a3=Integer.parseInt(in.readLine());int b3=Integer.parseInt(in.readLine());System.out.println("MULTIPLICATION RESULTS:"+(ref.mul(a3,b3)));break;

case 4:System.out.println("Enter two nos to Div..");int a4=Integer.parseInt(in.readLine());int b4=Integer.parseInt(in.readLine());System.out.println("DIVISION RESULTS:"+(ref.div(a4,b4)));break;

case 5: flag=true;break;

}}while(!flag);}catch(Exception e){}}

}

Implementation Program (RMIImpl):import java.rmi.*;import java.rmi.server.UnicastRemoteObject;

public class RMIImpl extends UnicastRemoteObject implements RMIIntf{

public RMIImpl()throws RemoteException{}public int add(int a, int b)throws RemoteException{

return(a+b);}public int sub(int a, int b)throws RemoteException{

return(a-b);}public int mul(int a, int b)throws RemoteException{

return(a*b);}public int div(int a, int b)throws RemoteException{

return(a/b);}

}

RMI Server side Interface Program (RMIIntf):

import java.rmi.Remote;import java.rmi.*;

public interface RMIIntf extends Remote{

int add(int a, int b)throws RemoteException;int sub(int a, int b)throws RemoteException;int mul(int a, int b)throws RemoteException;int div(int a, int b)throws RemoteException;

}

RMI Server Program (RMIServer):import java.net.*;import java.rmi.Naming;

public class RMIServer{

public static void main(String args[]){

try{RMIImpl impl=new RMIImpl();Naming.rebind("impl",impl);}catch(Exception e){System.out.println(e);}

}}

OUTPUT:

import java.io.*;class spr{public static void main(String args[]) throws Exception{int dist[][] = new int[50][50];int a[][] = new int[50][50];int path[][] = new int[50][50];int n, i, j, k, l; DataInputStream dis = new DataInputStream(System.in);String st;System.out.print("Enter the Number of vertices : ");st = dis.readLine();n = Integer.parseInt(st);System.out.println("Enter the Weight / Cost Between vertices, if no Enter 999");for(i=0; i<n; ++i)for(j=0; j<n; ++j) if( i !=j ) {

System.out.print(" From " + i + " To " + j + " : ");st = dis.readLine();dist[i][j] = Integer.parseInt(st);}elsedist[i][j] = 999;

for(i=0; i<n; ++i)for(j=0; j<n; ++j)path[i][j] = 0;for(i=0; i<n; ++i)for(j=0; j<n; ++j)a[i][j] = dist[i][j];for(k=0; k<n; ++k)for(i=0; i<n; ++i)for(j=0; j<n; ++j) if( (a[i][k] + a[k][j]) < a[i][j] ) {a[i][j] = a[i][k] + a[k][j];path[i][j] = k;

} for(i=0; i<n; ++i) for(j=0; j<n; ++j)if( i != j ) if(path[i][j] == 0) {System.out.print("The Shortest path From " + i + " To"+j + " is ");System.out.println( i + " ===> " + j + " Cost is " + a[i][j] ); } else {

System.out.print("The Shortest path From " + i + " To"+ j+ " is ");System.out.print( i + " ===> " + path[i][j] + " ===> " + j); System.out.println(" Cost is " + a[i][j] ); } }}

Sliding Window Protocol

import java.io.*;

class p11sldwnd{int len; public static void main(String args[]) throws Exception{p11sldwnd pvar = new p11sldwnd();int a[] = new int[50];int b[] = new int[50];int n, wsz, i, j, k, t, v, ch, sdch, rvch; DataInputStream dis = new DataInputStream(System.in);String st;j = t = v = wsz = 0;while(true){System.out.println("\n\t\t\tSLIDING WINDOW PROTOCOL");System.out.println("\t\t\t*************************\n");

System.out.println("\t\t1. Sender");System.out.println("\t\t2. Receiver");System.out.println("\t\t3. Exit");System.out.print("\t\t Enter Your Choice : ");st = dis.readLine();ch = Integer.parseInt(st);switch(ch){case 1 : bk1: while(true){

System.out.println("\n\t\tData Senders Menu");System.out.println("\t\t-----------------");System.out.println("\t\t1. Store");System.out.println("\t\t2. Window Size Set");System.out.println("\t\t3. Transit");System.out.println("\t\t4. Curret Window Items");System.out.println("\t\t5. Exit");System.out.print("\t\t Enter Your Choice : ");st = dis.readLine();sdch = Integer.parseInt(st);switch(sdch){case 1:System.out.print("\n\t\t How many Data want to Store - n : ");st = dis.readLine();n = Integer.parseInt(st);for(i=0; i<n; ++i){System.out.print("\n\t\t Enter the data : ");

st = dis.readLine();a[i] = Integer.parseInt(st);}break;case 2:System.out.print("\n\t\t Enter the Window Size : ");st = dis.readLine();wsz = Integer.parseInt(st);break;case 3:System.out.print("\n\t\t How many data want to transmit : ");st = dis.readLine();pvar.len = Integer.parseInt(st);for(i=0; i < pvar.len; ++i)System.out.println(b[i]);break;case 4:System.out.println("\n\t\t The Data nside Transmitter Window");t = 0;wsz = wsz + j;v = v + j;for(i=v; i < wsz; ++i)b[t++] = a[i];for(i=0; i < t; ++i)System.out.println(b[i]);break;case 5:break bk1;}}break;case 2: bk2: while(true){System.out.println("\n\t\tData Receivers Menu");System.out.println("\t\t-------------------");System.out.println("\t\t1. Receive");System.out.println("\t\t2. Acknowledge");System.out.println("\t\t3. Exit");System.out.print("\t\t Enter Your Choice : ");st = dis.readLine();rvch = Integer.parseInt(st);switch(rvch){case 1:

System.out.println("\n\t\t The number of data Reveived ");for(i=0; i < pvar.len; ++i)System.out.println(b[i]);

break;case 2:System.out.print("\n\t\t Enter the number of data Acknowledged: ");

st = dis.readLine();j = Integer.parseInt(st);break;case 3:break bk2;}

} break;

case 3:System.exit(1);

}}

}}