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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

How are you? Sending data to socket Data has been sent to socket Closing socket connection Socket closed .200 32775 Creating datagram socket Socket created successfully Data: Hi.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.16. How are you? Closing the socket connection Socket closed Client Side: [network@server Network]$ cc UDP_client.Output: Server Side: [network@server Network]$ cc UDP_server./a.c [network@server Network]$ .6.c [network@server Network]$ ./a.

. 5. Declare variables for socket ID. 9. Repeat the previous step until the message is error-free. socket addresses. Specify number of allowed connection. character buffer. Repeat previous step until the whole message is received by the client. 2. socket types. port number. 3. Wait for connection. Create socket for client. socket types. Connect client socket to the server socket addresses. Client: 1. Include necessary header files to support functions for socket definition. Declare variables for socket ID. I/O functions. Read a frame from the server. Create socket for server. UNIX system calls. 4. Read the status of the transmitted frame. 4. I/O functions. 8. UNIX system calls. Bind socket with addresses. Include necessary header files to support functions for socket definition. port number. Receive the next frame from the server and continue the previous two steps till end. socket addresses. 2. 7. 11. 6. 8. etc. character buffer. in each case of error retransmit all the frames from the error frame till the last sent frame. Close the connected socket. ALGORITHM: Server: 1. 10. 9. 6.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. Close the connected socket. Accept connection (If any). 7. etc. Read the message from the user and transmit it to the client. 5. Check the received frame and report error if any. 3. internet addresses. internet addresses.

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

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

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

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

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 .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./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./a.c [network@server Network]$ .out Enter the port address: 1250 Received: Tria0123 Do you want to report an error(1-Yes.

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

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

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

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

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

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

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

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

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

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

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

c [network@server Network]$ ./a./a.16.out Enter the server port: 3000 Remote Host Address: 172.c [network@server Network]$ .6.200 3000 .6.out 172.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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

nam w] $ns namtrace-all $nf set tf [open 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.tr w] proc finish {} { global ns nf tf $ns flush-trace close $nf close $tf exec nam out.tcl Program Code: set ns [new Simulator] set nf [open out.Study of TCP Performance using Network Simulator Filename: TCP.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] .nam & exec xgraph -x Time -y Bandwidth out.

5 "$ftp stop" $ns at 5.0 "finish" $ns run .$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.2 "$ftp start" $ns at 4.0 "record" $ns at 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.

Output: .

.Result: Thus TCP performance was studied successfully.

Set the animator file and the trace file to simulator.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. to represent the data flow. Use this trace file to draw a performance graph. Close all the instances of ns and trace files. Initiate the animator (nam). 13. Add UDP agent to a specific node and assign another node as the sink. . 8. Constant Bit Rate (CBR). Create the Application level traffic.5 seconds and store the contents in a trace file. 14. Record the data rate at every interval of 0. 6. Stop. Procedure: 1. 3. 7. Verify the output. Design the topology by connecting the nodes with required links and desirable orientation. Start 2. 15. 10. 11. Simulate the designed network. Set a simulator variable (ns). The data (traffic) flows from the agent to the sink. 4. Create nodes for the network topology. 12. 9. 5.

nam & exec xgraph -x Time -y DataRate out.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 .tcl Program code: set ns [new Simulator] set nf [open out.nam w] $ns namtrace-all $nf set tf [open out.Study of UDP Performance using Network Simulator Filename: UDP.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.tr w] proc finish {} { global ns nf tf $ns flush-trace close $nf close $tf exec nam out.

5 "$appl stop" $ns at 5.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.5 "$appl start" $ns at 3.0 "finish" $ns run .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.

Output: .

Result: Thus UDP performance was studied successfully. .

Sign up to vote on this title
UsefulNot useful