You are on page 1of 17

COMPUTER NETWORKS LAB MANNUAL

1.Implimentation of DISTANCE VECTOR ROUTING Algorithm(JAVA)


import java.io.*;

public class DVR

static int graph[][];

static int via[][];

static int rt[][];

static int v;

static int e;

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

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

System.out.println("Please Enter The Numbers of Vertices:");

v=Integer.parseInt(br.readLine());

System.out.println("Please Enter The No.of Edges");

e=Integer.parseInt(br.readLine());

graph=new int[v][v];

via=new int[v][v];

rt=new int[v][v];

for(int i=0;i<v;i++)

for(int j=0;j<v;j++)

if(i==j)

graph[i][j]=0;

else

graph[i][j]=9999;

for(int i=0;i<e;i++)

System.out.println("Please Enter Data for Edge"+(i+1)+":");


System.out.println("Source:");

int s=Integer.parseInt(br.readLine());

s--;

System.out.println("Destination:");

int d=Integer.parseInt(br.readLine());

d--;

System.out.println("Cost:");

int c=Integer.parseInt(br.readLine());

graph[s][d]=c;

graph[d][s]=c;

dvr_calc_disp("The Initial Routing Tables are:");

System.out.print("Please Enter the source Node for the Edge Whose cost has Changed:");

int s=Integer.parseInt(br.readLine());

s--;

System.out.print("Please Enter the Destination Node for the Edge Whose cost has Changed:");

int d=Integer.parseInt(br.readLine());

d--;

System.out.println("Please Enter the New Cost:");

int c=Integer.parseInt(br.readLine());

graph[s][d]=c;

graph[d][s]=c;

dvr_calc_disp("The New Routing Tables are:");

static void dvr_calc_disp(String message)

System.out.println();

init_tables();
update_tables();

System.out.println(message);

print_tables();

System.out.println();

static void update_table(int source)

for(int i=0;i<v;i++)

if(graph[source][i]!=9999)

int dist=graph[source][i];

for(int j=0;j<v;j++)

int inter_dist=rt[i][j];

if(via[i][j]==source)

inter_dist=9999;

if(dist+inter_dist<rt[source][j])

rt[source][j]=dist+inter_dist;

via[source][j]=i;

static void update_tables()

int k=0;
for(int i=0;i<4*v;i++)

update_table(k);

k++;

if(k==v)

k=0;

static void init_tables()

for(int i=0;i<v;i++)

for(int j=0;j<v;j++)

if(i==j)

rt[i][j]=0;

via[i][j]=i;

else

rt[i][j]=9999;

via[i][j]=100;

static void print_tables()

{
for(int i=0;i<v;i++)

for(int j=0;j<v;j++)

System.out.print("Dist:"+rt[i][j]+" ");

System.out.println();

OUTPUT :
2.Implementation of FILE TRANSFER PROTOCOL (FTP) (JAVA)
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

public class FTPDownloadFileDemo

public static void main(String [] args)

String server="local host";

int port=21;

String user="user";

String pass="pass";

FTPClient ftpClient=new FTPClient();

try

ftpcCient.connect(server,port);

ftpClient.login(user,pass);

ftpClient.enterLocalPassiveMode();

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

String remoteFile1="/Test/files.txt";

File downloadFile1=new File("c:/javap/files1.txt");

OutputStream outputstream1=new BufferedOutputStream(new FileOutputStream(downloadFile1));


boolean success=ftpClient.retrieveFile(remoteFile1,outputStream1);

outputStream1.close();

if(success)

System.out.println("File #1 has been Downloaded Successfully");

String remoteFile2="/test/files.txt";

File downloadfile2=new File("c:/javaps/File2.txt");

OutputStream outputStream2=new BufferedOutputStream(new FileOutputStream(downloadFile2));

InputStream inputStream=ftpClient.retrieveFileSystem(remoteFile2);

byte[] bytesArray=new byte[4096];

int bytesRead=-1;

while((bytesRead=inputStream.read(bytesArray))=-1)

outputStream2.write(bytesArray,0,bytesRead);

success=ftpClient.completePendingcommand();

if(success)

System.out.println("File #2 has been Downloaded Successfully");

outputStream2.close();

inputStream.close();

catch(IOException ex)

System.out.println( "Error: "+ex.getMessage());

ex.printStackTrace();

finally

{
try

if(ftpClient.isConnected())

ftpClient.logout();

ftpClient.disconnect();

catch(IOException ex)

ex.printStackTrace();

OUTPUT :

Open FILEZILLA Application


1.Implementation of TCP (UNIX)

Client source-code:

#include<arpa/inet.h>

#include<netinet/in.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/socket.h>

#include<unistd.h>

#define PORT 8090

int main()

struct sockaddr_in address;

int sock=0,valread;

struct sockaddr_in serv_addr;

char str[100];

printf("\n input the string");

scanf("%[^\n]s",str);

char buffer[1024]={0};
if((sock=socket(AF_INET,SOCK_STREAM,0))<0)

printf("\nsocket creation error\n");

return -1;

memset(&serv_addr,'0',sizeof(serv_addr));

serv_addr.sin_family=AF_INET;

serv_addr.sin_port=htons(PORT);

if(inet_pton(AF_INET,"127.0.0.1",&serv_addr.sin_addr)<=0)

printf("\naddress not supported\n");

return -1;

if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)

printf("\nconnection failed\n");

return -1;

int l=strlen(str);

send(sock,str,sizeof(str),0);

valread=read(sock,str,l);

printf("%s\n",str);

return 0;

Server source-code:

#include<netinet/in.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/socket.h>
#include<unistd.h>

#define PORT 8090

int main()

int server_fd,new_socket,valread;

struct sockaddr_in address;

char str[100];

int addrlen=sizeof(address);

char buffer[1024]={0};

char*hello="hello from server";

if((server_fd=socket(AF_INET,SOCK_STREAM,0))==0)

perror("socket failed");

exit(EXIT_FAILURE);

address.sin_family=AF_INET;

address.sin_addr.s_addr=INADDR_ANY;

address.sin_port=htons(PORT);

if(bind(server_fd,(struct sockaddr*)&address,sizeof(address))<0)

perror("bind failed");

exit(EXIT_FAILURE);

if(listen(server_fd,3)<0)

perror("listen");

exit(EXIT_FAILURE);

if((new_socket=accept(server_fd,(struct sockaddr*)&address,(socklen_t*)&addrlen))<0)

perror("accept");
exit(EXIT_FAILURE);

valread=read(new_socket,str,sizeof(str));

int i,j,temp;

int l=strlen(str);

printf("\nstring send by client:%s\n",str);

for(i=0,j=l-1;i<j;i++,j--)

temp=str[i];

str[i]=str[j];

str[j]=temp;

send(new_socket,str,sizeof(str),0);

printf("\nmodified string sent to client\n");

return 0;

OUTPUT :
2.Implementation of UDP (UNIX)
Client source-code:

#include<stdio.h>

#include<sys/types.h>

#include<stdlib.h>

#include<string.h>

#include<sys/socket.h>
#include<netinet/in.h>

#include<errno.h>

#include<netdb.h>

int main(int argc,char *argv[])

int sid,n;

char buffer[1024];

struct sockaddr_in saddr;

struct hostent *hen;

if(argc<3)

fprintf(stderr,"Error: host name and port is not given \n");

exit(1);

sid=socket(AF_INET,SOCK_DGRAM,0);

if(sid<0)

perror("socket create");

hen=gethostbyname(argv[1]);

if(hen==NULL)

fprintf(stdout,"host not found");

exit(1);

saddr.sin_family=AF_INET;

saddr.sin_port=htons(atoi(argv[2]));

bcopy((char *)hen->h_addr,(char *)&saddr.sin_addr.s_addr,hen->h_length);

if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0)

perror("socket bind");

printf("enter the data you want to send: \n");

fgets(buffer,1023,stdin);

n=sendto(sid,buffer,1023,0,(struct sockaddr*)&saddr,sizeof(saddr));
if(n<0)

perror("error in send to");

close(sid);

return 0;

Server source-code :

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<errno.h>

#include<unistd.h>

int main(int argc,char *argv[])

socklen_t sid,clen;

char buffer[1024];

struct sockaddr_in saddr,caddr;

int n;

if(argc<2)

fprintf(stderr,"ERROR : port is not given\n");

exit(1);

sid=socket(AF_INET,SOCK_DGRAM,0);

if(sid<0)

perror("socket_create");

bzero((char*)&saddr,sizeof(saddr));

saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[1]));

saddr.sin_addr.s_addr=INADDR_ANY;

if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0)

perror("socket_bind");

clen=sizeof(caddr);

bzero(buffer,1024);

n= recvfrom(sid,buffer,1023,0,(struct sockaddr*)&caddr,&clen);

if(n<0)

perror("receive");

printf("the msg that is sent by the client is %s",buffer);

close(sid);

return 0;

OUTPUT :

You might also like