Você está na página 1de 69

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.

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

Ex No: 02 TCP DATE AND TIME

Date:

AIM: To write a program in C to implement TCP Daytime Client Server.

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. Retrieve time information and send it to the client through the connected socket. 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. Give request to obtain time information. 6. Read the information and print it. 7. Close the connected socket.

TCP DATE AND TIME Filename: Date_server.c Server program Code:


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

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

Filename: Date_client.c Client program Code:


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

while((n=read(sockfd,recvline,sizeof(recvline)-1))>0) { recvline[n]=0; if(fputs(recvline,stdout)==EOF) { perror("Error in fputs()"); exit(1); } } if(n<0) { perror("Error while read"); exit(1); } exit(0); }

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

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

Ex No: 03 ECHO SERVER

Date:

AIM: To write a program in C to implement Echo server.

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. Echo the received information from the connected socket. 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. Read the message to be echoed from the user. 6. Send it to the server and read the response that comes back from the server. 7. Print the response received from the server. 8. Close the connected socket.

ECHO SERVER Filename: echoserver.c Server program Code:


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

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

Filename: echoclient.c Client program Code:


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

strcpy(buffer,""); read(conn_s,buffer,20); printf("\nEcho response:%s\n",buffer); return EXIT_SUCCESS; }

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

Output:
Server Side: [network@server Network]$ cc echoserver.c [network@server Network]$ ./a.out Enter the port address: 2134 Client Side: [network@server Network]$ cc echoclient.c [network@server Network]$ ./a.out 172.16.6.200 2134 Remote host address: 172.16.6.200 Remote port address: 2134 Enter the string to be echoed: Check Echo response: Check

Message received and echoed: Check Connection Closed

Ex No: 04 DOMAIN NAME SYSTEM

Date:

AIM: To write a program in C to implement Domain Name System.

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 domain name from the client. 9. Search for the given domain name in dns_map.txt and send the corresponding IP address to the client. 10. 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. Read the domain name from the user. 6. Send the domain name and receive the IP address. 7. Print the received IP address. 8. Close the connected socket.

DOMAIN NAME SYSTEM Filename: DNS_server.c Server program Code:


//Program to implement DNS. #include<sys/socket.h> #include<netinet/in.h> #include<string.h> #include<stdio.h> #include<time.h> int main() { int sd,sd2,nsd,sport; char rec[20], text[20]; struct sockaddr_in servaddr; FILE *fp1=fopen("dns_map.txt","r"); printf("\nEnter the port address:"); scanf("%d",&sport); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) printf("Error in creating socket\n"); else printf("Socket created\n"); servaddr.sin_family=AF_INET; 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("Bind error\n"); else printf("Bind success\n"); listen(sd,5); nsd=accept(sd,(struct sockaddr*)NULL,NULL); if(nsd<0) printf("Cannot connect\n"); else printf("Connection successful\n"); recv(nsd,rec,20,0); printf("\n%s\n",rec); fscanf(fp1,"%s",text); while(!feof(fp1)) { if(strcmp(text,rec)==0) { fscanf(fp1,"%s",text); printf("Sending IP address.\n"); send(nsd,text,20,0); break; }

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

Filename: DNS_client.c Client program Code:


//Program to implement DNS. #include<sys/socket.h> #include<netinet/in.h> #include<string.h> #include<stdio.h> #include<time.h> int main() { int csd,cport; char sen[20],rec[20]; struct sockaddr_in servaddr; printf("\nEnter the port address:"); scanf("%d",&cport); csd=socket(AF_INET,SOCK_STREAM,0); if(csd<0) printf("Error: Socket creation failed\n"); else printf("Socket creation successful\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"); printf("Enter the domain name:"); scanf("%s",sen); send(csd,sen,20,0); recv(csd,rec,20,0); printf("\nThe IP address is:%s\n",rec); return 0; }

Filename:

dns_map.txt

localhost 172.16.6.200 localhost1 172.16.6.201 new 172.16.6.210

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

Output:
Server Side: [network@server Network]$ cc DNS_server.c [network@server Network]$ ./a.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.c [network@server Network]$ ./a.out Enter the port address: 2950 Socket is creation successfully Connected Enter the domain name: localhost1 The IP address is: 172.16.6.201

Ex No: 05 UDP CHAT

Date:

Aim: To write a program in C to implement UDP client server chat. Algorithm: UDP 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. Retrieve information from Socket. 7. Echo retrieved information to socket 8. Close the connected socket. UDP 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. Close the connected socket.

UDP CHAT Filename: UDP_server.c Server program Code:


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

Filename: UDP_client.c Client program Code:


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

Result: Thus the program to implement UDP Chat server was written successfully and the output was verified.

Output:
Server Side: [network@server Network]$ cc UDP_server.c [network@server Network]$ ./a.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. How are you? Closing the socket connection Socket closed Client Side: [network@server Network]$ cc UDP_client.c [network@server Network]$ ./a.out 172.16.6.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

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. 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 message from the user and transmit it to the client. 9. Read the status of the transmitted frame; in each case of error retransmit all the frames from the error frame till the last sent frame. 10. Repeat previous step until the whole message is received by the client. 11. 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. Read a frame from the server. 6. Check the received frame and report error if any. 7. Repeat the previous step until the message is error-free. 8. Receive the next frame from the server and continue the previous two steps till end. 9. Close the connected socket.

SLIDING WINDOW GO BACK N Filename: GN_server.c Server program Code:


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

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

Filename: GN_client.c Client program Code:


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

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

Output:
Server Side: [network@server Network]$ cc GN_server.c [network@server Network]$ ./a.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.c [network@server Network]$ ./a.out Enter the port address: 1250 Received: Tria0123 Do you want to report an error(1-Yes, 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 Received: t8 Do you want to report an error(1-Yes, 2-No): 0 Exiting

Ex No: 06(b) SLIDING WINDOW SELECTIVE REPEAT

Date:

AIM: To write a C program to implement Sliding Window protocol of type selective repeat. 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 message from the user and transmit it to the client. 9. Read the status of the transmitted frame; in each case of error retransmit the particular frame. 10. Repeat previous step until the whole message is received by the client. 11. 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. Read a frame from the server. 6. Check the received frame and report error if any. 7. Repeat the previous step until the message is error-free. 8. Receive the next frame from the server and continue the previous two steps till end. 9. Close the connected socket.

SLIDING WINDOW SELECTIVE REPEAT Filename: SR_server.c Server program Code:


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

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

Filename: SR_client.c Client program Code:


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

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

Output:
Server Side: [network@server Network]$ cc SR_server.c [network@server Network]$ ./a.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.c [network@server Network]$ ./a.out Enter the port address: 1250 Received: Hell0123 Do you want to report an error(1-Yes, 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, 2-No): 0 Exiting

Ex No: 07 REMOTE PROCEDURE CALL

Date:

AIM: To write a C program to implement remote procedure call. 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. Define a procedure convert to toggle the case of characters supplied by the client. 9. Read the data to be operated to be by the procedure. 10. Invoke the procedure with the parameters and store the return value in the buffer. 11. Send the value in the buffer to the client. 12. 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. Read the string to be converted from the user. 6. Send the parameter to the server. 7. Receive the output string and display it to the user. 8. Close the connected socket.

REMOTE PROCEDURE CALL Filename: RPC_server.c Server program Code:


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

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

Filename: RPC_client.c Client program Code:


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

return EXIT_SUCCESS; }

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

Output:
Server Side: [network@server Network]$ cc RPC_server.c [network@server Network]$ ./a.out Enter the server port: 3000 Remote Host Address: 172.16.6.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.c [network@server Network]$ ./a.out 172.16.6.200 3000

Ex No: 08(a) Address Resolution Protocol

Date:

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

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

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

Result:
Thus the program to implement Address Resolution Protocol to obtain MAC address was written successfully and the output was verified.

Output:

Server side: [network@server Network]$ cc ARP_server.c [network@server Network]$ ./a.out MAC IP 1.1.1.3 1:A1:2E:3:4:5 1.1.1.4 1:B1:C3:D2:49:5 Enter the key and the IP address7000 1.1.1.3 Enter the key and the IP address

Client side: [network@server Network]$ cc ARP_client.c [network@server Network]$ ./a.out Enter the IP address:1.1.1.3

The MAC address is: 1:A1:2E:3:4:5

Ex No: 08(b) Reverse Address Resolution Protocol


Aim:

Date:

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

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

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

Result:
Thus the program to implement Address Resolution Protocol to obtain IP address was written successfully and the output was verified.

Output: Server side: [network@server Network]$ cc RARP_server.c [network@server Network]$ ./a.out IP MAC 1.1.1.3 1:A1:2E:3:4:5 1.1.1.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.c [network@server Network]$ ./a.out Enter the MAC address:1:A1:2E:3:4:5

The IP address is: 1.1.1.3

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. Algorithm: 1. Read the number of nodes, n. 2. Read the cost matrix for the path from each node to another node. 3. Initialize SOURCE to 1 and DEST to 1. 4. Compare D of a node which is the distance from source to that corresponding node. 5. Repeat steps 6 to 8 for n-1 nodes. 6. Choose the node that has not be included compare the distance to reach the node using the newly included node. 7. Take the minimum value as the new distance. 8. Print the distance of the given path calculated by the program and the path. 9. Read further source and destination from the user to calculate the shortest path. 10. Exit when the user chooses -1 for source and destination.

Open Shortest Path First Routing Protocol Filename: OSPF.c Program Code: #include <stdio.h> #include <conio.h> #define INFINITY 0x7FFF

static int adj[20][20]; int dist[20], previous[20], unoptimized[20], n; void Init(); int UnoptimizedNodeExists(); void FindShortest(int src); void main() { int wt, stack[20], top=0, src, dst, v; clrscr(); AskNumberOfNodes: printf("Enter the number of nodes (max 20): "); scanf("%d", &n); if(n>20) { printf("\nSorry, so many are not allowed. Try again!\n"); goto AskNumberOfNodes; } printf("\nEnter the edges one by one. -1, -1 to stop.\n"); while(1) { printf("\nSource\t\t: "); scanf("%d", &src); printf("Destination\t: "); scanf("%d", &dst); if(src==-1 && dst==-1) break; if(src<1 || src>n || dst<1 || dst>n) { printf("\nInvalid input\n"); continue; } src--; dst--; printf("Edge weight\t: "); scanf("%d", &wt);

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

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

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

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.tcl Program Code: set ns [new Simulator] set nf [open out.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.nam & exec xgraph -x Time -y Bandwidth out.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.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]

$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 "record" $ns at 0.2 "$ftp start" $ns at 4.5 "$ftp stop" $ns at 5.0 "finish" $ns run

Output:

Result: Thus TCP performance was studied successfully.

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.

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 UDP agent to a specific node and assign another node as the sink. 7. The data (traffic) flows from the agent to the sink. 8. Create the Application level traffic, Constant Bit Rate (CBR), to represent the data flow. 9. Record the data rate at every interval of 0.5 seconds and store the contents in a trace file. 10. Use this trace file to draw a performance graph. 11. Close all the instances of ns and trace files. 12. Initiate the animator (nam); 13. Simulate the designed network. 14. Verify the output. 15. Stop.

Study of UDP Performance using Network Simulator Filename: UDP.tcl Program code: set ns [new Simulator] set nf [open out.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.nam & exec xgraph -x Time -y DataRate out.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.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

$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.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.0 "record" $ns at 0.5 "$appl start" $ns at 3.5 "$source set packetSize_ 45" $ns at 4.5 "$appl stop" $ns at 5.0 "finish" $ns run

Output:

Result: Thus UDP performance was studied successfully.

Você também pode gostar