You are on page 1of 15

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,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); } 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); } } Output SERVER SIDE OUTPUT Socket Opened Binded successfully Accepted CLIENT SIDE OUTPUT WITH SERVER TIME Socket is Opened Connected successfully Message Read Mon Jan 22 15:09:19 2007

if(connect(sockfd,(SA Ex.No:2 Program to implement Echo Server *)&servaddr,sizeof(servaddr))!=0) and Client using TCP Sockets { Client Side printf("connection with the server #include<stdio.h> failed..\n"); #include<netinet/in.h> exit(0); #include<sys/socket.h> } #include<netdb.h> else #include<string.h> printf("connected to the server..\n"); #define MAX 80 func(sockfd); #define PORT 43464 close(sockfd); #define SA struct sockaddr } Server Side void func(int sockfd) #include<stdio.h> { #include<netinet/in.h> char buff[MAX]; #include<sys/types.h> int n; #include<sys/socket.h> for(;;) #include<netdb.h> { #include<string.h> bzero(buff,sizeof(buff)); #define MAX 80 printf("Enter the string"); #define PORT 43464 n=0; #define SA struct sockaddr while((buff[n++]=getchar())!='\n'); write(sockfd,buff,sizeof(buff)); void func(int sockfd) bzero(buff,sizeof(buff)); { read(sockfd,buff,sizeof(buff)); char buff[MAX]; printf("From server:%s",buff); int n; if((strncmp("exit",buff,4))==0) for(;;) { { printf("Client Exit..\n"); bzero(buff,MAX); break; read(sockfd,buff,sizeof(buff)); } printf("From client:%s\t",buff); } write(sockfd,buff,sizeof(buff)); } bzero(buff,sizeof(buff)); int main() { if((strncmp(buff,"exit",4))==0) int sockfd,connfd; { struct sockaddr_in servaddr,cli; printf("server exit...\n"); sockfd=socket(AF_INET,SOCK_STREAM,0); } if(sockfd==-1) } { } printf("sockect creation failed"); int main() exit(0); { } int len,sockfd,connfd; else struct sockaddr_in servaddr,cli; printf("Sockect sucessfully created.\n"); sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); if(sockfd==-1) servaddr.sin_family=AF_INET; { servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); printf("Sockect creation failed..\n"); servaddr.sin_port=htons(PORT); exit(0); }

else printf("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); SERVER OUTPUT: if((bind(sockfd,(SA *)&servaddr,sizeof(servaddr)))!=0) [cseastaff@localhost cn]$ cc tcp_echo_server.c { [cseastaff@localhost cn]$ ./a.out printf("socket bind failed.\n"); Sockect successfully created. exit(0); Socket sucessfully binded. } Server listening. else server accept client. printf("Socket sucessfully binded.\n"); From client:hai if((listen(sockfd,5))!=0) From client:welcome { From client:exit printf("Listen failed.\n"); Broken pipe exit(0); } CLIENT OUTPUT: else printf("Server listening.\n"); [cseastaff@localhost cn]$ cc tcp_echo_client.c len=sizeof(cli); [cseastaff@localhost cn]$ ./a.out connfd=accept(sockfd,(SA *)&cli,&len); Sockect sucessfully created. if(connfd<0) connected to the server.. { Enter the stringhai printf("Server accept failed.\n"); From server:hai exit(0); Enter the stringwelcome } From server:welcome else Enter the stringexit printf("server accept client.\n"); From server:exit Client Exit.. func(connfd); close(sockfd); }

*)&servaddr,sizeof(servaddr))!=0) Ex.No:3 Program to implement Chat Server { and Client using TCP Sockets printf("connection with the server #include<stdio.h> failed.\n"); #include<stdlib.h> exit(0); #include<netinet/in.h> } #include<sys/socket.h> else #include<netdb.h> printf("connected to the server.\n"); #include<string.h> func(sockfd); #define MAX 80 close(sockfd); #define PORT 43464 } #define SA struct sockaddr OUTPUT: void func(int sockfd) [cseastaff@localhost cn]$ cc tcp_chat_client.c { [cseastaff@localhost cn]$ ./a.out char buff[MAX]; Sockect sucessfully created. int n; connected to the server. for(;;) Enter the stringhai { From server:hai bzero(buff,sizeof(buff)); Enter the stringhello printf("Enter the string"); From server:exit n=0; Client Exit. while((buff[n++]=getchar())!='\n'); [cseastaff@localhost cn]$ write(sockfd,buff,sizeof(buff)); bzero(buff,sizeof(buff)); read(sockfd,buff,sizeof(buff)); TCP_CHAT_SERVER printf("From server:%s",buff); if((strncmp("exit",buff,4))==0) #include<stdio.h> { #include<stdlib.h> printf("Client Exit.\n"); #include<netinet/in.h> break; #include<sys/socket.h> } #include<netdb.h> } #include<string.h> } #define MAX 80 #define PORT 43464 int main() #define SA struct sockaddr { int sockfd,connfd; void func(int sockfd) struct sockaddr_in servaddr,cli; { sockfd=socket(AF_INET,SOCK_STREAM,0); char buff[MAX]; if(sockfd==-1) int n; { for(;;) printf("sockect creation failed"); { exit(0); bzero(buff,MAX); } read(sockfd,buff,sizeof(buff)); else printf("From client:%s\t To client:",buff); printf("Sockect sucessfully created.\n"); bzero(buff,MAX); bzero(&servaddr,sizeof(servaddr)); n=0; servaddr.sin_family=AF_INET; while((buff[n++]=getchar())!='\n'); servaddr.sin_addr.s_addr=inet_addr(127.0.0.1); write(sockfd,buff,sizeof(buff)); servaddr.sin_port=htons(PORT); if(connect(sockfd,(SA

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); } else printf("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); } else printf("Socket sucessfully binded.\n"); if((listen(sockfd,5))!=0) { printf("Listen failed.\n"); exit(0); } else printf("Server listening.\n"); len=sizeof(cli); connfd=accept(sockfd,(SA *)&cli,&len); if(connfd<0) { printf("Server accept failed.\n"); exit(0); } else printf("server accept client.\n"); func(connfd); close(sockfd); }

OUTPUT: [cseastaff@localhost cn]$ cc tcp_chat_server.c [cseastaff@localhost cn]$ ./a.out Sockect successfully created. Socket sucessfully binded. Server listening. server accept client. From client:hai To client:hai From client:hello To client:exit Server Exit..

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

exit(0); }

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.txt www.google.com 216.239.51.99 www.yahoo.com 69.147.76.15 www.gmail.com 64.233.161.83

[Staff@linux cnlab2010]$ ./a.out 127.0.0.1 6060 Enter the host namewww.google.com IP 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 structure struct 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 structure struct udpheader { OUTPUT:Client unsigned short int udph_srcport; [Satff@linux cnlab2010]$ cc udp_dns_client.c unsigned short int udph_destport; unsigned short int udph_len; [Satff@linux cnlab2010]$ ./a.out 127.0.0.1 6060 unsigned short int udph_chksum; }; Enter the host namewww.google.com int main(int argc, char *argv[]) { int sd; IP address is: char buffer[PCKT_LEN]; // Our own headers' structures 202.192.66.102[kujani@linux cnlab2010]$ struct ipheader *ip = (struct ipheader *) buffer; struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader)); OUTPUT: Server // Source and destination addresses: IP and port [Staff@linux cnlab2010]$ cc udp_dns_client.c struct sockaddr_in sin, din;

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 protocol sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); if(sd < 0) { perror("socket() error\n"); // If something wrong just exit exit(-1); } else printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n"); // The address family sin.sin_family = AF_INET; din.sin_family = AF_INET; // Port numbers sin.sin_port = htons(atoi(argv[2])); din.sin_port = htons(atoi(argv[4])); // IP addresses sin.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 delay ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader); ip->iph_ident = htons(54321); ip->iph_ttl = 64; // hops ip->iph_protocol = 17; // UDP // Source IP address, can use spoofed address here!!! ip->iph_sourceip = inet_addr(argv[1]); // The destination IP address ip->iph_destip = inet_addr(argv[3]); // Fabricate the UDP header. Source port number, redundant udp->udph_srcport = htons(atoi(argv[2]));

// Destination port number udp->udph_destport = htons(atoi(argv[4])); udp->udph_len = htons(sizeof(struct udpheader)); // Calculate the checksum for integrity ip->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); } else printf("setsockopt() is OK.\n"); */ // Send loop, send for every 2 second for 100 count printf("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:

Ex.No:6 Program to implement RPC using System.out.println("Exception:",e); Java } Program : AddClient.java } import java.rmi.*; } public class AddClient { Program: AddServerImpl.java public static void main(String args[]) import java.rmi.*; { try import java.rmi.server.*; { String addServerURL="rmi://" + args[0] + public class AddServerImpl extends "/AddServer"; UnicastRemoteObject implements AddServerIntf AddServerIntf { addServerIntf=(AddServerIntf)Naming.lookup(ad public AddServerImpl() throws RemoteException dServerURL); { } System.out.println("The First number is public double add(double d1, double d2) throws "+args[1]); RemoteException double d1= { Double.valueOf(args[1]).doubleValue(); return d1+d2; } System.out.println("The Secondnumber is } "+args[2]); Program: AddServerintf.java import java.rmi.*; double d2= public interface AddServerIntf extends Remote Double.valueOf(args[2]).doubleValue(); { double add(double d1, double e2) throws System.out.println("The Sum is :"+ RemoteException; addServerIntf.add(d1,d2)); } } catch(Exception e) Output: { Client Side System.out.println(Exception "+e); C:\jdk1.5.0_05\bin> javac AddClient.java } Server Side } C:\jdk1.5.0_05\bin> javac AddServer.java } C:\jdk1.5.0_05\bin> javac AddServerInf.java Program: AddServer.java C:\jdk1.5.0_05\bin> javac AddServerImpl.java import java.net.*; import java.rmi.*; Step1: After Compling public class AddServer Server Side { C:\jdk1.5.0_05\bin>start rmiregistry public static void main(String args[]) C:\jdk1.5.0_05\bin>rmic AddServerImpl { C:\jdk1.5.0_05\bin>java AddServer try Client Side { C:\jdk1.5.0_05\bin>java AddClient 127.0.0.1 5 AddServerImpl addServerImpl =new 5 AddServerImpl(); The First number is 5 The Second number is 5 Naming.rebind("AddServer", addServerImpl); The Sum is : 10 } catch(Exception e) {

} } 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.out Enter the number of nodes 4 Enter the distance between the host 11 Enter the distance between the host 12 Enter the distance between the host 13 Enter the distance between the host 14 Enter the distance between the host 21 Enter the distance between the host 22 Enter the distance between the host 23 Enter the distance between the host 24 Enter the distance between the host 31 Enter the distance between the host 32 Enter the distance between the host 33 Enter the distance between the host 34 Enter the distance between the host 41 Enter the distance between the host 42 Enter the distance between the host 43 Enter the distance between the host 44

0 7 2 3 2 0 6 8 1 3 0 2 2 2 2 0

The cost between each nodes in Matric form: 0 7 2 3 2 0 6 8 1 3 0 2 2 2 2 0 The Shortest path Output matrix is: 0 5 2 3 2 0 4 5 1 3 0 2 2 2 2 0

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(); 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 [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.Exit Enter 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.Exit Enter the choice3 Ex.No:10 Program to implement Address Resolution Protocol Client 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; 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); }

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++) { Server Side: x=strcmp(mesg,ip[i]); #include<sys/socket.h> if(x==0) #include<netinet/in.h> { #include<stdio.h> strcpy(mesg1,mac[i]); #include<sys/types.h> break; #include<string.h> } else main() { { strcpy(mesg1,"Enter the correct IP address"); int sockfd,aid,n,flag=0,i=0,j=0,x,m=0; } char c,mesg[100],mesg1[100]; } char ip[3] send(aid,mesg1,sizeof(mesg1),0); [20]={"192.168.11.100","172.16.5.57","172.16.5. printf("\n"); 10"}; for(i=0;i<100;i++) char mac[3][20]={"12-C3-11-F3-32-E3","A1-12- mesg[i]=0; C3-A2-B4-C4","B4-C4-11-23-42-22"}; } struct sockaddr_in servaddr; while(m!=1); char buffer[256],b2[256],b3[256]; close(sockfd); for(i=0;i<100;i++) } { mesg[i]=0; OUTPUT: mesg1[i]=0; ClientSide: } [Staff@linux cnlab2010]$ cc arpclient.c sockfd=socket(AF_INET,SOCK_STREAM,0); [Staff@linux cnlab2010]$ ./a.out if(sockfd<0) Sockect createdComnnection sucessesEnter the IP printf("Error in socket"); address : 192.168.11.100 else Server Side : printf("Sockect created"); [kujani@linux cnlab2010]$ cc arpserver.c [kujani@linux cnlab2010]$ ./a.out servaddr.sin_family=AF_INET; Sockect createdBinded Sucessfully servaddr.sin_addr.s_addr=htons(INADDR_ANY) IP address from client: 192.168.11.100 ; MAC address is sending servaddr.sin_port=htons(6500); IP address from client: 192.168.11.100 if(bind(sockfd,(struct MAC address is sending sockaddr*)&servaddr,sizeof(servaddr))<0) MAC address from server12-C3-11-F3-32-E3 printf("Error in binding"); else printf("Binded Sucessfully"); listen(sockfd,5); aid=accept(sockfd,(struct sockaddr*)NULL,NULL); if(aid<0)

Ex.No. : 9 Implementation of UNICAST routing protocol Program: set ns [new Simulator] #Define different colors for data flows (for NAM)OUTPUT $ns color 1 Blue csestaff@csestaff-desktop:~$ ns ex1.tcl $ns color 2 Red Screen Shot #Open the Trace file set file1 [open unicast.tr w] $ns trace-all $file1 #Open the NAM trace file set file2 [open unicast.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc 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 nodes set n0 [$ns node] set n1 [$ns node] Ex: No: 11 Study of UDP Performance set n2 [$ns node] set ns [new Simulator] set n3 [$ns node] $ns color 0 blue set n4 [$ns node] $ns color 1 red set n5 [$ns node] $ns color 2 white #Create links between the nodes $ns duplex-link $n0 $n1 0.3Mb 10ms DropTail set n0 [$ns node] $ns duplex-link $n1 $n2 0.3Mb 10ms DropTail set n1 [$ns node] $ns duplex-link $n2 $n3 0.3Mb 10ms DropTail set n2 [$ns node] $ns duplex-link $n1 $n4 0.3Mb 10ms DropTail set n3 [$ns node] $ns duplex-link $n3 $n5 0.5Mb 10ms DropTail set f [open out.tr w] $ns duplex-link $n4 $n5 0.5Mb 10ms DropTail $ns trace-all $f set nf [open out3.nam w] #Setup a TCP connection $ns namtrace-all $nf set tcp [new Agent/TCP/Newreno] $ns duplex-link $n0 $n2 5Mb 2ms DropTail $ns attach-agent $n0 $tcp $ns duplex-link $n1 $n2 5Mb 2ms DropTail set sink [new Agent/TCPSink/DelAck] $ns duplex-link $n2 $n3 1.5Mb 10ms DropTail $ns attach-agent $n5 $sink $ns duplex-link-op $n0 $n2 orient right-up $ns connect $tcp $sink $ns duplex-link-op $n1 $n2 orient right-down $tcp set fid_ 1 $ns duplex-link-op $n2 $n3 orient right #Setup a FTP over TCP connection $ns duplex-link-op $n2 $n3 queuePos 0.5 set ftp [new Application/FTP] set udp0 [new Agent/UDP] $ftp attach-agent $tcp $ns attach-agent $n0 $udp0 $ftp set type_ FTP set cbr0 [new Application/Traffic/CBR]

$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

$cbr0 attach-agent $udp0 set udp1 [new Agent/UDP] $ns attach-agent $n3 $udp1 $udp1 set class_ 0 set cbr1 [new Application/Traffic/CBR] $cbr1 attach-agent $udp1 set null0 [new Agent/Null] $ns attach-agent $n1 $null0 set 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-trace close $f close $nf puts "running nam..." exec nam out2.nam & exit 0 } $ns run OUTPUT: csestaff@csestaff-desktop:~$ ns ex14.tcl 210 0.0037499999999999999 running nam...

$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.5 set tcp [new Agent/TCP] $tcp set class_ 1 set sink [new Agent/TCPSink] $ns attach-agent $n1 $tcp $ns attach-agent $n3 $sink $ns connect $tcp $sink set 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-trace close $f close $nf puts "running nam..." exec nam out.nam & exit 0 } $ns run OUTPUT: csestaff@csestaff-desktop:~$ ns ex15.tcl Screen Shot

Ex.No: 12.Study of TCP Performance set ns [new Simulator] $ns color 0 blue $ns color 1 red $ns color 2 white set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set f [open out.tr w] $ns trace-all $f set 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

You might also like