P. 1
Network Document

Network Document

|Views: 5.625|Likes:
Publicado porKishore Prabhu

More info:

Published by: Kishore Prabhu on Aug 31, 2011
Direitos Autorais:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/31/2013

pdf

text

original

Sections

  • TCP CHAT
  • AIM:
  • ALGORITHM:
  • Server:
  • Filename: Chat_server.c Server program Code:
  • Filename: Chat_client.c Client program Code:
  • Result:
  • Output:
  • TCP DATE AND TIME
  • Filename: Date_server.c Server program Code:
  • Filename: Date_client.c Client program Code:
  • ECHO SERVER
  • Filename: echoserver.c Server program Code:
  • Filename: echoclient.c Client program Code:
  • DOMAIN NAME SYSTEM
  • Client:
  • Filename: DNS_server.c Server program Code:
  • Filename: DNS_client.c Client program Code:
  • UDP CHAT
  • Aim:
  • Algorithm:
  • UDP Server:
  • UDP Client:
  • Filename: UDP_server.c Server program Code:
  • Filename: UDP_client.c Client program Code:
  • SLIDING WINDOW – GO BACK N
  • Filename: GN_server.c Server program Code:
  • Filename: GN_client.c Client program Code:
  • SLIDING WINDOW – SELECTIVE REPEAT
  • Filename: SR_server.c Server program Code:
  • Filename: SR_client.c Client program Code:
  • REMOTE PROCEDURE CALL
  • Filename: RPC_server.c Server program Code:
  • Filename: RPC_client.c Client program Code:
  • Address Resolution Protocol
  • Algorithm: Server:
  • Filename: ARP_server.c Server program Code:
  • Filename: ARP_client.c Client program Code:
  • Reverse Address Resolution Protocol
  • Filename: RARP_server.c Server program Code:
  • Filename: RARP_client.c Client program Code:
  • Open Shortest Path First Routing Protocol
  • Filename: OSPF.c Program Code:
  • Study of TCP Performance using Network Simulator
  • Procedure:
  • Filename: TCP.tcl Program Code:
  • Study of UDP Performance using Network Simulator
  • Filename: UDP.tcl Program code:

Ex No: 01 TCP CHAT

Date:

AIM: To write a program in C to implement TCP Chat.

ALGORITHM: Server: 1. Include necessary header files to support functions for socket definition, socket types, internet addresses, I/O functions, UNIX system calls. 2. Declare variables for socket ID, port number, socket addresses, character buffer, etc. 3. Create socket for server. 4. Bind socket with addresses. 5. Specify number of allowed connection. 6. Wait for connection. 7. Accept connection (If any). 8. Read the data from the client and continue till the string “bye” is entered. 9. Close the connected socket. Client: 1. Include necessary header files to support functions for socket definition, socket types, internet addresses, I/O functions, UNIX system calls. 2. Declare variables for socket ID, port number, socket addresses, character buffer, etc. 3. Create socket for client. 4. Connect client socket to the server socket addresses. 5. Send the data to the server and wait for the server to reply til the user enters “bye”. 6. Close the connected socket.

TCP CHAT Filename: Chat_server.c Server program Code:
//Program for TCP chat. #include<stdio.h> #include<netinet/in.h> #include<sys/socket.h> #include<time.h> #include<unistd.h> #include<string.h> int main() { int sd,sd2,nsd,clilen,sport,len; char sendmsg[20],recmsg[20]; struct sockaddr_in cliadd,servaddr; printf("\nEnter the port address:"); scanf("%d",&sport); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) printf("Error:Socket creation failed\n"); else printf("Socket is created successfully\n"); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(sport); sd2=bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr)); if(sd2<0) printf("Error in binding\n"); else printf("Binding successful\n"); listen(sd,5); clilen=sizeof(cliadd); nsd=accept(sd,(struct sockaddr *)NULL,NULL); if(nsd<0) printf("Error: Cannot accept\n"); else printf("Accept successful\n"); do { recv(nsd,recmsg,20,0); printf("%s",recmsg); fgets(sendmsg,20,stdin); len=strlen(sendmsg); sendmsg[len-1]='\0'; send(nsd,sendmsg,20,0); }while(strcmp(sendmsg,"bye")!=0); return 0; }

Filename: Chat_client.c Client program Code:
//Program for TCP chat. #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main() { int csd,cport,len; char senmsg[20],recmsg[20]; struct sockaddr_in servaddr; printf("\nEnter the port addrerss:"); scanf("%d",&cport); csd=socket(AF_INET,SOCK_STREAM,0); if(csd<0) printf("Error: Socket creation failed\n"); else printf("Socket is created successfully\n"); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(cport); if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) printf("Cannot connect\n"); else printf("Connected\n"); do { fgets(senmsg,20,stdin); len=strlen(senmsg); senmsg[len-1]='\0'; send(csd,senmsg,len,0); recv(csd,recmsg,20,0); printf("\n%s",recmsg); }while(strcmp(recmsg,"bye")!=0); return 0; }

Result: Thus the program to implement TCP chat client server was written successfully and the output was verified.

/a.c [network@server Network]$ .Output: Server Side: [network@server Network]$ cc Chat_server.c [network@server Network]$ .out Enter the port address: 2134 Socket is created successfully Connected Hi .out Enter the port address: 2134 Socket is created successfully Bind successful Accept successful Hi bye Client Side: [network@server Network]$ cc Chat_client./a.

internet addresses. character buffer. internet addresses. Create socket for client. Include necessary header files to support functions for socket definition. 5. 5. I/O functions. Specify number of allowed connection. 3. 2. Connect client socket to the server socket addresses. 6. ALGORITHM: Server: 1. I/O functions. Read the information and print it. Retrieve time information and send it to the client through the connected socket. Create socket for server. 7. Wait for connection. 8. port number. Close the connected socket. UNIX system calls. etc. UNIX system calls. Declare variables for socket ID. port number. Declare variables for socket ID. etc. 6. socket types. character buffer. 4. 7. socket addresses. Bind socket with addresses.Ex No: 02 TCP DATE AND TIME Date: AIM: To write a program in C to implement TCP Daytime Client Server. socket addresses. 4. Client: 1. 2. Close the connected socket. . socket types. 3. Accept connection (If any). Include necessary header files to support functions for socket definition. Give request to obtain time information. 9.

if(bind(listenfd. #include<string. char buff[128+1]. port=(argc>1)?atoi(argv[1]):5500. exit(1). } bzero((char *)&servaddr.c Server program Code: //Program for TCP date-time.h> #include<unistd. exit(1).connfd. servaddr. exit(1).sizeof(servaddr)).sin_family=AF_INET.h> #include<sys/types.7)<0) { perror("Error in listen")..h> #include<time.NULL).sizeof(servaddr))) { perror("Error in binding").0). Server code. exit(1).(struct sockaddr *)NULL.h> int main(int argc. } . } for(. struct sockaddr_in servaddr.h> #include<stdio.h> #include<sys/time. servaddr. time_t ticks.sin_addr.sin_port=htons(port). listenfd=socket(AF_INET. } if(listen(listenfd. if(listenfd<0) { perror("In listen()"). if(connfd<0) { perror("Error while connecting").h> #include<sys/socket.TCP DATE AND TIME Filename: Date_server.(struct sockaddr *)&servaddr.h> #include<netinet/in.s_addr=htonl(INADDR_ANY).char *argv[]) { int listenfd. unsigned short port.) { connfd=accept(listenfd.h> #include<stdlib. servaddr.SOCK_STREAM.

buff.24s\r\n"."%. write(connfd.ticks=time(NULL).ctime(&ticks)). } } . exit(0). close(connfd).strlen(buff)). sprintf(buff.

exit(1).h> #include<time.sizeof(servaddr))<0) { perror("Error while connecting").char *argv[]) { int sockfd. char *hostname. Client code. port=(argc>2)?atoi(argv[2]):5500.s_addr<=0) { perror("Bad address after gethostbyname()"). hostname=(argc>1)?argv[1]:"localhost".h> #include<stdio.h> #include<unistd.0))<0) { perror("Error while creation of socket").sin_addr. if((hp=gethostbyname(hostname))==0) { perror("Error in getting host in gethostbyname()"). servaddr.c Client program Code: //Program for TCP date-time.h> #include<sys/types.SOCK_STREAM.h> #include<stdlib. servaddr.h> #include<sys/time.h> #include<netdb.Filename: Date_client. #include<string.h> int main(int argc.sin_family=AF_INET. memcpy(&servaddr. exit(0). } if(connect(sockfd.hp->h_addr.sin_addr. struct sockaddr_in servaddr.h> #include<netinet/in. struct hostent *hp. exit(1). unsigned short port.(struct sockaddr *)&servaddr. } printf("Today the daytime and date is:").sin_port=htons(port).sizeof(servaddr)). } bzero((char *)&servaddr.n.h> #include<sys/socket.hp->h_length).h> #include<arpa/inet. } if((sockfd=socket(AF_INET. . char recvline[128]. if(servaddr. exit(1).

while((n=read(sockfd. if(fputs(recvline. } exit(0).recvline.sizeof(recvline)-1))>0) { recvline[n]=0. exit(1). } } if(n<0) { perror("Error while read"). exit(1).stdout)==EOF) { perror("Error in fputs()"). } Result: Thus the program to implement TCP daytime server client was written successfully and the output was verified. .

/a.Output: Server Side: [network@serverNetwork]$ cc Date_server./a.out Today the day time and date is: Mon Jul 18 09:48:09 2010 .c [network@serverNetwork]$ .c [network@serverNetwork]$ .out Client Side: [network@serverNetwork]$ cc Date_client.

ALGORITHM: Server: 1. I/O functions. socket addresses. Accept connection (If any). 2. etc. Connect client socket to the server socket addresses. 8. Close the connected socket. 4. internet addresses. 3. UNIX system calls. 7. etc. Close the connected socket. 9. port number. Create socket for server. 5. UNIX system calls. 2. Wait for connection. character buffer. Print the response received from the server. character buffer. port number. Bind socket with addresses. 6. Client: 1. 6. 3. Include necessary header files to support functions for socket definition. Declare variables for socket ID. Send it to the server and read the response that comes back from the server.Ex No: 03 ECHO SERVER Date: AIM: To write a program in C to implement Echo server. 8. 7. . Read the message to be echoed from the user. socket addresses. Specify number of allowed connection. socket types. Declare variables for socket ID. 4. Echo the received information from the connected socket. socket types. internet addresses. I/O functions. Include necessary header files to support functions for socket definition. Create socket for client. 5.

NULL))<0) { fprintf(stderr.char *argv[]) { int list_s.h> #include<sys/types."\nECHOSERV: Error creating listening socket\n"). exit(EXIT_FAILURE).sin_port=htons(port).h> #define MAX_LINE 1000 int main(int argc.NULL.0))<0) { fprintf(stderr.sin_family=AF_INET.""). read(conn_s.(struct sockaddr*)&servaddr. } while(1) { if((conn_s=accept(list_s.5)<0) { fprintf(stderr. servaddr. } if(listen(list_s.h> #include<stdio.20).buffer. int port."\nECHOSERV: Error calling bind()\n"). exit(EXIT_FAILURE).SOCK_STREAM.sizeof(servaddr)).h> #include<stdlib. int conn_s.ECHO SERVER Filename: echoserver.sizeof(servaddr))<0) { fprintf(stderr.h> #include<string.s_addr=htonl(INADDR_ANY).sin_addr. if(bind(list_s. servaddr. } memset(&servaddr. exit(EXIT_FAILURE).h> #include<arpa/inet.c Server program Code: //Program to implement echo server. struct sockaddr_in servaddr.&port)."\nECHOSERV: Error in calling accept()\n"). . char buffer[MAX_LINE]. scanf("%d".0. if((list_s=socket(AF_INET. servaddr.h> #include<unistd."\nECHOSERV: Error calling listen()\n"). printf("\nEnter server port to listen:"). #include<sys/socket. exit(EXIT_FAILURE). } strcpy(buffer.

if(close(conn_s)<0) { fprintf(stderr.buffer).strlen(buffer)).buffer. exit(EXIT_SUCCESS). } } } . } else { printf("Connection closed\n"). write(conn_s. exit(EXIT_FAILURE)."\nECHOSERV: Error in calling close()\n").printf("\nMessage recived and echoed:%s\n".

SOCK_STREAM. char *endptr.&servaddr.buffer.c Client program Code: //Program to implement echoserver.0).h> #include<unistd.sizeof(servaddr))<0) { printf("\nECHOCLNT: Error in calling connect()\n").&endptr.(struct sockaddr*)&servaddr.h> #define MAX_LINE 1000 int main(int argc. fgets(buffer. szAddress=argv[1].sin_addr)<=0) { printf("\nECHOCLNT: Invalid remote IP address"). exit(EXIT_FAILURE).MAX_LINE.stdin).strlen(buffer)+1). exit(EXIT_FAILURE).Filename: echoclient.h> #include<stdlib.sizeof(servaddr)). } if((conn_s=socket(AF_INET. . servaddr.h> #include<arpa/inet. if(inet_aton(szAddress. } printf("\nEnter the string to echo:"). printf("\nRemote host address:%s\nRemote port no:%s".argv[2]). exit(EXIT_FAILURE). } if(connect(conn_s. port=strtol(szPort.h> #include<string.h> #include<stdio. write(conn_s. if(*endptr) { printf("\nECHOCLNT: Invalid port supplied\n"). } memset(&servaddr. char *szAddress. int port.argv[1]. char *szPort.sin_family=AF_INET.0))<0) { printf("\nECHOCLNT: Error in creating socket\n"). char buffer[MAX_LINE]. servaddr. szPort=argv[2]. struct sockaddr_in servaddr. #include<sys/types. exit(EXIT_FAILURE).char *argv[]) { int conn_s.sin_port=htons(port).0.

.strcpy(buffer.buffer. read(conn_s. } Result: Thus the program to implement echo server was written successfully and the output was verified. return EXIT_SUCCESS. printf("\nEcho response:%s\n"."").buffer).20).

/a.200 2134 Remote host address: 172.out 172.6.16.6.Output: Server Side: [network@server Network]$ cc echoserver.c [network@server Network]$ .16./a.c [network@server Network]$ .out Enter the port address: 2134 Client Side: [network@server Network]$ cc echoclient.200 Remote port address: 2134 Enter the string to be echoed: Check Echo response: Check Message received and echoed: Check Connection Closed .

socket types. 8. . Wait for connection. 6. ALGORITHM: Server: 1. Include necessary header files to support functions for socket definition. character buffer. 8. Close the connected socket. 7. Include necessary header files to support functions for socket definition. socket addresses. internet addresses. 2. Connect client socket to the server socket addresses. Declare variables for socket ID. Declare variables for socket ID. Print the received IP address. 3. socket addresses. UNIX system calls. UNIX system calls. port number. internet addresses. 4. character buffer. 5. etc. 5. Create socket for server. 4. 6. Read the domain name from the user. 3. 10.Ex No: 04 DOMAIN NAME SYSTEM Date: AIM: To write a program in C to implement Domain Name System. Bind socket with addresses. Close the connected socket. Read the domain name from the client.txt and send the corresponding IP address to the client. Specify number of allowed connection. socket types. Client: 1. 2. I/O functions. Search for the given domain name in dns_map. Accept connection (If any). 7. I/O functions. etc. Send the domain name and receive the IP address. Create socket for client. 9. port number.

NULL).0). servaddr.text). } .sizeof(servaddr)).s_addr=htonl(INADDR_ANY). printf("Sending IP address. text[20].sport. else printf("Connection successful\n"). servaddr. break.DOMAIN NAME SYSTEM Filename: DNS_server.SOCK_STREAM.(struct sockaddr*)&servaddr. char rec[20].nsd.rec. servaddr. if(nsd<0) printf("Cannot connect\n").rec). else printf("Bind success\n"). if(sd2<0) printf("Bind error\n")."%s".sin_family=AF_INET.0). send(nsd.sd2. while(!feof(fp1)) { if(strcmp(text. recv(nsd.0).sin_port=htons(sport). sd=socket(AF_INET.sin_addr.(struct sockaddr*)NULL. listen(sd.5). sd2=bind(sd. fscanf(fp1.h> int main() { int sd. FILE *fp1=fopen("dns_map.text). printf("\nEnter the port address:")."r")."%s".h> #include<stdio. if(sd<0) printf("Error in creating socket\n"). else printf("Socket created\n").text.&sport). nsd=accept(sd. printf("\n%s\n". scanf("%d".h> #include<string.txt".20. struct sockaddr_in servaddr.h> #include<netinet/in.c Server program Code: //Program to implement DNS.rec)==0) { fscanf(fp1.\n"). #include<sys/socket.20.h> #include<time.

fscanf(fp1.text). return 0."%s". } . } printf("\n").

h> int main() { int csd. else printf("Socket creation successful\n"). scanf("%d".SOCK_STREAM.txt localhost 172.16. struct sockaddr_in servaddr.0).sin_family=AF_INET.h> #include<string. return 0.(struct sockaddr*)&servaddr.sizeof(servaddr))<0) printf("Cannot connect\n").20. } Filename: dns_map.&cport).200 localhost1 172.rec[20]. servaddr. printf("\nThe IP address is:%s\n".sen.sin_addr.16. servaddr. else printf("Connected\n").h> #include<time.Filename: DNS_client. printf("Enter the domain name:").6. recv(csd. if(connect(csd. printf("\nEnter the port address:"). csd=socket(AF_INET.0). #include<sys/socket.h> #include<netinet/in.sin_port=htons(cport). servaddr. scanf("%s".rec).201 new 172.0). char sen[20].6.210 .20.sen). if(csd<0) printf("Error: Socket creation failed\n"). send(csd.16.rec.c Client program Code: //Program to implement DNS.h> #include<stdio.cport.s_addr=htonl(INADDR_ANY).6.

.Result: Thus the program to implement DNS was written successfully and the output was verified.

201 .16./a./a.c [network@server Network]$ .c [network@server Network]$ .6.Output: Server Side: [network@server Network]$ cc DNS_server.out Enter the port address: 2950 Socket is created Bind success Connection successful localhost1 Sending the IP address Client Side: [network@server Network]$ cc DNS_client.out Enter the port address: 2950 Socket is creation successfully Connected Enter the domain name: localhost1 The IP address is: 172.

socket addresses. socket types. Include necessary header files to support functions for socket definition. socket types. character buffer. UNIX system calls. Create socket for server. Echo retrieved information to socket 8. I/O functions. 4. 2. Include necessary header files to support functions for socket definition. socket addresses. Close the connected socket. Specify number of allowed connection. etc. port number. I/O functions. internet addresses. Bind socket with addresses. port number. 6. 3. UNIX system calls. etc. Declare variables for socket ID. 4. 5.Ex No: 05 UDP CHAT Date: Aim: To write a program in C to implement UDP client server chat. UDP Client: 1. Declare variables for socket ID. 7. Retrieve information from Socket. Connect client socket to the server socket addresses. 5. Algorithm: UDP Server: 1. . 3. character buffer. Create socket for client. internet addresses. 2. Close the connected socket.

(struct sockaddr*)&server.h> #include<sys/socket.sin_port)). sock=socket(AF_INET.buf. if(sock==-1) perror("Socket was not created. server.sin_addr.client_add_size. else perror("Socket created successfully\n").0.sizeof(server))<0) { perror("Error in binding server\n").sizeof(buf).\n").namelen. printf("%s\n". char buf[20].h> int main() { int sock. client_add_size=sizeof(client). } .SOCK_DGRAM. if(bind(sock.buf). #include<string. } namelen=sizeof(server). exit(0). } printf("The assigned port is: %d\n".(struct sockaddr*)&server. struct sockaddr_in client. exit(0). } printf("Data has been sent to the socket\n"). exit(0). server. if(getsockname(sock.(socklen_t*)&namelen)<0) { perror("Error in getsockname()\n").sin_port=0. return 0.UDP CHAT Filename: UDP_server.h> #include<netinet/in.s_addr=INADDR_ANY. printf("Waiting for a message to arrive\n").h> #include<stdlib.h> #include<arpa/inet.NULL)<0) { perror("Error in recvframe()\n").(struct sockaddr*)NULL.ntohs(server.c Server program Code: //Program to implement UDP chat.0).h> #include<stdio. if(recvfrom(sock.server.

h> int main(int argc.h> #include<stdio.\n"). char string[100].(struct sockaddr*)&server. server.sin_addr.sin_port=port.sizeof(server)). if(sock==-1) printf("Socket was not created.SOCK_DGRAM. struct sockaddr_in server. sendto(sock.string). sock=socket(AF_INET.0. } Result: Thus the program to implement UDP Chat server was written successfully and the output was verified.sin_family=AF_INET. else printf("Socket created successfully\n"). printf("Enter the data to be sent:").h> #include<stdlib.s_addr=inet_addr(argv[1]). printf("Sending data to the socket\n").h> #include<sys/socket. server.buf.char **argv) { int sock. printf("Creating datagram socket"). return 0. .(strlen(buf)+1).h> #include<arpa/inet.0).c Client program Code: //Program to implement UDP Chat. strcpy(buf. int port. printf("Data has been sent to the socket\n"). scanf("%s".buf[32].string). #include<string. server. port=htons(atoi(argv[2])).Filename: UDP_client.

How are you? Closing the socket connection Socket closed Client Side: [network@server Network]$ cc UDP_client.c [network@server Network]$ .6.16.c [network@server Network]$ ./a.out 172.out Creating datagram socket Socket created successfully Assigned port address: 32775 Waiting for a message to arrive Data has been sent to socket The message was: Hi.Output: Server Side: [network@server Network]$ cc UDP_server./a.200 32775 Creating datagram socket Socket created successfully Data: Hi. How are you? Sending data to socket Data has been sent to socket Closing socket connection Socket closed .

Bind socket with addresses. etc. Include necessary header files to support functions for socket definition. Receive the next frame from the server and continue the previous two steps till end. socket addresses. socket addresses. Check the received frame and report error if any. Close the connected socket. 2. character buffer. Wait for connection. Accept connection (If any). character buffer. Specify number of allowed connection. socket types. 9. . 4. I/O functions. 4. port number. Read a frame from the server. Connect client socket to the server socket addresses. ALGORITHM: Server: 1. 9. 11. 8. 6. 10. Create socket for client. UNIX system calls. 8. Include necessary header files to support functions for socket definition. socket types. 6. Declare variables for socket ID. Read the status of the transmitted frame. 7. Create socket for server. Read the message from the user and transmit it to the client.Ex No: 06(a) SLIDING WINDOW – GO BACK N Date: AIM: To write a C program to implement Sliding Window protocol of type go back n. in each case of error retransmit all the frames from the error frame till the last sent frame. internet addresses. UNIX system calls. Close the connected socket. port number. 2. 5. I/O functions. Declare variables for socket ID. Repeat the previous step until the message is error-free. etc. 7. 3. internet addresses. Client: 1. 5. 3. Repeat previous step until the whole message is received by the client.

j. "%d". scanf("%d". i+j). scanf("%s". 20). lfd. SIZE). listen(std.h> #include<sys/types. printf("Enter the port address"). ack[20]. saddr. str+i. printf("Enter the text:").h> #include<string. str).h> #include<arpa/inet. "%d". j++) { printf("%d". read(lfd. &sport). temp[20]. saddr. len = strlen(frame). frame[20]. temp).SLIDING WINDOW – GO BACK N Filename: GN_server. 0.s_addr = htonl(INADDR_ANY). (struct sockaddr *)&caddr. status. i = 0. &status). (struct sockaddr *)&saddr. sizeof(saddr)). if(std<0) perror("Error"). strcat(frame. sizeof(saddr)).c Server program Code: #include<stdio. lfd = accept(std. strncpy(frame. lfd = bind(std. ack. struct sockaddr_in saddr. sscanf(ack. if(status == -1) . i+j).sin_family = AF_INET. } write(lfd.h> #define SIZE 4 main() { int std. saddr.sin_port = htons(sport). if(lfd) perror("Bind Error"). 0). sport. 5). for(j=0. frame. char str[20].h> #include<netinet/in. len. SOCK_STREAM. sprintf(temp. 20). sizeof(frame)).h> #include<sys/socket. len = sizeof(&caddr).sin_addr. std = socket(AF_INET. i. &len). printf("\nTransmitting frames:"). j<len. caddr. while(i<strlen(str)) { memset(frame. bzero(&saddr.

j++) { sprintf(temp. sizeof(frame)). "%d". j<len. } . temp). "Exit".printf("\nTransmission successful"). sleep(2). sizeof("Exit")). printf("\nExitting!\n"). frame. close(lfd). j+status). strcat(frame. } i = i + SIZE. } printf("\n"). status). frame[j] = '\0'. close(std). printf("%d". len = strlen(frame). } write(lfd. j++. else { printf("Received error in: %d". for(j=0. } write(lfd.. printf("\nRetransmitting frames"). for(j=0. if((j+status)%4 == 0) break.) { frame[j] = str[j+status]. j+status).

scanf("%d". choice. printf("Enter the port address:"). write(std. char str[20]. sizeof(err)). len. std = socket(AF_INET. bzero(&saddr. "-1".. saddr. connect(std. 0). else { printf("Enter the sequence no of the frame where error has occured"). 20). scanf("%d".0. sizeof(saddr)). (struct sockaddr *)&saddr. caddr. &cport).) { read(std.sin_addr). err).h> #include<sys/types. str). cport.Filename: GN_client.0. "Exit") == 0) { printf("Exitting!\n"). printf("Received the transmitted frame: %s\n". if(choice == 0) write(std. &choice). inet_pton(AF_INET. 20). sizeof("-1")).NO". } printf("Received: %s\nError? 1 . } } close(std).h> #include<arpa/inet. } . for(. if(std<0) perror("Error"). lfd. saddr. str.sin_port = htons(cport).1".c Client program Code: #include<stdio.h> main() { int std. err. SOCK_STREAM. if(strcmp(str. &saddr.h> #include<netinet/in. read(std. scanf("%s". err[20]. str1[20].h> #include<string.h> #include<sys/socket.sin_family = AF_INET. "127. str. str). sizeof(saddr)). struct sockaddr_in saddr.S or 0 . break.

.Result: Thus the program to implement Sliding Window protocol of type Go back n was written successfully and the output was verified.

/a. 2-No): 0 Received: t8 Do you want to report an error(1-Yes.c [network@server Network]$ .Output: Server Side: [network@server Network]$ cc GN_server.out Enter the port address: 1250 Enter text: TrialText Transmitting frames: 0123 Received error at 0 Retransmitting frames: 1234 Transmitting frames: 4567 Transmission successful Transmitting frames: 8 Transmission successful Exiting Client Side: [network@server Network]$ cc GN_client.out Enter the port address: 1250 Received: Tria0123 Do you want to report an error(1-Yes./a. 2-No): 1 Enter the seq no of the frame: 0 Received the retransmitted frame: Tria0123 Received: lTex4567 Do you want to report an error(1-Yes. 2-No): 0 Exiting .c [network@server Network]$ .

Close the connected socket. internet addresses. etc. Include necessary header files to support functions for socket definition. Check the received frame and report error if any. 7. Read a frame from the server. I/O functions. socket addresses. 5. Declare variables for socket ID. 4. port number. internet addresses. Receive the next frame from the server and continue the previous two steps till end. Repeat previous step until the whole message is received by the client. Wait for connection. Connect client socket to the server socket addresses. Close the connected socket. Create socket for client. socket types. I/O functions. . 2. 6. 2. 9. Bind socket with addresses. ALGORITHM: Server: 1. UNIX system calls. character buffer. 4. 5. character buffer. Declare variables for socket ID. socket types. 8. socket addresses. UNIX system calls. Client: 1. Specify number of allowed connection. 3. 7. 6. in each case of error retransmit the particular frame. Read the status of the transmitted frame. 8. 9. Create socket for server. etc. 11. Read the message from the user and transmit it to the client. Include necessary header files to support functions for socket definition.Ex No: 06(b) SLIDING WINDOW – SELECTIVE REPEAT Date: AIM: To write a C program to implement Sliding Window protocol of type selective repeat. Accept connection (If any). 3. port number. 10. Repeat the previous step until the message is error-free.

for(j=0. lfd = accept(std. sscanf(ack. printf("\nTransmitting frames:"). sizeof(frame)). ack[20]. frame[20]. read(lfd. SOCK_STREAM.sin_addr. saddr. j<len. j++) { printf("%d". i+j). (struct sockaddr *)&saddr. i = 0. if(status == -1) . "%d". scanf("%d".h> #include<arpa/inet.h> #include<string. 0. } write(lfd. len = sizeof(&caddr).sin_port = htons(sport). 20). "%d". len. sizeof(saddr)). printf("Enter the text:"). 20). if(std<0) perror("Error"). if(lfd) perror("Bind Error"). str+i. lfd = bind(std. sprintf(temp. std = socket(AF_INET. sizeof(saddr)).s_addr = htonl(INADDR_ANY). status.h> #define SIZE 4 main() { int std. temp). strncpy(frame. sport. ack. temp[20]. char str[20]. caddr. struct sockaddr_in saddr.h> #include<sys/types. saddr. SIZE).SLIDING WINDOW – SELECTIVE REPEAT Filename: SR_server.c Server program Code: #include<stdio. i+j).h> #include<netinet/in. 5). bzero(&saddr. &len). scanf("%s". j. &status). 0). frame. str). len = strlen(frame). lfd. strcat(frame. saddr. i.sin_family = AF_INET. listen(std.h> #include<sys/socket. (struct sockaddr *)&caddr. printf("Enter the port address"). while(i<strlen(str)) { memset(frame. &sport).

sizeof(frame)). frame[1] = '\0'. frame. sleep(2). printf("\nRetransmitting frame"). } . } i = i + SIZE. frame[0] = str[status]. status).printf("\nTransmission successful"). sizeof("Exit")). close(std). write(lfd. printf("\nExitting!\n"). close(lfd). "Exit". else { printf("Received error in: %d". } write(lfd.

20). "127. err.sin_port = htons(cport). scanf("%d". 0).. } } close(std).Filename: SR_client. scanf("%d". str). choice. char str[20]. SOCK_STREAM. str1[20]. err).c Client program Code: #include<stdio. scanf("%s".sin_family = AF_INET. bzero(&saddr. if(choice == 0) write(std. else { printf("Enter the sequence no of the frame where error has occured"). cport. len. &saddr.h> #include<netinet/in. (struct sockaddr *)&saddr. 20). if(std<0) perror("Error"). &cport). str). saddr. if(strcmp(str. lfd. for(. err[20].NO): ". std = socket(AF_INET.h> #include<arpa/inet. } printf("Received: %s\nError?(1 . } . break. sizeof("-1")). read(std. &choice). connect(std.0. "Exit") == 0) { printf("Exitting!\n"). caddr.) { read(std. inet_pton(AF_INET.h> #include<sys/types. "-1". printf("Enter the port address:").h> main() { int std.h> #include<string. str. struct sockaddr_in saddr.1". sizeof(saddr)). printf("Received the transmitted frame: %s\n". write(std. saddr.sin_addr).h> #include<sys/socket.0. sizeof(saddr)). str.YES or 0 . sizeof(err)).

Result: Thus the program to implement Sliding Window protocol of type selective repeat was written successfully and the output was verified. .

c [network@server Network]$ ./a.out Enter the port address: 1250 Received: Hell0123 Do you want to report an error(1-Yes. 2-No): 0 Exiting .out Enter the port address: 1250 Enter text: Hello Transmitting frames: 0123 Received error at 2 Retransmitting frame Transmitting frames: 4 Transmission successful Exiting Client Side: [network@server Network]$ cc SR_client./a.c [network@server Network]$ .Output: Server Side: [network@server Network]$ cc SR_server. 2-No): 1 Enter the seq no of the frame: 2 Received the retransmitted frame: l Received: o4 Do you want to report an error(1-Yes.

socket types. 6. 12. etc. UNIX system calls. 5. Read the data to be operated to be by the procedure. 4. 3. Declare variables for socket ID. Invoke the procedure with the parameters and store the return value in the buffer. 5. 7. Include necessary header files to support functions for socket definition. Close the connected socket. port number. Include necessary header files to support functions for socket definition. 6. port number. socket types. 2. Receive the output string and display it to the user. Connect client socket to the server socket addresses. etc. Close the connected socket. Specify number of allowed connection. internet addresses. 8. 9. 3. Read the string to be converted from the user. ALGORITHM: Server: 1. I/O functions. internet addresses. Send the value in the buffer to the client. . Create socket for server. 11. 4. Client: 1. UNIX system calls. Accept connection (If any). 2. Declare variables for socket ID. 7. 8. socket addresses. character buffer. socket addresses. I/O functions. Create socket for client.Ex No: 07 REMOTE PROCEDURE CALL Date: AIM: To write a C program to implement remote procedure call. character buffer. Define a procedure convert to toggle the case of characters supplied by the client. Wait for connection. 10. Send the parameter to the server. Bind socket with addresses.

h> #define MAX_LINE 1000 void convert(char v[]) { int i. (struct sockaddr *)&servaddr.s_addr = htonl(INADDR_ANY). int port.h> #include<unistd.h> #include<arpa/inet. conn_s. } . "ECHOSERV:Error in calling bind()\n").h> #include<ctype. v[i]!= '\0'. SOCK_STREAM. scanf("%d".h> #include<stdio. } memset(&servaddr. for(i=0. sizeof(servaddr))<0) { fprintf(stderr. &port). "ECHOSERV: Error in creating socket\n"). servaddr. } } int main(int argc.h> #include<stdlib. exit(EXIT_FAILURE). struct sockaddr_in servaddr.h> #include<string.c Server program Code: //Program to implement Remote Procedure Call #include<sys/socket. sizeof(servaddr)). exit(EXIT_FAILURE). servaddr. list_s = socket(AF_INET. i++) { if(islower(v[i])) v[i]=toupper(v[i]). char buffer[MAX_LINE]. if(list_s<0) { fprintf(stderr. 0. else v[i]=tolower(v[i]). 0).REMOTE PROCEDURE CALL Filename: RPC_server. if(bind(list_s.h> #include<sys/types.sin_addr.sin_family = AF_INET.sin_port = htons(port). servaddr. printf("\nEnter the server port:"). char *argv[]) { int list_s.

8)<0) { fprintf(stderr. } else exit(EXIT_SUCCESS). 20). "ECHOSERV: Error in listen\n"). exit(EXIT_FAILURE). exit(EXIT_FAILURE). buffer. NULL))<0) { fprintf(stderr. "ECHOSERV:Error in calling accept\n"). read(conn_s. write(conn_s. 20). } } . if(close(conn_s)<0) { fprintf(stderr. NULL. convert(buffer). } while(1) { if((conn_s = accept(list_s. " ").if(listen(list_s. buffer. exit(EXIT_FAILURE). } strcpy(buffer. "ECHOSERV: Error in close()\n").

sizeof(servaddr))<0) { printf("ECHOCLINT: Error in calling connect()\n"). } if((conn_s = socket(AF_INET.sin_port = htons(port). exit(EXIT_FAILURE). 20).h> #include<sys/socket. read(conn_s.h> #include<unistd. printf("\nRemote Host Address: %s\nRemote Port No: %s\n". exit(EXIT_FAILURE). } printf("Enter the string to tOGGLE case:"). char buffer[MAX_LINE]. } if(connect(conn_s. char *argv[]) { int conn_s. *szAddress. stdin). exit(EXIT_FAILURE). szAddress = argv[1]. 0). &servaddr. argv[1].h> #include<sys/types. strcpy(buffer. port = strtol(szPort. printf("The string with changed case is: %s\n".sin_family = AF_INET.sin_addr)<=0) { printf("ECHOCLINT: Invalid remote IP Address:").c Client program Code: //Program to implement Remote Procedure Call #include<stdio. " "). 0. MAX_LINE. servaddr. (struct sockaddr *)&servaddr. buffer.h> #include<string. szPort = argv[2]. *szPort. argv[2]). 0))<0) { printf("ECHOCLNT: Error creating listen"). short int port. servaddr. sizeof(servaddr)).h> #define MAX_LINE 1000 int main(int argc. strlen(buffer) + 1). buffer. struct sockaddr_in servaddr. buffer). .h> #include<stdlib. } memset(&servaddr.h> #include<arpa/inet. write(conn_s. &endptr. if(inet_aton(szAddress. exit(EXIT_FAILURE). if(*endptr) { printf("ECHOCLNT: Invalid port supplied\n").Filename: RPC_client. SOCK_STREAM . fgets(buffer. *endptr.

} Result: Thus the program to implement remote procedure call was written successfully and the output was verified.return EXIT_SUCCESS. .

200 Remote Port No: 3000 Enter the string to tOGGLE case: TrialTEXT The string with changed case is: tRIALtext Client Side: [network@server Network]$ cc RPC_client.16.Output: Server Side: [network@server Network]$ cc RPC_server.c [network@server Network]$ .6.out 172.6./a.200 3000 ./a.16.out Enter the server port: 3000 Remote Host Address: 172.c [network@server Network]$ .

5. 4. The key value is 7000. 7. If the IP address is not available then error message is displayed. then corresponding MAC address is written in the shared memory. Stop. After shared memory is created is created using shmget. Start 2. Start 2. Enter the IP address into the shared memory. If exists. 3. Stop. 3. After client is invoked. Print table that contains IP address and MAC address. The stored MAC address is printed by the client. Client: 1. 6. It is attached to memory at next available space using shmat() 4. 8. 5. Search is made on the table for IP address. Algorithm: Server: 1. use the shared memory to enter the key and IP address 6. The entered IP address is sent to the server and its corresponding MAC address is written to the shared memory by the server.Ex No: 08(a) Address Resolution Protocol Date: Aim: To write a program to implement Address Resolution Protocol to obtain MAC address. 7. Create a shared memory based on the key value. .

shmid1. shmid2 = shmget(key. "1:B1:C3:D2:49:5".1. scanf("%d%s". IPC_CREAT|0666). shmid2.1. } . sizeof(int).h> #include<string. NULL. key. while(1) { printf("Enter the key and the IP address"). NULL. *dest. } printf("Exiting!"). i. *flag = 0.h> #include<sys/shm. "1.i<4. 0).i+=2) printf("\n%s\t\t%s". ip). printf("IP\t\tMAC"). &key. "1. table[6][20] = {"1:A1:2E:3:4:5".1. 0666). i+=2) if(!strcmp(ip. 30. i<4. table[i-1]).1. int shmid.h> main() { char ip[10].h> #include<sys/ipc.c Server program Code: #include<stdio.3". if(i>4) dest = '\0'. table[i+1]). *ptr. for(i = 1. flag = shmat(shmid1. table[i])) break. table[i].Address Resolution Protocol Filename: ARP_server. for(i=0. 0).4"}. *flag. dest = shmat(shmid2. else strcpy(dest. shmid1 = shmget(6000.

h> #define key 7000 main() { int shmid.c Client program Code: #include<stdio. else printf("The MAC address is: %s". shmid1. NULL. } Result: Thus the program to implement Address Resolution Protocol to obtain MAC address was written successfully and the output was verified.Filename: ARP_client. while(*flag == 1). ip). shmid = shmget(6000. "%d%s". 0). 0666). local = shmat(shmid2. . char *bus. NULL. IPC_CREAT|0666).h> #include<sys/shm. IPC_CREAT|0666))<0) perror("Error!"). scanf("%s". NULL. printf("Enter the IP address:"). shmid1 = shmget(key. local).h> #include<sys/ipc. *flag = 1. *local. key. 30. shmid2. if(*local == '\0') printf("IP address is invalid"). 30. sprintf(bus. ip). *flag. 30. bus = shmat(shmid1. if((shmid2 = shmget(key. 0). 0). flag = shmat(shmid1. ip[20].

1.1.3 Enter the key and the IP address Client side: [network@server Network]$ cc ARP_client.1.4 1:B1:C3:D2:49:5 Enter the key and the IP address7000 1.1.1.1.out Enter the IP address:1./a.Output: Server side: [network@server Network]$ cc ARP_server.1.c [network@server Network]$ .c [network@server Network]$ .3 The MAC address is: 1:A1:2E:3:4:5 .1./a.out MAC IP 1.3 1:A1:2E:3:4:5 1.

use the shared memory to enter the key and MAC address 6. 3. Stop. Create a shared memory based on the key value. The key value is 7000.Ex No: 08(b) Reverse Address Resolution Protocol Aim: Date: To write a program to implement Reverse Address Resolution Protocol to obtain IP address. then corresponding IP address is written in the shared memory. 7. 3. After shared memory is created is created using shmget. 6. It is attached to memory at next available space using shmat() 4. After client is invoked. Stop. 8. Enter the MAC address into the shared memory. 4. Start 2. Search is made on the table for MAC address. Client: 1. 7. . Algorithm: Server: 1. If exists. The stored IP address is printed by the client. 5. Print table that contains IP address and MAC address. If the MAC address is not available then error message is displayed. The entered MAC address is sent to the server and its corresponding IP address is written to the shared memory by the server. Start 2. 5.

1.Reverse Address Resolution Protocol Filename: RARP_server.c Server program Code: //Program to implement RARP.3". key. shmid2 = shmget(key. 30. "1:A1:2E:3:4:5".h> #include<sys/shm. #include<stdio. printf("IP\t\tMAC"). i+=2) if(!strcmp(mac. "1. for(i=0. *ptr.1.h> main() { char mac[10]. while(1) { printf("Enter the key and the MAC address"). sizeof(int).4". &key. table[i-1]). table[i]. 0). NULL. shmid1. "1:B1:C3:D2:49:5"}. for(i = 1. *flag = 0.i+=2) printf("\n%s\t\t%s".1. *dest. else strcpy(dest. } . IPC_CREAT|0666). table[i])) break. int shmid. *flag. table[6][20] = {"1.h> #include<string.1. dest = shmat(shmid2. shmid1 = shmget(6000. flag = shmat(shmid1. 0666). 0). i.h> #include<sys/ipc. table[i+1]). NULL. scanf("%d%s". if(i>4) dest = '\0'. } printf("Exiting!"). shmid2.i<4. i<4. mac).

0). printf("Enter the MAC address:"). NULL. local = shmat(shmid2. *flag = 1. shmid1 = shmget(key. 30. bus = shmat(shmid1. *local. while(*flag == 1). 30. } Result: Thus the program to implement Address Resolution Protocol to obtain IP address was written successfully and the output was verified. IPC_CREAT|0666))<0) perror("Error!"). NULL. "%d%s".Filename: RARP_client. shmid2. local). scanf("%s". 0).h> #include<sys/shm. flag = shmat(shmid1. else printf("The IP address is: %s". NULL. if((shmid2 = shmget(key. mac).c Client program Code: //Program to implement RARP. char *bus.h> #include<sys/ipc. *flag. shmid1. 0). key. #include<stdio. if(*local == '\0') printf("MAC address is invalid"). .h> #define key 7000 main() { int shmid. mac). 0666). IPC_CREAT|0666). sprintf(bus. 30. mac[20]. shmid = shmget(6000.

out Enter the MAC address:1:A1:2E:3:4:5 The IP address is: 1.c [network@server Network]$ .1.Output: Server side: [network@server Network]$ cc RARP_server./a.1.3 .1.1.out IP MAC 1.1./a.1.3 1:A1:2E:3:4:5 1.c [network@server Network]$ .4 1:B1:C3:D2:49:5 Enter the key and the MAC address7000 1:A1:2E:3:4:5 Enter the key and the MAC address Client side: [network@server Network]$ cc RARP_client.

Choose the node that has not be included compare the distance to reach the node using the newly included node. Take the minimum value as the new distance. Read the cost matrix for the path from each node to another node. 7. 3. 9. 10. 2. Read the number of nodes. Compare D of a node which is the distance from source to that corresponding node. Exit when the user chooses -1 for source and destination. Repeat steps 6 to 8 for n-1 nodes.Ex No: 09 Open Shortest Path First Routing Protocol Date: Aim: To simulate the Open Shortest Path First routing protocol based on the cost assigned to each path. . Read further source and destination from the user to calculate the shortest path. Initialize SOURCE to 1 and DEST to 1. n. 8. Algorithm: 1. Print the distance of the given path calculated by the program and the path. 4. 6. 5.

\n"). } src--. scanf("%d". if(n>20) { printf("\nSorry. } printf("\nEnter the edges one by one. Try again!\n"). . &dst). void FindShortest(int src). &wt). -1 to stop. dst. src. &src). scanf("%d". unoptimized[20]. int UnoptimizedNodeExists().Open Shortest Path First Routing Protocol Filename: OSPF. printf("Destination\t: ").h> #define INFINITY 0x7FFF static int adj[20][20]. scanf("%d". stack[20]. void main() { int wt. void Init(). dst--. int dist[20]. clrscr().h> #include <conio. previous[20]. continue. if(src<1 || src>n || dst<1 || dst>n) { printf("\nInvalid input\n"). -1. top=0. n. so many are not allowed. goto AskNumberOfNodes.c Program Code: #include <stdio. while(1) { printf("\nSource\t\t: "). AskNumberOfNodes: printf("Enter the number of nodes (max 20): "). &n). if(src==-1 && dst==-1) break. scanf("%d". v. printf("Edge weight\t: ").

if(wt<1) { printf("\nWeight must be atleast 1! Edge neglected.. if(src==-1 && dst==-1) break. } src--. continue. if(src<1 || src>n || dst<1 || dst>n) { printf("\nInvalid input\n"). } } } . stack[top]+1). &src. while(1) { printf("\nEnter source and destination nodes: ").". } if(top==1 && src!=dst) printf("\nSorry no such path found!\n"). v=dst. -1 to stop. } printf("\b\b \n"). while(v!=(-1)) { stack[top]=v. FindShortest(src). &dst). } printf("\nTo find shortest path. top++. continue. use -1. printf("%d . scanf("%d%d". dst--. v=previous[v].\n"). else { printf("\n"). } adj[src][dst]=adj[dst][src]=wt.. while(top) { top--.\n").

unoptimized[i]=1. u=src. // Initialize prev[]. i<n. i<n. i++) { if(unoptimized[i] && dist[i]<minDistance) { u=i. source) is obviously zero dist[src]=0. alt. dist[i]=INFINITY.void Init() { int i. for(i=0. } } . } } int UnoptimizedNodeExists() { int i. minDistance=dist[i]. i++) { previous[i]=(-1). // u := vertex in Q with smallest dist[] . } void FindShortest(int src) { int i. for(i=0. } return 0. i<n. dist[] and // Q := the set of all nodes in Graph . // dist_between(source. minDistance. for(i=0. while(UnoptimizedNodeExists()) { minDistance = INFINITY. i++) { if(unoptimized[i]) return 1. Init(). v.

previous[v]=u. if(alt<dist[v]) { dist[v]=alt. } // Remove u from Q unoptimized[u]=0. v<n. break.if(minDistance == INFINITY) { printf("\nOne or more isolated node(s) found!\n"). } } } } } . v++) { // (where v is not removed from Q) if(adj[u][v] && unoptimized[v]) { alt = dist[u] + adj[u][v]. // For each neighbor v of u: for(v=0.

Output: Enter the number of nodes (max 20): 5 Enter the edges one by one. -1, -1 to stop. Source :1 Destination : 2 Edge weight : 1 Source :2 Destination : 5 Edge weight : 6 Source :2 Destination : 4 Edge weight : 1 Source :2 Destination : 3 Edge weight : 3 Source :5 Destination : 4 Edge weight : 4 Source :3 Destination : 4 Edge weight : 2 Source :1 Destination : 3 Edge weight : 5 Source : -1 Destination : -1 Graph Structure

To find shortest path... use -1, -1 to stop. Enter source and destination nodes: 1 5 1-2-4-5 Enter source and destination nodes: 1 4 1-2-4 Enter source and destination nodes:-1 -1

Result:
Thus the program to simulate the Open Shortest Path First routing protocol based on the cost assigned to each path was implemented successfully and the output was verified.

Ex No:10(a)

Date: Study of TCP Performance using Network Simulator

Aim: To study the performance of TCP based network by considering the bandwidth.

Procedure: 1. Start 2. Set a simulator variable (ns). 3. Set the animator file and the trace file to simulator. 4. Create nodes for the network topology. 5. Design the topology by connecting the nodes with required links and desirable orientation. 6. Add TCP agent to a specific node and assign another node as the sink. 7. Create the Application level traffic, File Transfer (FTP), to represent the data flow. 8. Record the data rate at every interval of 0.5 seconds and store the contents in a trace file. 9. Use this trace file to draw a performance graph. 10. Close all the instances of ns and trace files. 11. Initiate the animator (nam) 12. Simulate the designed network. 13. Verify the output. 14. Stop

Study of TCP Performance using Network Simulator Filename: TCP.tr & exit 0 } proc record {} { global sink tf #Get an instance of the simulator set ns [Simulator instance] #Set the time after which the procedure should be called again set time 0.nam & exec xgraph -x Time -y Bandwidth out.tr w] proc finish {} { global ns nf tf $ns flush-trace close $nf close $tf exec nam out.nam w] $ns namtrace-all $nf set tf [open out.tcl Program Code: set ns [new Simulator] set nf [open out.5 #How many bytes have been received by the traffic sinks? set bw0 [$sink set bytes_] #Get the current time set now [$ns now] #Calculate the bandwidth (in MBit/s) and write it to the files puts $tf "$now [expr $bw0/$time*8/100000]" #Reset the bytes_ values on the traffic sinks $sink set bytes_ 0 #Re-schedule the procedure $ns at [expr $now+$time] "record" } set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] .

5 "$ftp stop" $ns at 5.2 "$ftp start" $ns at 4.$ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n3 1Mb 10ms DropTail $ns duplex-link $n2 $n3 1Mb 10ms DropTail $ns duplex-link-op $n0 $n2 orient right-up $ns duplex-link-op $n1 $n3 orient right $ns duplex-link-op $n2 $n3 orient left-up $ns queue-limit $n1 $n3 4 $ns duplex-link-op $n2 $n3 queuePos 0.5 set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n1 $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 0.0 "finish" $ns run .0 "record" $ns at 0.

Output: .

Result: Thus TCP performance was studied successfully. .

Constant Bit Rate (CBR). 8. 9. 15. Start 2. Procedure: 1. Create nodes for the network topology. Add UDP agent to a specific node and assign another node as the sink. 14. Stop. Use this trace file to draw a performance graph. Verify the output. Set the animator file and the trace file to simulator. Initiate the animator (nam). . Set a simulator variable (ns). Record the data rate at every interval of 0. Design the topology by connecting the nodes with required links and desirable orientation. 5. 6. 10. The data (traffic) flows from the agent to the sink. to represent the data flow.Ex No: 10(b) Date: Study of UDP Performance using Network Simulator Aim: To study the performance of an UDP based network by considering the data rate of the packets sent. 11. 4. 3. 12. 7. 13. Simulate the designed network. Close all the instances of ns and trace files. Create the Application level traffic.5 seconds and store the contents in a trace file.

0 #Get the current time set now [$ns now] #Get the data rate and write it to the files puts $tf "$now [$appl set rate_]" #Re-schedule the procedure $ns at [expr $now+$time] "record" } set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] $ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n3 1Mb 10ms DropTail $ns duplex-link $n2 $n3 1Mb 10ms DropTail .tr & exit 0 } proc record {} { global tf source appl set ns [Simulator instance] #Set the time after which the procedure should be called again set time 2.nam w] $ns namtrace-all $nf set tf [open out.tr w] proc finish {} { global ns nf tf $ns flush-trace close $nf close $tf exec nam out.Study of UDP Performance using Network Simulator Filename: UDP.tcl Program code: set ns [new Simulator] set nf [open out.nam & exec xgraph -x Time -y DataRate out.

5 set source [new Agent/UDP] $ns attach-agent $n0 $source $source set packetSize 10 set dest [new Agent/Null] $ns attach-agent $n1 $dest $ns connect $source $dest set appl [new Application/Traffic/CBR] $appl attach-agent $source $appl set random_ true $ns at 0.5 "$appl stop" $ns at 5.5 "$appl start" $ns at 3.0 "finish" $ns run .5 "$source set packetSize_ 45" $ns at 4.$ns duplex-link-op $n0 $n2 orient right-up $ns duplex-link-op $n1 $n3 orient right $ns duplex-link-op $n2 $n3 orient left-up $ns queue-limit $n2 $n3 4 #$ns duplex-link-op $n4 $n5 queuePos 0.0 "record" $ns at 0.

Output: .

.Result: Thus UDP performance was studied successfully.

You're Reading a Free Preview

Descarregar
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->