Upload
jerald-jasper
View
217
Download
0
Embed Size (px)
Citation preview
7/29/2019 Network Pgm(1 5)
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;
7/29/2019 Network Pgm(1 5)
2/18
}
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();
7/29/2019 Network Pgm(1 5)
3/18
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(newOutputStreamWriter(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();
}}
7/29/2019 Network Pgm(1 5)
4/18
OUTPUT :
DATE AND TIME SERVER & CLIENT
7/29/2019 Network Pgm(1 5)
5/18
ECHO SERVER AND CLIENT
7/29/2019 Network Pgm(1 5)
6/18
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
7/29/2019 Network Pgm(1 5)
7/18
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]+"`");
}
}
}
7/29/2019 Network Pgm(1 5)
8/18
OUTPUT :
7/29/2019 Network Pgm(1 5)
9/18
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]);
7/29/2019 Network Pgm(1 5)
10/18
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;
}
7/29/2019 Network Pgm(1 5)
11/18
OUTPUT :
7/29/2019 Network Pgm(1 5)
12/18
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");
7/29/2019 Network Pgm(1 5)
13/18
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
7/29/2019 Network Pgm(1 5)
14/18
}
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);
}
}
}
}
7/29/2019 Network Pgm(1 5)
15/18
OUTPUT :
5. ROUTING PROTOCOLS
7/29/2019 Network Pgm(1 5)
16/18
PROGRAM :
#include
#includeint 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
7/29/2019 Network Pgm(1 5)
17/18
}
for(i=0;i
7/29/2019 Network Pgm(1 5)
18/18
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
5 9 6 4
2 1 8 3
6 1 4 2
5 1 8 2
THE OUTPUT MATRIX :
0 5 6 4
2 0 8 3
3 1 0 2
3 1 8 0