Вы находитесь на странице: 1из 18

1. PROGRAMS USING TCP SOCKETS PROGRAM: ECHO SERVER import java.net.*; import java.io.

*; public class echoserver { public static void main(String args[])throws IOException { ServerSocket serverSocket=null; try { serverSocket=new ServerSocket(10007); } catch(IOException e) { System.err.println("could not listen on port:10007"); System.exit(1); } Socket clientSocket=null; System.out.println("Waiting for connection..."); try { clientSocket=serverSocket.accept(); } catch(IOException e) { System.err.println("Accept failed"); System.exit(1); } System.out.println("connection successful"); System.out.println("waiting for input..."); PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true); BufferedReader in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; while((inputLine=in.readLine())!=null) { System.out.println("Server=" + inputLine); out.println(inputLine); if(inputLine.equals("Bye")) break;

} out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } ECHO CLIENT import java.io.*; import java.net.*; public class echoclient { public static void main(String args[])throws IOException { String serverHostname=new String("127.0.0.1"); if(args.length>0) serverHostname=args[0]; System.out.println("Attempting to connect to host" + serverHostname+"onport 10007"); Socket echoSocket=null; PrintWriter out=null; BufferedReader in=null; try { //echoSocket=new Socket("taranis",7); echoSocket=new Socket(serverHostname,10007); out=new PrintWriter(echoSocket.getOutputStream(),true); in=new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch(UnknownHostException e) { System.err.println("Don't know about host:"+serverHostname); System.exit(1); } BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in)); String userInput; System.out.print("input:"); while((userInput=stdIn.readLine())!=null) { out.println(userInput); System.out.println("echo:"+ in.readLine()); System.out.println("input"); } out.close();

in.close(); stdIn.close(); echoSocket.close(); }} DATE AND TIME SERVER import java.io.*; import java.net.*; import java.util.*; import java.lang.*; class Tserver { public static void main(String as[ ])throws Exception { Date d=new Date(); String s1=d.toString(); ServerSocket ss =new ServerSocket(1080); Socket s=ss.accept(); PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true); out.println(s1); s.close(); } }

DATE AND TIME CLIENT import java.net.*; import java.io.*; class Tclient { public static void main(String df[])throws Exception { InetAddress a=InetAddress.getLocalHost(); Socket s=new Socket(a,1080); BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); System.out.println(in.readLine()); s.close(); } }

OUTPUT :

DATE AND TIME SERVER & CLIENT

ECHO SERVER AND CLIENT

2. PROGRAM USING UDP SOCKETS PROGRAM : import javax.naming.directory.Attributes; import javax.naming.directory.InitialDirContext; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import java.net.InetAddress; import java.net.UnknownHostException; public class DNSLookup { public static void main(String args[]) { if(args.length!=1) { System.err.println("print out DNS Record for an Internet Addresss"); System.err.println("USAGE:java DNSLookup domain Name/domain Address"); System.exit(-1); } try { InetAddress inetAddress; if(Character.isDigit(args[0].charAt(0))) { byte[]b=new byte[4]; String[]bytes=args[0].split("[.]"); for(int i=0;i<bytes.length;i++) { b[i]=new Integer(bytes[i]).byteValue(); }

inetAddress=InetAddress.getByAddress(b); } else { inetAddress=InetAddress.getByName(args[0]); } System.out.println(inetAddress.getHostName()+"/"+inetAddress.getHostAddress()); InitialDirContext iDirC=new InitialDirContext(); Attributes attributes=iDirC.getAttributes("dns:/"+inetAddress.getHostName()); NamingEnumeration attributeEnumeration=attributes.getAll(); System.out.println("--DNS Information--"); while(attributeEnumeration.hasMore()) { System.out.println(""+attributeEnumeration.next()); } attributeEnumeration.close(); } catch(UnknownHostException exception) { System.err.println("ERROR:NO INTERNET Address for "+args[0]+"`"); } catch(NamingException exception) { System.err.println("ERROR:NO DNS record for `"+args[0]+"`"); } } }

OUTPUT :

3. PROGRAM USING RPC PROGRAM : ADDSERVER import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl=new AddServerImpl(); Naming.rebind("AddServer",addServerImpl); } catch(Exception e) { System.out.println("Exception :"+e); } } }

ADDCLIENT import java.rmi.*; public class AddClient { public static void main(String args[]) { try { String addServerURL="rmi://"+args[0]+"/AddServer"; AddServerIntf addServerIntf=(AddServerIntf)Naming.lookup(addServerURL); System.out.println("the first number is"+args[1]); double d1=Double.valueOf(args[1]).doubleValue( ); System.out.println("the Second numberis"+args[2]);

double d2=Double.valueOf(args[2]).doubleValue(); System.out.println("the sum is"+addServerIntf.add(d1,d2)); } catch(Exception e) { System.out.println("Exception:"+e); } } }

ADDSERVERIMPL import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl()throws RemoteException {} public double add(double d1,double d2)throws RemoteException { return d1+d2; } }

ADDSERVERINTF import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1,double d2)throws RemoteException; }

OUTPUT :

4. SLIDING WINDOW PROTOCOL


PROGRAM : 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); } } } }

OUTPUT :

5. ROUTING PROTOCOLS

PROGRAM : #include <stdio.h> #include<conio.h> int main() { int n; int i,j,k; int a[10][10],b[10][10]; printf("\n Enter the number of nodes:"); scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("\n Enter the distance between the host %d - %d:",i+1,j+1); scanf("%d",&a[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } for(k=0;k<n;k++) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]>a[i][k]+a[k][j]) { a[i][j]=a[i][k]+a[k][j]; } } }

} for(i=0;i<n;i++) { for(j=0;j<n;j++) { b[i][j]=a[i][j]; if(i==j) { b[i][j]=0; } }} printf("\n\n THE OUTPUT MATRIX :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d\t",b[i][j]); } printf("\n"); } getch(); }

OUTPUT : Enter the number of nodes:4 Enter the distance between the host 1 - 1: 5 Enter the distance between the host 1 - 2: 9 Enter the distance between the host 1 - 3: 6 Enter the distance between the host 1 - 4: 4 Enter the distance between the host 2 - 1: 2 Enter the distance between the host 2 - 2: 1 Enter the distance between the host 2 - 3: 8 Enter the distance between the host 2 - 4: 3 Enter the distance between the host 3 - 1: 6 Enter the distance between the host 3 - 2: 1 Enter the distance between the host 3 - 3: 4 Enter the distance between the host 3 - 4: 2 Enter the distance between the host 4 - 1: 5 Enter the distance between the host 4 - 2: 1 Enter the distance between the host 4 - 3: 8 Enter the distance between the host 4 - 4: 2 5964 2183 6142 5182 THE OUTPUT MATRIX : 0564 2083 3102 3180

Вам также может понравиться