+ All Categories
Home > Documents > Cs 9216 Networking Lab Cor c

Cs 9216 Networking Lab Cor c

Date post: 27-Oct-2014
Category:
Upload: rajakumaran-mohan
View: 142 times
Download: 1 times
Share this document with a friend
Popular Tags:
57
AIM: To write a program to transfer string between client and server using TCP. ALGORITHM: Step 1: Start the program. Step 2: Include the necessary header files. Step 3: To create the socket using socket() function. Step 4: The TCP- Transmission Control Protocol is one the core protocols of the internet protocol suite. Using TCP ,application s on networked hosts can create connection to one, over which they can exchange data or packets. Step 6: The data from the client is read by a read() and write by data using write(). Step 7:The string is transferred by client to server. if the server is ready its sends the sequence of the requested string to the client. Step 8: Print out with the necessary details. Step 9: Stop the program. Ex.No:1 TCP SOCKET Date:
Transcript
Page 1: Cs 9216 Networking Lab Cor c

AIM:

To write a program to transfer string between client and server using TCP.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: To create the socket using socket() function.

Step 4: The TCP- Transmission Control Protocol is one the core protocols of the internet protocol suite. Using TCP ,application s on networked hosts can create connection to one, over which they can exchange data or packets.

Step 6: The data from the client is read by a read() and write by data using write().

Step 7:The string is transferred by client to server. if the server is ready its sends the sequence of the requested string to the client.

Step 8: Print out with the necessary details.

Step 9: Stop the program.

Ex.No:1 TCP SOCKET

Date:

Page 2: Cs 9216 Networking Lab Cor c

SOURCE CODE:

Server:

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/types.h>int main(){ int por; printf("\n Enter the por no:"); scanf("%d",&por); int a,n; int sersock,newsock; char str[25],str2[25]; struct sockaddr_in seraddr,cliinfo; socklen_t csize=sizeof(cliinfo); seraddr.sin_family=AF_INET; seraddr.sin_port=htons(por); seraddr.sin_addr.s_addr=htonl(INADDR_ANY); if((sersock=socket(AF_INET,SOCK_STREAM,0))<0) { perror("\n SOCKET CREATION ERROR"); exit(0); } if(bind(sersock,(struct sockaddr*)&seraddr,sizeof(seraddr))<0) { perror("\n BIND ERROR"); exit(0); } if(listen(sersock,1)<0) { perror("\n LISTEN ERROR"); exit(0); } if((newsock=accept(sersock,(struct sockaddr*)&cliinfo,&csize))<0) { perror("\n ACCEPT ERROR"); exit(0);

Page 3: Cs 9216 Networking Lab Cor c

} else printf("\n SERVER CONNECTED TO %s\n",inet_ntoa(cliinfo.sin_addr)); read(newsock,str,sizeof(str)); do { printf("\n CLIENT:%s\n",str); printf("\n SERVER:"); scanf("%s",str2); write(newsock,str2,sizeof(str2)); listen(newsock,1); read(newsock,str,sizeof(str)); n=strcmp(str,"bye"); a=strcmp(str2,"bye"); } while(n!=0 || a!=0); close(sersock); close(newsock); return 0; }

Client:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/types.h>#include <sys/socket.h>int main(int count,char *arg[]){ int por; printf("\n Enter the port no:"); scanf("%d",&por); int a; int clisock; char str[25],str2[25]; struct sockaddr_in cliaddr; cliaddr.sin_family=PF_INET; cliaddr.sin_port=htons(por); cliaddr.sin_addr.s_addr=inet_addr(arg[1]); if((clisock=socket(PF_INET,SOCK_STREAM,0))<0)

Page 4: Cs 9216 Networking Lab Cor c

{ perror("\n SOCKET CREATION ERROR"); exit(0); } if(connect(clisock,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0) { perror("\n CONNECT ERROR"); exit(0); } printf("\n CLIENT CONNECTED TO %s\n",arg[1]); printf("\n CLIENT:"); scanf("%s",str); if(write(clisock,str,sizeof(str))<0) { printf("\n DATA COULD NOT BE SENT"); }

do { listen(clisock,1); read(clisock,str,sizeof(str)); printf("\n SERVER:%s",str); printf("\n CLIENT:"); scanf("%s",&str2); write(clisock,str2,sizeof(str2)); a=strcmp(str2,"bye"); } while(a!=0); close(clisock); return 0;}

Page 5: Cs 9216 Networking Lab Cor c

OUTPUT:

SERVER:

[05mecse090@networkserver ~]$ cc tcpser.c[05mecse090@networkserver ~]$ ./a.out Enter the por no:6789 SERVER CONNECTED TO 197.168.2.225 CLIENT:hai SERVER:hello CLIENT:hru

SERVER:fine

CLIENT:bye

SERVER:bye

CLIENT:

[05mecse090@networkserver ~]$ cc tcpcli.c[[05mecse090@networkserver ~]$./a.out 197.168.2.225

Enter the port no:6789 CLIENT CONNECTED TO 197.168.2.225

CLIENT:hai

SERVER:hello CLIENT:hru

SERVER:fine CLIENT:bye

Page 6: Cs 9216 Networking Lab Cor c

RESULT:

Thus, the string transfer using TCP has been executed successfully.

Ex.No:2 UDP SOCKET CREATION

Date:

Page 7: Cs 9216 Networking Lab Cor c

AIM:

To write a program to transfer string between client and server using UDP.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: To create the socket using socket() functon.

Step 4: Port is the number that indicates what kind of protocol a server on the internet using.

Step 5: A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and noted.

Step 6: Datagram packets are used to implemented a connectionless packet delivery service. each message is routed fro one machine to another based solely on information contained within that packet.

Step 7:The string is transferred by client to server. if the server is ready its sends the sequence of the requested string to the client.

Step 8: Print out with the necessary details.

Step 9: Stop the program.

SOURCE CODE:

Page 8: Cs 9216 Networking Lab Cor c

Server:#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>int main(int argc,char **argv){ int por; printf("Enter the port no:"); scanf("%d",&por); int sersock; int size,val; char msg[1024]; struct sockaddr_in seraddr,cliinfo; size=sizeof(struct sockaddr); socklen_t csize=sizeof(cliinfo); seraddr.sin_family=AF_INET; seraddr.sin_port=htons(por); seraddr.sin_addr.s_addr=htonl(INADDR_ANY); if((sersock=socket(AF_INET,SOCK_DGRAM,0))<0) { perror("SOCKET CREATION ERROR"); exit(0); } if((bind(sersock,(struct sockaddr*)&seraddr,size))<0) { perror("BIND ERROR"); exit(0); } printf("\n UDP SOCKET CREATED.... LISTENING IN PORT %d\n",por); while(strcmp(msg,"bye")!=0) { if((val=recvfrom(sersock,msg,1024,0,(struct sockaddr*)&cliinfo,&csize)<0) { perror("RECVFROM ERROR"); exit(0); } else printf("SERVER RECEIVED THE MESSAGE:%s\n",msg); if(strcmp(msg,"bye")==0) { printf("\n SERVER:"); scanf("%s",msg);

Page 9: Cs 9216 Networking Lab Cor c

exit(0); } printf("\n SERVER:"); scanf("%s",msg); if((sendto(sersock,msg,val,0,(struct sockaddr*)&cliinfo,csize))<0) { perror("\n SENDTO ERROR"); exit(0); } } close(sersock); return 0;}

CLIENT:

#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>

int main(int argc,char **argv){ int por; printf("Enter the port no:"); scanf("%d",&por); if(argc<2) { printf("INSUFFICIENT PARAMETERS"); exit(0); } struct sockaddr_in cliaddr; int clisock; int size=sizeof(struct sockaddr); char msg[1024],str[1024];

if((clisock=socket(AF_INET,SOCK_DGRAM,0))<0) { perror("\n SOCKET CREATION ERROR"); exit(0); } socklen_t csize=sizeof(cliaddr); cliaddr.sin_family=AF_INET; cliaddr.sin_port=htons(por);

Page 10: Cs 9216 Networking Lab Cor c

cliaddr.sin_addr.s_addr=inet_addr(argv[1]); printf("CONNECTION SUCCESSFUL USING UDP SOCKET"); if(strcmp(msg,"bye")==0) exit(0); while(strcmp(msg,"bye")!=0) { printf("\n CLIENT:"); scanf("%s",msg);

if((sendto(clisock,msg,sizeof(msg),0,(struct sockaddr*)&cliaddr, sizeof(cliaddr)))<0) { perror("\n SEND TO ERROR"); exit(0); } if(strcmp(msg,"bye")==0) exit(0); printf("\n FROM SERVER:"); if((recvfrom(clisock,str,1024,0,NULL,NULL))<0) { perror("RECV FROM ERROR"); exit(0); } printf("%s",str); } close(clisock); return 0;}

OUTPUT:

Page 11: Cs 9216 Networking Lab Cor c

SERVER:

[05mecse090@networkserver ~]$ cc udpserver.c[05mecse090@networkserver ~]$./a.outEnter the port no:5897

UDP SOCKET CREATED.... LISTENING IN PORT 5897SERVER RECEIVED THE MESSAGE:hello

SERVER:haiSERVER RECEIVED THE MESSAGE:hru

SERVER:fineSERVER RECEIVED THE MESSAGE:bye

SERVER:bye

CLIENT:

[05mecse090@networkserver ~]$ cc udpclient.c[05mecse090@networkserver ~]$./a.out 197.168.2.225Enter the port no:5897CONNECTION SUCCESSFUL USING UDP SOCKET CLIENT:hello

FROM SERVER:hai CLIENT:hru

FROM SERVER:fine CLIENT:bye

Page 12: Cs 9216 Networking Lab Cor c

RESULT:

Thus, the UDP Server-Client has been executed successfully.

Ex.No:3 SIMULATION OF SLIDING WINDOW PROTOCOL

Date:

Page 13: Cs 9216 Networking Lab Cor c

AIM:To write a C program for sliding window protocol.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: To create the socket using socket() functon.

Step 4: Enter the number of frames.

Step 5: And the corresponding message is send to receiver.

Step 6: Acknowledgement is received by receiver.

Step 7: If u send another message,ACK 2 message is received.

Step 8: Send the acknnowledge to sender.

Step 9: Print out with the necessary details.

Step 10: Stop the program.

Page 14: Cs 9216 Networking Lab Cor c

SOURCE CODE:

Server:

#include<string.h>#include<stdio.h>#include<netdb.h>#include<netinet/in.h>#include<sys/types.h>#include<sys/socket.h>#include<errno.h>int main(int argc,char ** argv){struct sockaddr_in saddr,caddr;int r,len,ssid,csid,pid,pid1,i,n;char wbuffer[1024],rbuffer[1024];float c;if(argc<2)fprintf(stderr,"Port number not specified\n");ssid=socket(AF_INET,SOCK_STREAM,0);if(ssid<0)perror("Socket failed\n");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(ssid,(struct sockaddr *)&saddr,sizeof(saddr))<0)perror("Socket Bind\n");listen(ssid,5);len=sizeof(caddr);csid=accept(ssid,(struct sockaddr *)&caddr,&len);if(csid<0)perror("Socket Accept\n");fprintf(stdout,"TYPE MESSAGE TO CLIENT\n");pid=fork();if(pid==0){while(1){bzero(rbuffer,1024);n=read(csid,rbuffer,1024);if(n==0)perror("Socket read\n");elsefprintf(stdout,"MESSAGE FROM CLIENT: %s\n",rbuffer);}exit(0);}else{while(1)

Page 15: Cs 9216 Networking Lab Cor c

{bzero(wbuffer,1024);fgets(wbuffer,1024,stdin);n=write(csid,wbuffer,1024);if(n==0)perror("Socket Write");}}return(0);}

CLIENT:

#include<stdio.h>#include<netdb.h>#include<netinet/in.h>#include<sys/types.h>#include<sys/socket.h>#include<errno.h>int main(int argc,char ** argv){struct sockaddr_in saddr;struct hostent *server;int n,ssid,csid,pid,pi;char wbuffer[1024],rbuffer[1024];char str[15];if(argc<3)fprintf(stderr,"Parameter inadequate\n");csid=socket(AF_INET,SOCK_STREAM,0);if(csid<0)perror("Socket Failed\n");bzero((char *)&saddr,sizeof(saddr));server=gethostbyname(argv[1]);saddr.sin_family=AF_INET;saddr.sin_port=htons(atoi(argv[2]));bcopy((char *)server->h_addr,(char *)&saddr.sin_addr.s_addr,server->h_length);ssid=connect(csid,(struct sockaddr *)&saddr,sizeof(saddr));if(ssid<0)perror("Socket Connect\n");fprintf(stdout,"ENTER MESSAGE TO SERVER:\n");pid=fork();if(pid==0){while(1){bzero(wbuffer,1024);fgets(wbuffer,1024,stdin);n=write(csid,wbuffer,sizeof(wbuffer));if(n==0)perror("Socket Write");}exit(0);}

Page 16: Cs 9216 Networking Lab Cor c

else{while(1){bzero(rbuffer,1024);n=read(csid,rbuffer,sizeof(rbuffer));if(n==0)perror("Socket Read\n");elsefprintf(stdout,"MESSAGE FROM SERVER: %s\n",rbuffer);}return(0);}}

OUTPUT:

Page 17: Cs 9216 Networking Lab Cor c

SERVER:

[05mecse090@networkserver ~]$ cc chatserv.c[05mecse090@networkserver ~]$./a.out 9898TYPE MESSAGE TO CLIENTMESSAGE FROM CLIENT: hi

hai

CLIENT:

[05mecse090@networkserver ~]$ cc chatcli.c[05mecse090@networkserver ~]$cc chatcli.c[05mecse090@networkserver ~]$ ./a.out 20.30.1.211 9898ENTER MESSAGE TO SERVER:hiMESSAGE FROM SERVER: hai

Page 18: Cs 9216 Networking Lab Cor c

RESULT:

Thus the c program for the simulation of sliding window protocol has been executed and the output is verified successfully.

Ex.No:4 FILE TRANSFER PROTOCOL

Date:

Page 19: Cs 9216 Networking Lab Cor c

AIM:

To write a program for the file transfer using ftp

ALGORITHM:

Client

Step 1: start the program

Step 2: Declare the variables and structure for sockets

Step 3: And then get the port number

Step 4: Create a socket using socket functions

Step 5: The socket is binded at the specified port

Step 6: Using the object, the port and address are declared

Step 7: Get the source file and the destination file from the user

Step 8: Use the send command for sending the two strings

Step 9: Receive the bytes sent from the server

Step 10: Print it in the console

Step 11: Close the socket

Page 20: Cs 9216 Networking Lab Cor c

Server

Step 1: Start the program

Step 2: Declare the variables and structure for sockets

Step 3: And then get the port number

Step 4: Create a socket using socket functions

Step 5: Use the connect command for socket connection

Step 6: Use bind option to bind the socket address

Step 7: Use accept command to receive the connection from the client

Step 8: Receive command from the client

Step 9: Send the file to the client socket

Step 10: Close the connection

Page 21: Cs 9216 Networking Lab Cor c

PROGRAM:

SERVER:

#include<stdio.h>#include<sys/types.h>#include<netinet/in.h>#include<string.h>#include<sys/socket.h>int main(){int sd,nsd,i,port=1234;char content[100]="\0",fname[100]="\0";struct sockaddr_in ser,cli;FILE *fp;if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1){printf("ERROR::SOCKET CREATION PROBLEM--CHECK THE PARAMETERS.\n");return 0;}bzero((char *)&ser,sizeof(ser));printf("THE PORT ADDRESS IS: %d\n",port);ser.sin_family=AF_INET;ser.sin_port=htons(port);ser.sin_addr.s_addr=htonl(INADDR_ANY);if(bind(sd,(struct sockaddr *)&ser,sizeof(ser))==-1){printf("\nERROR::BINDING PROBLEM, PORT BUSY--PLEASE CSS IN THE SER AND CLI\n");return 0;}i=sizeof(cli);listen(sd,1);printf("\nSERVER MODULE\n");printf("********************\n");nsd=accept(sd,(struct sockaddr *)&cli,&i);if(nsd==-1){printf("\nERROR::CLIENT ACCEPTIN PROBLEM--CHECK THE DEIPTOR PARAMETER.\n\n");return 0;}printf("\nCLIENT ACCEPTED");i=recv(nsd,fname,30,0);fname[i]='\0';fp=fopen(fname,"rb");while(1)

Page 22: Cs 9216 Networking Lab Cor c

{i=fread(&content,1,30,fp);content[i]='\0';send(nsd,content,30,0);strcpy(content,"\0");if(i<30)break;}send(nsd,"EOF",4,0);printf("\nFILE TRANSFERED TO DESTINATION\n\n");fclose(fp);close(sd);close(nsd);return 0;}

CLIENT:

#include<stdio.h>#include<sys/types.h>#include<netinet/in.h>#include<string.h>#include<sys/socket.h>int main(){int sd,i,port=1234;char content[100]="\0",fname[100]="\0",file[100]="\0";struct sockaddr_in ser;FILE *fp;if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1){

printf("\nERROR::SOCKET CREATION PROBLEM--CHECK THE PARAMETER.\n\n");

return 0;}bzero((char *)&ser,sizeof(ser));printf("\nTHE PORT ADDRESS IS: %d\n",port);ser.sin_family=AF_INET;ser.sin_port=htons(port);ser.sin_addr.s_addr=htonl(INADDR_ANY);if(connect(sd,(struct sockaddr *)&ser,sizeof(ser))==-1){printf("\nERROR::CANT CONNECT TO SERVER--CHECK PARAMETERS.\n\n");return 0;}printf("\nTHIS IS THE CLIENT MODULE. THIS MODULE CAN ASK THE SERVER A FILE");

Page 23: Cs 9216 Networking Lab Cor c

printf("\n*****************************************************************\n\n");printf("\nENTER THE PATHNAME OF SOURCE FILE::\n");scanf("%s",fname);printf("\nENTER THE PATHNAME OF DESTINATION FILE::\n");scanf("%s",file);send(sd,fname,30,0);fp=fopen(file,"wb");while(1){i=recv(sd,content,30,0);content[i]='\0';if(!strcmp(content,"EOF"))break;//fwrite(&content,strlen(content),1,fp);printf("%s",content);strcpy(content,"\0");}printf("\n\nFILE RECEIVED\n\n");fclose(fp);close(sd);return 0;}

Page 24: Cs 9216 Networking Lab Cor c

OUTPUT:

SERVER:

[3itb41@TELNET ~]$ cd ftpser.c[3itb41@TELNET serv]$ ./a.outTHE PORT ADDRESS IS: 1234

SERVER MODULE********************CLIENT ACCEPTEDFILE TRANSFERED TO DESTINATION

CLIENT:

[3itb41@TELNET cli]$ cc ftpcli.c [3itb41@TELNET cli]$ ./a.out

THE PORT ADDRESS IS: 1234

THIS IS THE CLIENT MODULE. THIS MODULE CAN ASK THE SERVER A FILE*****************************************************************

ENTER THE PATHNAME OF SOURCE FILE::/home/3itb41/file1.html

ENTER THE PATHNAME OF DESTINATION FILE::fp1.html<HTML><BODY>Hi</BODY></HTML>

FILE RECEIVED

Page 25: Cs 9216 Networking Lab Cor c

RESULT:

Thus the c program for transferring file from one machine to another machine using TCP is executed and the output is verified successfully.

Page 26: Cs 9216 Networking Lab Cor c

Ex.No.Date:

AIM:To write a C program for sliding window protocol.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: To create the socket using socket() functon.

Step 4: Enter the number of frames.

Step 5: And the corresponding message is send to receiver.

Step 6: Acknowledgement is received by receiver.

Step 7: If u send another message,ACK 2 message is received.

Step 8: Send the acknnowledge to sender.

Step 9: Print out with the necessary details.

Step 10: Stop the program.

SOURCE CODE

Page 27: Cs 9216 Networking Lab Cor c

#include<stdio.h>#include<conio.h>void main(){int i,sq,ws,buf=20,ls=0,rcv[20],count,ack,ch;clrscr();do{count=0;printf("\n ENTER WINDOW SIZE: ");scanf("%d",&ws);if(buf>0){for(sq=ls;count<ws;sq++){rcv[sq]=sq;printf("\n %d",rcv[sq]);count++;buf--;ls++;}}else{printf("\n BUFFER IS FULL !");break;}ack=ls;printf("\n ACKNO =%d",ack);printf("\n ENTER 1 TO QUIT OR ENTER ANY NO:");scanf("%d",&ch);}while(ch!=1);}

OUTPUT:

ENTER WINDOW SIZE: 5

Page 28: Cs 9216 Networking Lab Cor c

0 1 2 3 4 ACKNO =5 ENTER 1 TO QUIT OR ENTER ANY NO:2 ENTER WINDOW SIZE: 10 5 6 7 8 9 10 11 12 13 14 ACKNO =15 ENTER 1 TO QUIT OR ENTER ANY NO:2 ENTER WINDOW SIZE: 5 15 16 17 18 19 ACKNO =15 ENTER 1 TO QUIT OR ENTER ANY NO:2 ENTER WINDOW SIZE: 2 BUFFER IS FULL !

RESULT:Thus the sliding window protocol was implemented by means of C program.

Page 29: Cs 9216 Networking Lab Cor c

AIM:To write a program to find a shortest path for all vertices using OSPF.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Enter the number of vertices.

Step 4: Get the distance table as input.

Step 5: Calculate the cost of direct and indirect path from source to destination.

Step 6: Compare the cost of all path.

Step 7: Display the minimum cost of all path.

Step 8: Print out with the necessary details.

Step 9: Stop the program.

Ex.No:5 SIMULATION OF ROUTING PROTOCOLS

Date:

Page 30: Cs 9216 Networking Lab Cor c

SOURCE CODE:

#include<stdio.h>#include<string.h>void main(){int count,src_router,i,j,k,w,v,min;int cost_matrix[100][100],dist[100],last[100];int flag[100];printf("\nENTER THE NO OF ROUTERS:");scanf("%d",&count);printf("\nENTER THE COST MATRIX VALUES:");for(i=0;i<count;i++){for(j=0;j<count;j++){printf("\n%d->%d:",i,j);scanf("%d",&cost_matrix[i][j]);if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;}}printf("\nENTER THE SOURCE ROUTER:");scanf("%d",&src_router);for(v=0;v<count;v++){flag[v]=0;last[v]=src_router;dist[v]=cost_matrix[src_router][v];}flag[src_router]=1;

for(i=0;i<count;i++){min=1000;for(w=0;w<count;w++){if(!flag[w])if(dist[w]<min){v=w;min=dist[w];}}flag[v]=1;

Page 31: Cs 9216 Networking Lab Cor c

for(w=0;w<count;w++){if(!flag[w])if((min+cost_matrix[v][w])<dist[w]){dist[w]=min+cost_matrix[v][w];last[w]=v;}}}for(i=0;i<count;i++){printf("\n%d==>%d:PATH TAKEN:%d",src_router,i,i);w=i;while(w!=src_router){printf("\n<--%d",last[w]);w=last[w];}printf("\nSHORTEST PATH COST:%d",dist[i]);}getch();}

Page 32: Cs 9216 Networking Lab Cor c

OUTPUT:

ENTER THE NO OF ROUTERS:3

ENTER THE COST MATRIX VALUES:0->0:100

0->1:1

0->2:1

1->0:3

1->1:100

1->2:1

2->0:1

2->1:1

2->2:100

ENTER THE SOURCE ROUTER:1

1==>0:PATH TAKEN:0<--2<--1SHORTEST PATH COST:21==>1:PATH TAKEN:1SHORTEST PATH COST:1001==>2:PATH TAKEN:2Jm <--1SHORTEST PATH COST:1

Page 33: Cs 9216 Networking Lab Cor c

RESULT:

Thus the program for simulating shortest path routing algorithm is executed and the output is verified successfully.

Page 34: Cs 9216 Networking Lab Cor c

AIM:To write a program to convert logical address into physical address.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: First to find the logical address of that system.

Step 4: Address Resolution Protocol (ARP) is the method for finding a host’s hardware address when only its IP address is known.

Step 5:After getting logical address,we can use ARP function to convert physical address.

Step 8: Print out with the necessary details.

Step 9: Stop the program.

Ex.No:6 CONVERT LOGICAL ADDRESS INTO PHYSICAL ADDRESS

Date:

Page 35: Cs 9216 Networking Lab Cor c

SOURCE CODE:

SERVER:

#include <stdio.h>#include <sys/types.h>#include <sys/shm.h> //ARP table in shared mem //it can be shared by n no of clientsint main(){ int shmid; int a,i; char *ptr,*shmptr; shmid=shmget(1200,10,IPC_CREAT|0666); shmptr=shmat(shmid,NULL,0); //attaching a ptr to shm area ptr=shmptr; for(i=0;i<3;i++) { puts("\n Enter the MAC:"); scanf("%s",ptr); a=strlen(ptr); ptr[a]=' '; puts("\n Enter the IP:"); ptr=ptr+a+1; scanf("%s",ptr); a=strlen(ptr); ptr[a]='\n'; ptr=ptr+a+1; } ptr[strlen(ptr)]='\0'; printf("\n THE ARP TABLE AT SERVER SIDE=\n %s",shmptr); shmdt(shmptr); //detach return 0;

}

Page 36: Cs 9216 Networking Lab Cor c

CLIENT:

#include <stdio.h>#include <sys/types.h>#include <sys/shm.h>int main(){

int shmid; int a; char *ptr,*shmptr; char ip[20],mac[20],ptr2[20]; shmid=shmget(1200,10,0666); shmptr=shmat(shmid,NULL,0); puts("THE ARP TABLE IS:"); printf("%s",shmptr); printf("\n 1.ARP 2.RARP 3.EXIT\n"); scanf("%d",&a); switch(a) { case 1: puts("\n Enter the IP address:"); scanf("%s",ip); ptr=strstr(shmptr,ip);//finds substring ip in string shmptr //and assign loc to ptr ptr-=8; // for mac addr sscanf(ptr,"%s %*s",ptr2);// %*s denotes neglect that string ie ip printf("\n MAC address:%s",ptr2); break;

case 2: puts("\n Enter the MAC address:"); scanf("%s",mac); ptr=strstr(shmptr,mac); sscanf(ptr,"%*s %s",ptr2);//sscanf for scanning shared mem printf("%s",ptr2); break; case 3: exit(1);

}}

Page 37: Cs 9216 Networking Lab Cor c

OUTPUT:

SERVER:

[05mecse090@networkserver ~]$ cc arpserver.c[05mecse090@networkserver ~]$./a.out

Enter the MAC:d.f.g.h

Enter the IP:11.56.23.84

Enter the MAC:r.t.y.u

Enter the IP:96.23.52.63 THE ARP TABLE AT SERVER SIDE= d.f.g.h 11.56.23.84r.t.y.u 96.23.52.63

CLIENT:

[05mecse090@networkserver ~]$cc arpclient.c[05mecse090@networkserver ~]$./a.out

THE ARP TABLE IS:d.f.g.h 11.56.23.84r.t.y.u 96.23.52.63w.e.t.y 56.32.85.12

1.ARP 2.RARP 3.EXIT1

Enter the IP address:11.56.23.84

MAC address:d.f.g.h

Page 38: Cs 9216 Networking Lab Cor c

[05mecse090@networkserver ~]$cc arpclient.c[[05mecse090@networkserver ~]$./a.outTHE ARP TABLE IS:d.f.g.h 11.56.23.84r.t.y.u 96.23.52.63 1.ARP 2.RARP 3.EXIT2 Enter the MAC address:r.t.y.u96.23.52.63

Page 39: Cs 9216 Networking Lab Cor c

RESULT:

Thus, the logical address is converted into physical address using Address Resolution Protocol.

Page 40: Cs 9216 Networking Lab Cor c

Introduction:

NS is a discrete event simulator for networking research. It provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks.

ns is an object oriented simulator, written in C++, with an OTcl interpreter as a frontend. The simulator supports a class hierarchy in C++ (also called the compiled hierarchy in this document), and a similar class hierarchy within the OTcl interpreter (also called the interpreted hierarchy in this document). The two hierarchies are closely related to each other; from the user’s perspective, there is a one-to-one correspondence between a class in the interpreted hierarchy and one in the compiled hierarchy.

ns uses two languages because simulator has two different kinds of things it needs to do. On one hand, detailed simulations of protocols requires a systems programming language which can efficiently manipulate bytes, packet headers, and implement algorithms that run over large data sets. For these tasks run-time speed is important and turn-around time (run simulation, find bug, fix bug, recompile, re-run) is less important. On the other hand, a large part of network research involves slightly varying parameters or configurations, or quickly exploring a number of scenarios. In these cases, iteration time (change the model and re-run) is more important. Since configuration runs once (at the beginning of the simulation), run-time of this part of the task is less important. ns meets both of these needs with two languages, C++ and OTcl. C++ is fast to run but slower to change, making it suitable for detailed protocol implementation. OTcl runs much slower but can be changed very quickly (and interactively), making it ideal for simulation configuration. ns (via tclcl) provides glue to make objects and variables appear on both langauges.

Nam is a Tcl/TK based animation tool for viewing network simulation traces and real world packet traces. It supports topology layout, packet level animation, and various data inspection tools.Nam began at LBL. It has evolved substantially over the past few years. The nam development effort was an ongoing collaboration with the VINT project.

How to start:

First of all, you need to create a simulator object. This is done with the commandNow we open a file for writing that is going to be used for the nam trace data.set nf [open out.nam w]$ns namtrace-all $nfThe first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In thesecond line we tell the simulator object that we created above to write all simulation

Page 41: Cs 9216 Networking Lab Cor c

data that is going to be relevant for nam into this file.The next step is to add a 'finish' procedure that closes the trace file and starts nam.proc finish {} {global ns nf$ns flush-traceclose $nfexec nam out.nam &exit 0}

The next line tells the simulator object to execute the 'finish' procedure after 5.0 seconds of simulation time.$ns at 5.0 "finish"

ns provides you with a very simple way to schedule events with the 'at' command. The last line finally starts the simulation.

$ns run

You can actually save the file now and try to run it with 'ns example1.tcl'. You are going to get an error message like 'nam: empty trace file out.nam' though, because until now we haven't defined any objects (nodes, links, etc.) or events. You will have to use the code from this section as starting point in the other sections. #Create a simulator objectset ns [new Simulator]

#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf

#Define a 'finish' procedureproc finish {} {

global ns nf$ns flush-trace#Close the trace fileclose $nf#Execute nam on the trace fileexec nam out.nam &exit 0

}

# Insert your own code for topology creation# and agent definitions, etc. here

#Call the finish procedure after 5 seconds simulation time$ns at 5.0 "finish"

#Run the simulation$ns run

Page 42: Cs 9216 Networking Lab Cor c

set n0 [$ns node]set n1 [$ns node]

A new node object is created with the command '$ns node'. The above code creates two nodes and assigns them to the handles 'n0' and 'n1'. The next line connects the two nodes.

$ns duplex-link $n0 $n1 1Mb 10ms DropTailThis line tells the simulator object to connect the nodes n0 and n1 with a duplex link

with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue. Now you can save your file and start the script with 'ns example1.tcl'. nam will be started automatically and you should see an output that resembles the picture below.

Commands to create a link between nodes:

$ns_ simplex-link <node1> <node2> <bw> <delay> <qtype> <args>This command creates an unidirectional link between node1 and node2 with

specified bandwidth (BW) and delay characteristics. The link uses a queue type of <qtype> and depending on the queue type different arguments are passed through <args>.$ns_ duplex-link <node1> <node2> <bw> <delay> <qtype> <args>

This creates a bi-directional link between node1 and node2. This procedure essentially creates a duplex-link from two simplex links, one from node1 to node2 and the other from node2 to node1. The syntax for duplex-link is same as that of simplex-link described above. $ns_ simplex-link-op <n1> <n2> <op> <args>

This is used to set attributes for a simplex link. The attributes may be the orientation, color, label, or queue-position.$ns_ duplex-link-op <n1> <n2> <op> <args>

This command is used to set link attributes (like orientation of the links, color, label, or queue-position) for duplex links.

Sending data:

Page 43: Cs 9216 Networking Lab Cor c

The next step is to send some data from node n0 to node n1. In ns, data is always being sent from one 'agent' to another. So the next step is to create an agent object that sends data from node n0, and another agent object that receives the data on node n1.#Create a UDP agent and attach it to node n0set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0

These lines create a UDP agent and attach it to the node n0, then attach a CBR traffic generator to the UDP agent. CBR stands for 'constant bit rate'. Line 7 and 8 should be self-explaining. The packet Size is being set to 500 bytes and a packet will be sent every 0.005 seconds (i.e. 200 packets per second).The next lines create a Null agent which acts as traffic sink and attach it to node n1.

set null0 [new Agent/Null]$ns attach-agent $n1 $null0

Now the two agents have to be connected with each other.$ns connect $udp0 $null0

And now we have to tell the CBR agent when to send data and when to stop sending. Note: It's probably best to put the following lines just before the line '$ns at 5.0 "finish"'.

$ns at 0.5 "$cbr0 start"$ns at 4.5 "$cbr0 stop"

This code should be self-explaining again. Now you can save the file and start the simulation again. When you click on the 'play' button in the nam window, you will see that after 0.5 simulation seconds,node 0 starts sending data packets to node 1. You might want to slow nam down then with the 'Step' slider.

Page 44: Cs 9216 Networking Lab Cor c

Now you start some experiments with nam and the Tcl script. You can click on any packet in the nam window to monitor it, and you can also click directly on the link to get some graphs with statistics.Try to change the 'packetsize_' and 'interval_' parameters in the Tcl script to see what happens.

Agents:Agents represent endpoints where network-layer packets are constructed or

consumed, and are used in the implementation of protocols at various layers.

ns_ attach-agent <node> <agent>

This command attaches the <agent> to the <node>. We assume here that the <agent> has already been created. An agent is typically created by set agent [new Agent/AgentType] where Agent/AgentType defines the class definiton of the specified agent type.

The topology:

You will always have to create a simulator object, you will always have to start the simulation with the same command, and if you want to run nam automatically, you will always have to open a trace file, initialize it, and define a procedure which closes it and starts nam.

Now insert the following lines into the code to create four nodes.set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]The following piece of Tcl code creates three duplex links between the nodes.

$ns duplex-link $n0 $n2 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n2 1Mb 10ms DropTail

You can save and start the script now. You might notice that the topology looks a bit awkward in nam. You can hit the 're-layout' button to make it look better, but it would be nice to have some more control over the layout. Add the next three lines to your Tcl script and start it again.

$ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op $n1 $n2 orient right-up$ns duplex-link-op $n2 $n3 orient right

Page 45: Cs 9216 Networking Lab Cor c

#Create a simulator objectset ns [new Simulator]

#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf

Page 46: Cs 9216 Networking Lab Cor c

RESULT:

The Network Simulator Packages was studied and verified.

Page 47: Cs 9216 Networking Lab Cor c

Recommended