You are on page 1of 20

IMPLEMENTATION OF TCP AIM: To write a network program for implementing chat using Transmission Control Protocol.

ALGORITHM: SERVER: Create a socket with port no(6789) Accept the connection from any client request to associate with it. Send a message to client to verify connection. Close the connection.

CLIENT: Get IP Address of server Connect the client with server if connection is successful. Print message from server Close the connection.

PROGRAM: SERVER: import java.io.*; import java.net.*; class serverchat { public static void main(String args[]) throws Exception { String sentence, newsentence; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); ServerSocket ss= new ServerSocket(6789);

System.out.println("Server Started Successfully"); do { Socket s=ss.accept(); BufferedReader inp=new BufferedReader(new InputStreamReader(s.getInputStream())); DataOutputStream out=new DataOutputStream(s.getOutputStream()); sentence=inp.readLine(); System.out.println("user1:"+sentence); newsentence=in.readLine(); out.writeBytes(newsentence+'\n'); } while(newsentence.compareTo("exit")!=0); } } CLIENT: import java.io.*; import java.net.*; class clienrchat { public static void main(String args[]) throws Exception { String sentence, newsentence; do { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); sentence=in.readLine(); Socket cs= new Socket("127.0.0.1",6789); BufferedReader inp=new BufferedReader(new InputStreamReader(cs.getInputStream())); DataOutputStream out=new DataOutputStream(cs.getOutputStream()); out.writeBytes(sentence+'\n'); newsentence=inp.readLine();

System.out.println("user2:"+newsentence); cs.close(); } while(sentence.compareTo("exit")!=0); } } OUTPUT:

IMPLEMENTATION OF UDP AIM: To implement User Datagram Protocol program using Java. ALGORITHM: Start the program The class names are given as UDP server, UDP client. Under the class, port number is assigned. The sending and receiving datas are declared. Print the server started successfully. The datas to be accessed are stored in the buffer. The port numbers given should be similar in both client and server. Compile both client and server program simultaneously. The datas given in client will be accessed in server. Stop the program.

PROGRAM: SERVER:

import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket ServerSocket = new DatagramSocket(9867); byte[] receiveData=new byte[1024]; byte[] sendData=new byte[1024]; while(true) { System.out.println("Server started successsfully"); DatagramPacket receivePacket=new DatagramPacket(receiveData, receiveData.length); ServerSocket.receive(receivePacket); String sentence=new String(receivePacket.getData()); System.out.println("Received:"+sentence); InetAddress IpAddress = receivePacket.getAddress(); int Port = receivePacket.getPort(); String capitalizedsentence=sentence.toUpperCase(); sendData=capitalizedsentence.getBytes(); DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IpAddress,Port); ServerSocket.send(sendPacket); } } } CLIENT: import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception {

BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket=new DatagramSocket(); InetAddress IpAddress = InetAddress.getByName("localhost"); byte[] sendData=new byte[1024]; byte[] receiveData=new byte[1024]; String sentence=inFromUser.readLine(); sendData=sentence.getBytes(); DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IpAddress,9867); clientSocket.send(sendPacket); DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length); clientSocket.receive(receivePacket); String modifiedsentence= new String(receivePacket.getData()); System.out.println("From Server:"+modifiedsentence); clientSocket.close(); } } OUTPUT:

CONCURRENT DATE AND TIME AIM: To implement java program for concurrent date time using TCP ALGORITHM: SERVER: Create a socket with port no(2000) Accept the connection from any client request to associate with it. Send the server time to client Close the connection. Repeat step 2 for the entire client request Exit the process. Get the IP address of server to which client wants to associate. Connect the client with server by same port number as the server. If server is active get and display the time from server. Close the connection.

CLIENT:

PROGRAM:

SERVER: import java.io.*; import java.net.*; import java.util.*; public class timeserver { public static void main(String[] args) { int port = 2000; try { ServerSocket socketid = new ServerSocket(port); System.out.println("Waiting for a client"); Socket socket = socketid.accept(); OutputStream StrOut = socket.getOutputStream(); DataOutputStream DataOut = new DataOutputStream(StrOut); String time = null; time = new java.util.Date().toString(); DataOut.writeUTF(time); socket.close(); } catch(Exception x) { System.out.println("Exception occured"); } } } CLIENT: import java.net.*; import java.io.*; import java.util.*; public class timeclient { public static void main(String[]args)

{ int ServerPort = 2000; String Address=args[0]; try { InetAddress IpAddress=InetAddress.getByName(Address); Socket socket=new Socket(IpAddress,ServerPort); InputStream Strln=socket.getInputStream(); DataInputStream DataIn=new DataInputStream(Strln); String time = null; time = DataIn.readUTF(); System.out.println("Time at the server side is:"+time); socket.close(); } catch(Exception x) { System.out.println("Exception occured"); } } } OUTPUT:

IMPLEMENATION OF DISTANCE VECTOR ALGORITHM AIM: To write C program for distance vector algorithm. ALGORITHM: 1. Initiate the program. Include various header files and declare the variable. Declare the various nodes for the topology. Enter the number of nodes and node for which distance vector table is needed. Enter the number of neighbours in alphabetical order and also the distance vector of source. Print the distance vector after receiving the vector. The nodes are incremented. Terminate the program. PROGRAM: #include<stdio.h> # define e 10000 # define nodes1 40

int topology [nodes1][nodes1]; static int l,n,neighb,nodes; static char neigh[nodes1]; struct { char name; int delay; } dv [nodes1]; void main() { int i,j,k[nodes1 *nodes1],add=0,src1; char src='a'; //clrscr(); printf("\t\t\t DISTANCE VECTOR ROUTING\n"); printf( "\t\t\t *****************\n"); printf("\n\tEntr the number of nodes.........."); scanf("%d",&nodes); fflush(stdin); printf("\n\t Enter the nodes for which Distance vector table is needed..."); scanf("%c", &src); fflush(stdin); printf("\n\t\t Enter the numbr of neighbours to %c .....", src); scanf("%d", &neighb); fflush(stdin); printf("\n\t\t Enter the number of neighbours (In alphabets order)...."); neigh[0]=src; for(i=1;i<neighb+1;i++) { scanf("%c",&neigh[i]); fflush(stdin); }

printf("\n\t Enter the distance vectors of the source and neighbouring nodes \n\n starting with source, then with neighbouring nodes in alphabetical order:"); for(i=0;i<nodes *(neighb+1);i++) { scanf("%d", &k[i]); } src1=src; l=nodes; n=1; for(i=0;i<nodes;i++) { if(i==src1-97) { for(j=0;j<nodes;j++) { if(k[j]<0) topology[i][j]=e; else topology[i][j]=k[j]; } } else if(i==neigh[n]-97) { for(j=0;j<nodes;j++) { if(k[l]<0) topology[i][j]=e; else topology[i][j]=k[l]; l++; } n++;

} else { for(j=0;j<nodes;j++) topology[i][j]=e; } } i=src1-97; for(j=0;j<nodes;j++) { dv[j].name=src1; dv[j].delay=topology[i][j]; } k[nodes *nodes]=e; n=1; for(i=0;i<nodes;i++) { if(i==neigh[n]-97) { add=topology[src1-97][i]; for(j=0;j<nodes;j++) { k[j]=add+topology[i][j]; } for(j=0;j<nodes;j++) { if(k[j]<dv[j].delay) { dv[j].name=i+97; dv[j].delay=k[j]; } } if(i!=src1-97) n++;

printf("\n\t\t\t%c's distance vector after receiving %c's vector.....\n",src1,i+97); for(j=0;j<nodes;j++) printf("\n to %c \tfrom:%c \t %d\n", j+97,dv[j].name,dv[j].delay); getch(); } } }

OUTPUT:

IMPLEMENTATION OF LINK STATE VECTOR ALGORITHM AIM: To write C++ program to implement link state vector algorithm. ALGORITHM: 1. 2. 3. 4. 5. 6. 1. Initiate the program. Include various header files and declare the variable. Declare two dimensional arrays. Open the main function. Enter the number of vertices and also weight between the vertices. Shortest distance between every node is calculated by means of declared counter variables. 7. Terminate the program.
PROGRAM:
#include<iostream.h> #include<conio.h> # define Not A vertex(-1) typedef int TwoDimArray [10][10]; void AllPairs (TwoDimArray A, TwoDimArray D, int N) { int i,j,k; for(i=0;i<N;i++) for(j=0;j<N;j++) D[i][j]=A[i][j]; for(k=0;k<N;k++) for(i=0;i<N;i++) for(j=0;j<N;j++)

if(D[i][k]+D[k][j]<D[i][j]); { D[i][j]=D[i][k]+D[k][j]; } } main() { TwoDimArray A; TwoDimArray D; int i,j,n; clrscr(); cout<<"Enter the number of vertices:"; cin>>n; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<"Enter the edge weight from"<<i+1<<"to"<<j+1<<":"; cin>>A[i][j]; } } AllPairs(A,D,n); cout<<"\n Shortest distance between every node is \n\n"; for(i=0;i<n;i++) cout<<" "<<i+1; for(i=0;i<n;i++) { cout<<"\n\n"<<i+1; for(j=0;j<n;j++) cout<<" "<<D[i][j]; } cout<<"\nn Press any key to eit:"; getch(); return 0; } OUTPUT:

IMPLEMENTATION OF SLIDING WINDOW PROTOCOL AIM: To write C program for sliding window protocol. ALGORITHM: 1. 2. 3. 4. 5. 6. 7. 8. Initiate the program. Include various header files and declare the variable. Enter the buffer size and also window size. Assign the counter value should be less than that of buffer size. Enter starting position of the window. For the data in sliding window, counter value is less than size. Determine the data received. After the acknowledgement is received, terminate the program.

PROGRAM: #include<stdio.h> void main() { int m,f,s,i,bs,b[20],in=0,r; printf("\n Enter the buffer size:"); scanf("%d",&bs); printf("\n Enter the window size:"); scanf("%d",&s); for(i=0;i<bs+s;i++) b[i]=0; for(i=0;i<bs;i++) { printf("\n Sender[%d]:",in); scanf("%d",&b[i]); in+=1; } printf("Enter starting position of the window:"); scanf("%d",&f); s+=f; while(bs>f) { printf("\n The data in sliding window is:"); for(i=f;i<s;i++) printf("%d",b[i]); s++; r=b[f]; printf("\n The data received in n receiver[%d]: %d",f,r); f++; printf("\n ACK %d",f); } getch(); } OUTPUT:

IMPLEMENTATION OF SHORTEST PATH ALGORITHM AIM: To write a program to perform comparison of routines. ALGORITHM: Start the program. Initiate the program

Include various reader files and declare variables Enter the number of nodes, the counter values are incremented. Enter the cost for node and initially value of index is assumed as zero. The vertices with weights for the graph is assigned. We can also find shortest path using Batment Forte Algorithm. Calculate the sequence of vertices and shortest distance. Terminate the program.

PROGRAM:

You might also like