Full Lab Program CN2010

24
Program 1: Program to implement Date and Time using TCP Sockets #include<netinet/in.h> #include<sys/socket.h> main( ) { struct sockaddr_in sa; struct sockaddr_in cli; int sockfd,coontfd; int len,ch; char str[100]; time_t tick; sockfd=socket(AF_INET,SOCK_STREAM ,0); if(socket<0) { printf(“error in socket\n”); exit(0); } else printf(“Socket Opened”); bzero(&sa,sizeof(sa)); sa.sin_port=htons(5600); sa.sin_addr.s_addr=htonl(0); if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) { printf(“Error in binding\n”); } else printf(“Binded Successfully”); listen(sockfd,50) for(;;) { len=sizeof(ch); conntfd=accept(sockfd,(struct sockaddr*)&cli,&len); printf(“Accepted”); tick=ctime(NULL); snprintf(str,sizeof(str),”%s”,cti me(&tick)); write(conntfd,str,100); } } CLIENT PROGRAM Source code in C #include<netinet/in.h> #include<sys/socket.h> main() { struct sockaddr_in sa,cli; int n,sockfd; int len; char buff[100]; sockfd=socket(AF_INET,SOCK_STREAM ,0); if(sockfd<0) { printf(“Error in Socket”); exit(0); } else printf(“Socket is Opened”); bzero(&sa,sizeof(sa)); sa.sin_family=AF_INET; sa.sin_port=htons(5600); if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) { printf(“Error in connection failed”); exit(0); } else printf(“connected successfully”): if(n=read(sockfd,buff,sizeof(buff ))<0) { printf(“Error in Reading”); exit(0); } else { printf(“Message Read %s”,buff); buff[n]=’\0’; printf(“%s”,buff);

Transcript of Full Lab Program CN2010

Page 1: Full Lab Program CN2010

Program 1: Program to implement Date and Time using TCP Sockets

#include<netinet/in.h>#include<sys/socket.h>main( ){struct sockaddr_in sa;struct sockaddr_in cli;int sockfd,coontfd;int len,ch;char str[100];time_t tick;sockfd=socket(AF_INET,SOCK_STREAM,0);if(socket<0){printf(“error in socket\n”);exit(0);}elseprintf(“Socket Opened”);bzero(&sa,sizeof(sa));sa.sin_port=htons(5600);sa.sin_addr.s_addr=htonl(0);if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0){printf(“Error in binding\n”);}elseprintf(“Binded Successfully”);listen(sockfd,50)for(;;){len=sizeof(ch);conntfd=accept(sockfd,(struct sockaddr*)&cli,&len);printf(“Accepted”);tick=ctime(NULL);snprintf(str,sizeof(str),”%s”,ctime(&tick));write(conntfd,str,100);}}

CLIENT PROGRAM Source code in C#include<netinet/in.h>#include<sys/socket.h>main(){struct sockaddr_in sa,cli;int n,sockfd;

int len;char buff[100];sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd<0){printf(“Error in Socket”);exit(0);}elseprintf(“Socket is Opened”);bzero(&sa,sizeof(sa));sa.sin_family=AF_INET;sa.sin_port=htons(5600);if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0){printf(“Error in connection failed”);exit(0);}elseprintf(“connected successfully”):if(n=read(sockfd,buff,sizeof(buff))<0){printf(“Error in Reading”);exit(0);}else{printf(“Message Read %s”,buff);buff[n]=’\0’;printf(“%s”,buff);}}

Output SERVER SIDE OUTPUT

Socket OpenedBinded successfullyAccepted

CLIENT SIDE OUTPUT WITH SERVER TIMESocket is OpenedConnected successfullyMessage Read Mon Jan 22 15:09:19 2007

Page 2: Full Lab Program CN2010

Ex.No:2 Program to implement Echo Server and Client using TCP SocketsClient Side#include<stdio.h>#include<netinet/in.h>#include<sys/socket.h>#include<netdb.h>#include<string.h>#define MAX 80#define PORT 43464#define SA struct sockaddr

void func(int sockfd){char buff[MAX];int n;

for(;;){bzero(buff,sizeof(buff));printf("Enter the string");n=0;while((buff[n++]=getchar())!='\n');write(sockfd,buff,sizeof(buff));bzero(buff,sizeof(buff));read(sockfd,buff,sizeof(buff));printf("From server:%s",buff);

if((strncmp("exit",buff,4))==0){printf("Client Exit..\n");break;}

}}

int main(){int sockfd,connfd;struct sockaddr_in servaddr,cli;sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1){printf("sockect creation failed");exit(0);}elseprintf("Sockect sucessfully created.\n");

bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");servaddr.sin_port=htons(PORT);

if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0){printf("connection with the server failed..\n");exit(0);}elseprintf("connected to the server..\n");

func(sockfd);close(sockfd);}Server Side#include<stdio.h>#include<netinet/in.h>#include<sys/types.h>#include<sys/socket.h>#include<netdb.h>#include<string.h>#define MAX 80#define PORT 43464#define SA struct sockaddr

void func(int sockfd){char buff[MAX];int n;

for(;;){bzero(buff,MAX);read(sockfd,buff,sizeof(buff));printf("From client:%s\t",buff);write(sockfd,buff,sizeof(buff));bzero(buff,sizeof(buff));

if((strncmp(buff,"exit",4))==0){printf("server exit...\n");}

}}int main(){int len,sockfd,connfd;struct sockaddr_in servaddr,cli;sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1){printf("Sockect creation failed..\n");exit(0);}

Page 3: Full Lab Program CN2010

elseprintf("Sockect successfully created.\n");

bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(PORT);

if((bind(sockfd,(SA *)&servaddr,sizeof(servaddr)))!=0){printf("socket bind failed.\n");exit(0);}elseprintf("Socket sucessfully binded.\n");if((listen(sockfd,5))!=0){printf("Listen failed.\n");exit(0);}elseprintf("Server listening.\n");

len=sizeof(cli);connfd=accept(sockfd,(SA *)&cli,&len);

if(connfd<0){printf("Server accept failed.\n");exit(0);}elseprintf("server accept client.\n");

func(connfd);close(sockfd);}

SERVER OUTPUT:

[cseastaff@localhost cn]$ cc tcp_echo_server.c[cseastaff@localhost cn]$ ./a.outSockect successfully created.Socket sucessfully binded.Server listening.server accept client.From client:haiFrom client:welcomeFrom client:exitBroken pipe

CLIENT OUTPUT:

[cseastaff@localhost cn]$ cc tcp_echo_client.c[cseastaff@localhost cn]$ ./a.outSockect sucessfully created.connected to the server..Enter the stringhaiFrom server:haiEnter the stringwelcomeFrom server:welcomeEnter the stringexitFrom server:exitClient Exit..

Page 4: Full Lab Program CN2010

Ex.No:3 Program to implement Chat Server and Client using TCP Sockets#include<stdio.h>#include<stdlib.h>#include<netinet/in.h>#include<sys/socket.h>#include<netdb.h>#include<string.h>#define MAX 80#define PORT 43464#define SA struct sockaddr

void func(int sockfd){char buff[MAX];int n;

for(;;){bzero(buff,sizeof(buff));printf("Enter the string");n=0;while((buff[n++]=getchar())!='\n');write(sockfd,buff,sizeof(buff));bzero(buff,sizeof(buff));read(sockfd,buff,sizeof(buff));printf("From server:%s",buff);

if((strncmp("exit",buff,4))==0){printf("Client Exit.\n");break;}

}}

int main(){int sockfd,connfd;struct sockaddr_in servaddr,cli;sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1){printf("sockect creation failed");exit(0);}elseprintf("Sockect sucessfully created.\n");

bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”);servaddr.sin_port=htons(PORT);

if(connect(sockfd,(SA

*)&servaddr,sizeof(servaddr))!=0){printf("connection with the server failed.\n");exit(0);}elseprintf("connected to the server.\n");

func(sockfd);close(sockfd);}

OUTPUT:[cseastaff@localhost cn]$ cc tcp_chat_client.c[cseastaff@localhost cn]$ ./a.out Sockect sucessfully created.connected to the server.Enter the stringhaiFrom server:haiEnter the stringhelloFrom server:exitClient Exit.[cseastaff@localhost cn]$

TCP_CHAT_SERVER

#include<stdio.h>#include<stdlib.h>#include<netinet/in.h>#include<sys/socket.h>#include<netdb.h>#include<string.h>#define MAX 80#define PORT 43464#define SA struct sockaddr

void func(int sockfd){char buff[MAX];int n;

for(;;){bzero(buff,MAX);read(sockfd,buff,sizeof(buff));printf("From client:%s\t To client:",buff);bzero(buff,MAX);n=0;while((buff[n++]=getchar())!='\n');write(sockfd,buff,sizeof(buff));

Page 5: Full Lab Program CN2010

if(strncmp("exit",buff,4)==0){printf("Server Exit..\n");break;}

}}

int main(){int sockfd, connfd,len;struct sockaddr_in servaddr,cli;sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1){printf("Sockect creation failed..\n");exit(0);}elseprintf("Sockect successfully created.\n");

bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(PORT);

if((bind(sockfd,(SA *)&servaddr,sizeof(servaddr)))!=0){printf("socket bind failed.\n");exit(0);}elseprintf("Socket sucessfully binded.\n");if((listen(sockfd,5))!=0){printf("Listen failed.\n");exit(0);}elseprintf("Server listening.\n");

len=sizeof(cli);connfd=accept(sockfd,(SA *)&cli,&len);

if(connfd<0){printf("Server accept failed.\n");exit(0);}elseprintf("server accept client.\n");

func(connfd);close(sockfd);}

OUTPUT:[cseastaff@localhost cn]$ cc tcp_chat_server.c[cseastaff@localhost cn]$ ./a.outSockect successfully created.Socket sucessfully binded.Server listening.server accept client.From client:haiTo client:haiFrom client:helloTo client:exitServer Exit..

Page 6: Full Lab Program CN2010

Ex.No: 4 Program to implement DNS to resolve Host Name using TCP SocketsClientSide:#include<sys/socket.h>#include<netinet/in.h>#include<stdio.h>#include<sys/types.h>#include<string.h>#include<netdb.h>int main(int argc,char *argv[]){int sockfd,portno,n,len;struct sockaddr_in servaddr,cli;struct hostent *server;char buffer[256];if(argc<3){fprintf(stderr,"type host name followed by port");exit(0);}portno=atoi(argv[2]);sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd<0)printerror("error in sockect creation");server=gethostbyname(argv[1]);bzero((char*)&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;bcopy((char*)server->h_addr,(char*)&servaddr.sin_addr.s_addr,server->h_length);servaddr.sin_port=htons(portno);printf("Enter the host name");bzero(buffer,256);fgets(buffer,255,stdin);len=sizeof(cli);n=sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&servaddr,len);if(n<0)printerror("Error in writing");bzero(buffer,256);printf("\nIP address is:");n=recvfrom(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)&servaddr,&len);if(n<0)printerror("Error in reading");printf("\n%s",buffer);return 0;}int printerror(char *msg){perror(msg);

exit(0);}

Server Side:

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdio.h>

#include<sys/types.h>

#include<string.h>

int main(int argc,char *argv[]){int sockfd,portno,clilen,n,flag=0;struct sockaddr_in servaddr,cli;char buffer[256],b2[256],b3[256];FILE *f;char ch='y';if(argc<2){fprintf(stderr,"No port provided");exit(0);}sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd<0)printerror("Error in socket");elseprintf("Sockect created");bzero((char*)&servaddr,sizeof(servaddr));portno=atoi(argv[1]);servaddr.sin_family=AF_INET;servaddr.sin_port=htons(portno);if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)printerror("Error in binding");elseprintf("Binded Sucessfully");clilen=sizeof(cli);bzero(buffer,256);n=recvfrom(sockfd,buffer,255,0,(struct sockaddr*)&cli,&clilen);if(n<0)printerror("Error in reading");printf("host name is %s",buffer);

Page 7: Full Lab Program CN2010

f=fopen("dns.txt","r");if(f==NULL)printf("File read error");fscanf(f,"%s %s",b2,b3);while(!feof(f)){strcat(b2,"\n");if(strcmp(buffer,b2)==0){strcpy(buffer,b3);flag=1;}if(flag)break;fscanf(f,"%s%s",b2,b3);}if(flag==0)strcpy(buffer,"No such host");printf("ip address is %s",buffer);n=sendto(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)&cli,clilen);if(n<0)printerror("Error in writing");return 0;}int printerror(char *msg){perror(msg);exit(0);}

INPUT File: dns.txtwww.google.com 202.192.66.102www.yahoo.com 192.190.55.30

OUTPUT:Client[Satff@linux cnlab2010]$ cc udp_dns_client.c

[Satff@linux cnlab2010]$ ./a.out 127.0.0.1 6060

Enter the host namewww.google.com

IP address is:

202.192.66.102[kujani@linux cnlab2010]$

OUTPUT: Server[Staff@linux cnlab2010]$ cc udp_dns_client.c

[Staff@linux cnlab2010]$ ./a.out 127.0.0.1 6060Enter the host namewww.google.comIP address is:202.192.66.102

Ex.No.5 : Implementation of PING program#include <unistd.h>#include <stdio.h>#include <sys/socket.h>#include <netinet/ip.h>#include <netinet/udp.h>// The packet length#define PCKT_LEN 8192// Can create separate header file (.h) for all headers' structure// The IP header's structurestruct ipheader { unsigned char iph_ihl:5, iph_ver:4; unsigned char iph_tos; unsigned short int iph_len; unsigned short int iph_ident; unsigned char iph_flag; unsigned short int iph_offset; unsigned char iph_ttl; unsigned char iph_protocol; unsigned short int iph_chksum; unsigned int iph_sourceip; unsigned int iph_destip;};

// UDP header's structurestruct udpheader { unsigned short int udph_srcport; unsigned short int udph_destport; unsigned short int udph_len; unsigned short int udph_chksum;};int main(int argc, char *argv[]){int sd;char buffer[PCKT_LEN];// Our own headers' structuresstruct ipheader *ip = (struct ipheader *) buffer;struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader));// Source and destination addresses: IP and portstruct sockaddr_in sin, din;

Page 8: Full Lab Program CN2010

int one = 1;const int *val = &one;memset(buffer, 0, PCKT_LEN);if(argc != 5){printf("- Invalid parameters!!!\n");printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);exit(-1);}// Create a raw socket with UDP protocolsd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);if(sd < 0){perror("socket() error\n");// If something wrong just exitexit(-1);}elseprintf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");// The address familysin.sin_family = AF_INET;din.sin_family = AF_INET;// Port numberssin.sin_port = htons(atoi(argv[2]));din.sin_port = htons(atoi(argv[4]));// IP addressessin.sin_addr.s_addr = inet_addr(argv[1]);din.sin_addr.s_addr = inet_addr(argv[3]);// Fabricate the IP header or we can use the// standard header structures but assign our own values.ip->iph_ihl = 5;ip->iph_ver = 4;ip->iph_tos = 16; // Low delayip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);ip->iph_ident = htons(54321);ip->iph_ttl = 64; // hopsip->iph_protocol = 17; // UDP// Source IP address, can use spoofed address here!!!ip->iph_sourceip = inet_addr(argv[1]);// The destination IP addressip->iph_destip = inet_addr(argv[3]);// Fabricate the UDP header. Source port number, redundant

udp->udph_srcport = htons(atoi(argv[2]));

// Destination port numberudp->udph_destport = htons(atoi(argv[4]));udp->udph_len = htons(sizeof(struct udpheader));// Calculate the checksum for integrityip->iph_chksum =rand();// Inform the kernel do not fill up the packet structure. we will build our own.../*if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0){perror("setsockopt() error");exit(-1);}elseprintf("setsockopt() is OK.\n");*/// Send loop, send for every 2 second for 100 countprintf("Trying...\n");printf("Using raw socket and UDP protocol\n");printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));int count;if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0){perror("sendto() error\n");exit(-1);}else{printf("Sendto() is OK\n");sleep(1);}close(sd);return 0;}

OUTPUT:

Page 9: Full Lab Program CN2010

Ex.No:6 Program to implement RPC using JavaProgram : AddClient.javaimport 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 Secondnumber is "+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);}}}Program: AddServer.javaimport 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);}}}

Program: AddServerImpl.javaimport 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;}}Program: AddServerintf.javaimport java.rmi.*;public interface AddServerIntf extends Remote {double add(double d1, double e2) throws RemoteException;}

Output:Client SideC:\jdk1.5.0_05\bin> javac AddClient.javaServer Side C:\jdk1.5.0_05\bin> javac AddServer.javaC:\jdk1.5.0_05\bin> javac AddServerInf.javaC:\jdk1.5.0_05\bin> javac AddServerImpl.java

Step1: After ComplingServer Side C:\jdk1.5.0_05\bin>start rmiregistryC:\jdk1.5.0_05\bin>rmic AddServerImplC:\jdk1.5.0_05\bin>java AddServer Client SideC:\jdk1.5.0_05\bin>java AddClient 127.0.0.1 5 5The First number is 5The Second number is 5The Sum is : 10

Page 10: Full Lab Program CN2010

Ex.No: 7 Program to implement Border Gateway Protocol #include<stdio.h>void main(){int n,i,j,k,a[10][10],b[10][10];clrscr();printf("enter the no of node");scanf("%d",&n);

for(i=0;i<n;i++){

for(j=0;j<n;j++) { printf("Enter the distance between the host %d%d\t",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("the output matrix is: \n" );

for(i=0;i<n;i++) {

for(j=0;j<n;j++) { printf("%d\t",b[i][j]); }

printf("\n"); }

}

OUTPUT:[Staff@localhost cnlab]$ cc bgp.c[Staff@localhost cnlab]$ ./a.outEnter the number of nodes 4Enter the distance between the host 11 0Enter the distance between the host 12 7Enter the distance between the host 13 2Enter the distance between the host 14 3Enter the distance between the host 21 2Enter the distance between the host 22 0Enter the distance between the host 23 6Enter the distance between the host 24 8Enter the distance between the host 31 1Enter the distance between the host 32 3Enter the distance between the host 33 0Enter the distance between the host 34 2Enter the distance between the host 41 2Enter the distance between the host 42 2Enter the distance between the host 43 2Enter the distance between the host 44 0

The cost between each nodes in Matric form:0 7 2 32 0 6 81 3 0 22 2 2 0

The Shortest path Output matrix is:0 5 2 32 0 4 51 3 0 22 2 2 0

Page 11: Full Lab Program CN2010

Ex.No:8 Program for simulation of Sliding Window Protocol#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int n,r;

struct frame

{

char ack;

int data;

}frm[10];

int sender(void);

void recvfrm(void);

void resend(void);

void resend1(void);

void goback(void);

void selective(void);

int main()

{

int c;

do

{

printf("\n 1.Selective repeat ARP \n 2. Goback ARQ\n 3.Exit");

printf("Enter the choice");

scanf("%d",&c);

switch(c)

{

case 1:

selective();

break;

case 2:

goback();

break;

case 3:

exit(0);

break;

}

}

while(c!=4);

return 0;

}

void goback()

{

sender();

recvfrm();

resend1();

printf("\n all packets sent successfully");

}

void selective()

{

sender();

recvfrm();

Page 12: Full Lab Program CN2010

resend();

printf("\n All packets sent succesfully");

}

int sender()

{

int i;

printf("Enter the number of packets to be sent");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("Enter the data for the packets[%d]:",i);

scanf("%d",&frm[i].data);

frm[i].ack='y';

}

return 0;

}

void recvfrm()

{

int i;

random();

r=rand()%n;

frm[r].ack='n';

for(i=0;i<n;i++)

{

if(frm[i].ack=='n')

{

printf("\n The packet no %d is no received \n",r);

}

}

}

void resend()

{

printf("\n Sending packet %d",r);

sleep(2);

frm[r].ack='y';

printf("\n The received packet is %d",frm[r].data);

}

void resend1()

{

int i;

printf("\n Resending from packet %d",r);

for(i=r;i<n;i++)

{

sleep(2);

frm[i].ack='y';

printf("\n Recieved data of packet %d is %d", i, frm[i].data);

}

}

OUTPUT:[Staff@linux cn]$ cc sliding.c

Page 13: Full Lab Program CN2010

[Staff@linux cn]$ ./a.out

1.Selective repeat ARP

2. Goback ARQ

3.ExitEnter the choice1

Enter the number of packets to be sent4

Enter the data for the packets[0]:1

Enter the data for the packets[1]:2

Enter the data for the packets[2]:3

Enter the data for the packets[3]:4

The packet no 2 is no received

Sending packet 2

The received packet is 3

All packets sent succesfully

1.Selective repeat ARP

2. Goback ARQ

3.ExitEnter the choice2

Enter the number of packets to be sent3

Enter the data for the packets[0]:10

Enter the data for the packets[1]:20

Enter the data for the packets[2]:30

The packet no 1 is no received

Resending from packet 1

Recieved data of packet 1 is 20

Recieved data of packet 2 is 30

All packets sent successfully

1.Selective repeat ARP

2. Goback ARQ

3.ExitEnter the choice3

Ex.No:10 Program to implement Address Resolution ProtocolClient Side:#include<stdio.h>

#include<stdlib.h>

#include<netinet/in.h>

#include<sys/socket.h>

#include<netdb.h>

#include<string.h>

main()

{

int sockfd,cid,n,i=0,j=0,x,m=0;

Page 14: Full Lab Program CN2010

char c,mesg[100],mesg1[100],mac[100],ip[100];

struct sockaddr_in servaddr;

for(i=0;i<100;i++)

{

mesg[i]=0;

mesg1[i]=0;

}

sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd<0)

printf("Error in socket");

else

printf("Sockect created");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htons(INADDR_ANY);

servaddr.sin_port=htons(6500);

if(cid=connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

{

printf("\nError in comnnection");

}

else

printf("Comnnection sucesses");

m=0;

do

{

m++;

printf("Enter the IP address");

c=getchar();

do

{

mesg[j]=c;

j++;

c=getchar();

}

while(c!='\n');

send(sockfd,mesg,sizeof(mesg),0);

recv(sockfd,mesg1,100,0);

printf("\nMAC address from server");

puts(mesg1);

mesg[i]=0;

}

while(m!=1);

close(sockfd);

}

Server Side:#include<sys/socket.h>

#include<netinet/in.h>

#include<stdio.h>

#include<sys/types.h>

#include<string.h>

Page 15: Full Lab Program CN2010

main()

{

int sockfd,aid,n,flag=0,i=0,j=0,x,m=0;

char c,mesg[100],mesg1[100];

char ip[3][20]={"192.168.11.100","172.16.5.57","172.16.5.10"};

char mac[3][20]={"12-C3-11-F3-32-E3","A1-12-C3-A2-B4-C4","B4-C4-11-23-42-22"};

struct sockaddr_in servaddr;

char buffer[256],b2[256],b3[256];

for(i=0;i<100;i++)

{

mesg[i]=0;

mesg1[i]=0;

}

sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd<0)

printf("Error in socket");

else

printf("Sockect created");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htons(INADDR_ANY);

servaddr.sin_port=htons(6500);

if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Error in binding");

else

printf("Binded Sucessfully");

listen(sockfd,5);

aid=accept(sockfd,(struct sockaddr*)NULL,NULL);

if(aid<0)

printf("error in accept");

m=0;

do

{

recv(aid,mesg,100,0);

printf("\nIP address from client");

puts(mesg);

printf("MAC address is sending");

for(i=0;i<3;i++)

{

x=strcmp(mesg,ip[i]);

if(x==0)

{

strcpy(mesg1,mac[i]);

break;

}

else

{

strcpy(mesg1,"Enter the correct IP address");

Page 16: Full Lab Program CN2010

}

}

send(aid,mesg1,sizeof(mesg1),0);

printf("\n");

for(i=0;i<100;i++)

mesg[i]=0;

}

while(m!=1);

close(sockfd);

}

OUTPUT:ClientSide: [Staff@linux cnlab2010]$ cc arpclient.c

[Staff@linux cnlab2010]$ ./a.out

Sockect createdComnnection sucessesEnter the IP address : 192.168.11.100

Server Side :[kujani@linux cnlab2010]$ cc arpserver.c

[kujani@linux cnlab2010]$ ./a.out

Sockect createdBinded Sucessfully

IP address from client: 192.168.11.100

MAC address is sending

IP address from client: 192.168.11.100

MAC address is sending

MAC address from server12-C3-11-F3-32-E3

Ex.No. : 9 Implementation of UNICAST routing protocolProgram:set ns [new Simulator]#Define different colors for data flows (for NAM)$ns color 1 Blue$ns color 2 Red#Open the Trace fileset file1 [open unicast.tr w]$ns trace-all $file1#Open the NAM trace fileset file2 [open unicast.nam w]$ns namtrace-all $file2#Define a 'finish' procedureproc finish {} { global ns file1 file2 $ns flush-trace close $file1 close $file2 exec nam unicast.nam & exit 0}# Next line should be commented out to have the static routing$ns rtproto DV#Create six nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]#Create links between the nodes$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail#Setup a TCP connectionset tcp [new Agent/TCP/Newreno]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink/DelAck]$ns attach-agent $n5 $sink

Page 17: Full Lab Program CN2010

$ns connect $tcp $sink$tcp set fid_ 1#Setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP$ns rtmodel-at 1.0 down $n1 $n4$ns rtmodel-at 4.5 up $n1 $n4$ns at 0.1 "$ftp start"$ns at 6.0 "finish"$ns run

OUTPUTcsestaff@csestaff-desktop:~$ ns ex1.tcl

Screen Shot

Ex: No: 11 Study of UDP Performanceset ns [new Simulator]$ns color 0 blue$ns color 1 red$ns color 2 whiteset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set f [open out.tr w]$ns trace-all $fset nf [open out3.nam w]$ns namtrace-all $nf$ns duplex-link $n0 $n2 5Mb 2ms DropTail$ns duplex-link $n1 $n2 5Mb 2ms DropTail$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail$ns duplex-link-op $n0 $n2 orient right-up$ns duplex-link-op $n1 $n2 orient right-down$ns duplex-link-op $n2 $n3 orient right$ns duplex-link-op $n2 $n3 queuePos 0.5set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0set udp1 [new Agent/UDP]$ns attach-agent $n3 $udp1$udp1 set class_ 0set cbr1 [new Application/Traffic/CBR]$cbr1 attach-agent $udp1set null0 [new Agent/Null]$ns attach-agent $n1 $null0set null1 [new Agent/Null]$ns attach-agent $n1 $null1$ns connect $udp0 $null0$ns connect $udp1 $null1$ns at 1.0 "$cbr0 start"$ns at 1.1 "$cbr1 start"puts [$cbr0 set packetSize_]puts [$cbr0 set interval_]$ns at 3.0 "finish"proc finish {} {

global ns f nf$ns flush-traceclose $fclose $nf

Page 18: Full Lab Program CN2010

puts "running nam..."exec nam out2.nam &exit 0

}$ns run

OUTPUT:csestaff@csestaff-desktop:~$ ns ex14.tcl

210

0.0037499999999999999

running nam...

Ex.No: 12.Study of TCP Performanceset ns [new Simulator]$ns color 0 blue$ns color 1 red$ns color 2 whiteset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set f [open out.tr w]$ns trace-all $fset nf [open out.nam w]$ns namtrace-all $nf$ns duplex-link $n0 $n2 5Mb 2ms DropTail$ns duplex-link $n1 $n2 5Mb 2ms DropTail$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail$ns duplex-link-op $n0 $n2 orient right-up$ns duplex-link-op $n1 $n2 orient right-down$ns duplex-link-op $n2 $n3 orient right$ns duplex-link-op $n2 $n3 queuePos 0.5set tcp [new Agent/TCP]$tcp set class_ 1set sink [new Agent/TCPSink]$ns attach-agent $n1 $tcp$ns attach-agent $n3 $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 1.2 "$ftp start"$ns at 1.35 "$ns detach-agent $n1 $tcp ; $ns detach-agent $n3 $sink"$ns at 3.0 "finish"proc finish {} {

global ns f nf

$ns flush-traceclose $fclose $nfputs "running nam..."exec nam out.nam &exit 0

}

$ns runOUTPUT:csestaff@csestaff-desktop:~$ ns ex15.tcl

Screen Shot