Sei sulla pagina 1di 20

1.

CRC
#include<stdio.h> #include<string.h> char gp[]="11000000000000101"; //char gp[]="10010"; int crc(char *input,char *output,char *gp,int mode) { int j,k; strcpy(output,input); if(mode) { for(j=1; j<strlen(gp); j++) strcat(output,"0"); } for(j=0; j<strlen(input); j++) if(*(output+j) == '1') for(k=0; k<strlen(gp); k++) { if (((*(output+j+k) =='0') && (gp[k] == '0') || (*(output+j+k) == '1') && (gp[k] == '1'))) *(output+j+k)='0'; else *(output+j+k)='1'; } for(j=0; j<strlen(output); j++) if(output[j] == '1') return 1; return 0; } int main() { system("clear"); printf("CRC-16 \n"); char input[50],output[50]; char recv[50]; printf("Enter the input message in binary:\n"); scanf("%s",input); crc(input,output,gp,1); printf("The transmitted message is: %s%s\n",input,output+strlen (input)); printf("Enter the recevied message in binary: \n"); scanf("%s",recv); if(!crc(recv,output,gp,0)) printf("\n No error in data\n"); else printf("\n Error in data transmission has occurred\n"); }
1

output:
./a.out Enter the input message in binary: 1010 The transmitted message is: 10100000000000111100 Enter the recevied message in binary: 111111111111 Error in data transmission has occurred [sudhakar@localhost sam]$ ./a.out Enter the input message in binary: 11 The transmitted message is: 110000000000001010 Enter the recevied message in binary: 110000000000001010 No error in data

2.//Frame sorting
#include <stdio.h> #include <string.h> #define fsize 3 #define pno 15 typedef struct packet { char data[fsize+1]; int seq_num; }packet; packet send[pno],rec[pno]; char msg[pno*fsize]; int max_frames; void make_frames(); void shuffle_frames(); void receive_frames(); void sort_frames(); int main() { system("clear"); printf("FRAME SORTING PROGRAM\n"); printf("Enter the message:"); gets(msg);

make_frames(); shuffle_frames(); receive_frames(); } //Used to sort the frames void sort_frames() { int min,i,j; packet temp; for(i=0;i<max_frames-1;i++) { min=i; for(j=i+1;j<max_frames;j++) if( rec[j].seq_num < rec[min].seq_num) min=j; temp.seq_num=rec[min].seq_num; strcpy(temp.data,rec[min].data); rec[min].seq_num=rec[i].seq_num; strcpy(rec[min].data,rec[i].data); rec[i].seq_num=temp.seq_num; strcpy(rec[i].data,temp.data); } } //Make frames and simulate sending data void make_frames() { int i=0,k=0; while( i<strlen(msg) ) { strcpy(send[k].data,&msg[i]); send[k].data[fsize]='\0'; send[k].seq_num=++k; i+=fsize; } max_frames=k; printf("Making frames....\n"); printf("Seq num. printf("-------------for(i=0;i<max_frames;i++) printf("%d. %s\n",send[i].seq_num,send[i].data); } Data\n"); ---------\n");

void shuffle_frames() { int t,k=0,i; int t_rand[max_frames+1]; for(i=0;i<max_frames;i++) t_rand[i]=0; while(k<max_frames) { t=rand()%max_frames; if( t_rand[ t ] == 0 ) { t_rand[t]=1 ; strcpy(rec[k].data,send[t].data); rec[k].seq_num=send[t].seq_num; k++; } } } void receive_frames() { //Print the frames received int i; printf("Receiving frames....\n"); //Print the frames created printf("Seq num. printf("-------------for(i=0;i<max_frames;i++) printf("%d. %s\n",rec[i].seq_num,rec[i].data); printf("Sorting...\n"); sort_frames(); printf("Sorted packets:\n"); printf("Seq num. printf("-------------for(i=0;i<max_frames;i++) printf("%d. %s\n",rec[i].seq_num,rec[i].data); } Data\n"); ---------\n");

Data\n"); ---------\n");

//outPut
./a.out FRAME SORTING PROGRAM Enter the message:hi welcome to bnmit Making frames.... Seq num. Data ---------------------1. hi 2. wel 3. com 4. e t 5. o b 6. nmi 7. t Receiving frames.... Seq num. Data ---------------------2. wel 5. o b 3. com 6. nmi 4. e t 7. t 1. hi Sorting... Sorted packets: Seq num. Data ---------------------1. hi 2. wel 3. com 4. e t 5. o b 6. nmi 7. t

3.Distance Vector
#include<stdio.h> int n, e, s[20][20], graph[20][20]; void initialize() { int i, j; for (i = 1; i <= 20; i++) for (j = 1; j <= 20 && (s[i][j] = graph[i][j] = 0); j++); } void getgraph() { int i, strt, end; printf("Enter no. of routers & edges in the graph: "); scanf("%d%d", &n, &e); while (e-- > 0) { printf("Enter start router -> end router : "); scanf("%d%d", &strt, &end); graph[strt][end] = graph[end][strt] = 1; } } void gettable(int src) { int i, j; printf("Enter information for Source router %d.\n", src); for (i = 1; i <= n; i++) if (graph[src][i] == 1) { printf("Enter dist 4m src router %d to %d :", src, i); scanf("%d", &s[src][i]); } printf("Enter the contents of Echo packet of Adj routers of %d\n",src); for (i = 1; i <= n; i++) if (graph[src][i] == 1) { printf("Enter the %d.\n", i); for (j = 1; j <= n; { if (i == j) printf("Enter scanf("%d", } } }

contents of Echo packet of router j++) continue; dist 4m router %d to %d :", i, j); &s[i][j]);

void process(int src, int dest) { int min = 999, i, delay, via; for (i = 1; i <= n; i++) if (graph[src][i] == 1) { delay = s[src][i] + s[i][dest]; if (delay < min) { min = delay; via = i; } } printf("Suitable path from router %d to %d is through router %d with delay %d units\n", src, dest, via, min); } int main() { int src, dest; initialize(); getgraph(); printf("Enter the Source & Destination router\n"); scanf("%d%d", &src, &dest); gettable(src); process(src, dest); return 0; }

Output:
$ gcc dv.c $ ./a.out Enter no. of routers & Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> Enter start router --> edges in the end router : end router : end router : end router : end router : end router : end router : end router : end router : end router : end router : end router : graph: 5 12 1 2 2 3 3 4 4 1 1 5 5 3 3 5 5 1 1 4 4 3 3 2 2 1

Enter the Source & Destination router 1 3 Enter information for Source router 1. Enter distance from source router 1 to 2 :3 Enter distance from source router 1 to 4 :3 Enter distance from source router 1 to 5 :7 Enter the contents of Echo packet of Adjacent routers of 1 Enter the contents of Echo packet of router 2. Enter distance from router 2 to 1 :8 Enter distance from router 2 to 3 :4 Enter distance from router 2 to 4 :999 Enter distance from router 2 to 5 :999 Enter the contents of Echo packet of router 4. Enter distance from router 4 to 1 :5 Enter distance from router 4 to 2 :999 Enter distance from router 4 to 3 :3 Enter distance from router 4 to 5 :999 Enter the contents of Echo packet of router 5. Enter distance from router 5 to 1 :4 Enter distance from router 5 to 2 :999 Enter distance from router 5 to 3 :1 Enter distance from router 5 to 4 :999 Suitable path from router 1 to 3 is through router 4 with delay 6 units

4.Server Socket
#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> #define SERV_TCP_PORT 3015 #define MAX 1000 int main() { int pid,sockfd,bytes,newsockfd,client,n,fp; struct sockaddr_in cli_addr,serv_addr; char buffer[MAX]; if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) printf("Socket error\n"); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=INADDR_ANY; serv_addr.sin_port=SERV_TCP_PORT; if((bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)))<0) printf("Blind error\n"); listen(sockfd,5); printf("\nserver online\n"); for(;;) { client=sizeof(cli_addr); newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&client); if(newsockfd<0) printf("Server accept error\n"); if((pid=fork())<0) printf("fork error\n"); else if(pid==0) { close(sockfd); bytes=recv(newsockfd,buffer,MAX,0); fp=open(buffer,O_RDONLY); if(fp<0) { printf("OPen error\n"); exit(0); } printf("File receive\n"); printf("\n%s",buffer); while((n=read(fp,buffer,MAX))>0) { bytes=send(newsockfd,buffer,n,0);
9

} close(newsockfd); exit(0); } } return; }

Client Socket
#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<fcntl.h> #define SERV_TCP_ADDR"127.0.0.1" #define SERV_TCP_PORT 3015 #define MAX 100 int main() { int pid,sockfd,bytes,clilen,n,fp; struct sockaddr_in serv_addr; char buffer[MAX]; printf("Enter filename\n"); scanf("%s",buffer); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr(SERV_TCP_ADDR); serv_addr.sin_port=SERV_TCP_PORT; if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) printf("Error\n"); if((connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)))<0) printf("Connection error\n"); printf("Conntents of file $s\n",buffer); printf("Requesting service\n"); bytes=send(sockfd,buffer,MAX,0); while((bytes=recv(sockfd,buffer,MAX,0))>0) write(1,buffer,bytes); close(sockfd); }

10

5. FIFO SERVER
#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #define FIFO1 "fifo1" #define FIFO2 "fifo2" #define PERMS 0666 char fname[256]; int main() { int readfd,writefd,fd; ssize_t n; char buff[512]; if(mkfifo(FIFO1,PERMS)<0) printf("cant create FIFO files\n"); if(mkfifo(FIFO2,PERMS)<0) printf("Cant create FIFO files\n"); printf("Waiting for connection request......\n"); readfd=open(FIFO1,O_RDONLY,0); writefd=open(FIFO2,O_WRONLY,0); printf("Connection Established......\n"); read(readfd,fname,255); printf("client has requested files %s \n ",fname); if((fd=open(fname,O_RDWR))<0) { strcpy(buff,"FILE DOES NOT EXIST....\n"); write(writefd,buff,strlen(buff)); } else { while((n=read(fd,buff,512))>0) write(writefd,buff,n); } close(readfd); unlink(FIFO1); close(writefd); unlink(FIFO2); }

11

FIFO CLIENT
#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #define FIFO1 "fifo1" #define FIFO2 "fifo2" #define PERMS 0666 char fname[256]; int main() { int readfd,writefd; ssize_t n; char buff[512]; printf("Trying to connect to server....\n"); writefd=open(FIFO1,O_WRONLY,0); readfd=open(FIFO2,O_RDONLY,0); printf("Connected.....\n"); printf("Enter the filename to request from server:"); scanf("%s",fname); write(writefd,fname,strlen(fname)); printf("waiting for server to reply......\n"); while((n=read(readfd,buff,512))>0) write(1,buff,n); close(readfd); close(writefd); return 0; }

12

6. RSA
#include<stdio.h> #include<string.h> int gcd(int x,int y) { int r; while(y!=0) { r=x%y; x=y; y=r; } return x; } unsigned int mod(unsigned int x,unsigned int y,unsigned int n) { unsigned long k=1; int i; for(i=0;i<y;i++) k=(k*x)%n; return(unsigned int)k; } int main() { unsigned int pt,z,q,n,d=0,e=2,i; char m[100]; int p[100],c[100]; printf("Enter the values of p & Q\n"); scanf("%d%d",&pt,&q); n=pt*q; z=(pt-1)*(q-1); while(e<z) { if(gcd(e,z)==1) break; else e++; } while(((d*e)-1)%z!=0) { d++; if(d==e) d++; } printf("Values are n=%d\t z=%d\t d=%d\t e=%d\t\n",n,z,d,e);
13

printf("Enter the string\n"); scanf("%s",m); printf("Encrypting\n"); printf("The ciper terxt is \n"); for(i=0;i<strlen(m);i++) { p[i]=m[i]-97; c[i]=mod(p[i],e,n); printf("%d",c[i]); } printf("\n"); printf("Decrypting \n"); printf("decrypted text is \n"); for(i=0;i<strlen(m);i++) { p[i]=mod(c[i],d,n); printf("%c",p[i]+97); } return 0; }

OUTPUT
./a.out Enter the values of p & Q 7 103 Values are n=721 z=612 Enter the string hello Encrypting The ciper terxt is 224303268268679 Decrypting decrypted text is hello

d=245

e=5

14

7. Hamming code
#include<stdlib.h> #include<stdio.h> char data[5]; int encoded[8],edata[7],syndrom[3]; int hmatrix[3][7]={1,0,0,0,1,1,1,0,1,0,1,0,1,1,0,0,1,1,1,0,1}; char gmatrix[4][8]={"0111000","1010100","1100010","1110001"}; int main() { int i,j; printf("hamming code-----encoding\n"); printf("Enter 4 bit data\n"); scanf("%s",&data); printf("generator matrix\n"); for(i=0;i<4;i++) printf("\t%s\n",gmatrix[i]); printf("Encoded area\n"); for(i=0;i<7;i++) { for(j=0;j<4;j++) encoded[i]+=((data[j]-'0')*(gmatrix[j][i]-'0')); encoded[i]=encoded[i]%2; printf("%d",encoded[i]); } printf("\n hamming code-----decoding\n"); printf("Enter encoded bits as received\n"); for(i=0;i<7;i++) scanf("%d",&edata[i]); for(i=0;i<3;i++) { for(j=0;j<7;j++) syndrom[i]=syndrom[i]+(edata[j]*hmatrix[i][j]); syndrom[i]=syndrom[i]%2; } for(j=0;j<7;j++) if((syndrom[0]==hmatrix[0][j])&&(syndrom[1]==hmatrix[1][j])&&(sy ndrom[2]==hmatrix[2][j])) break; if(j==7) printf("data is error free!!\n"); else { printf("Error received at bit number %d of the data \n",j+1); edata[j]=!edata[j]; printf("The correct data should be:");
15

for(i=0;i<7;i++) printf("%d",edata[i]); } }

OUTPUT
./a.out hamming code-----encoding Enter 4 bit data 1010 generator matrix 0111000 1010100 1100010 1110001 Encoded area 1011010 hamming code-----decoding Enter encoded bits as received 1 0 1 1 0 1 0 data is error free!!

16

8 LEAKY BUCKET
#include <stdio.h> void bktInput(int a, int b, int size) { if (a > size) printf("\tBucket overflow.\n"); else { sleep(1); //Generate equal time delay while (a > b) { printf("\t%d bytes outputted\n", b); a -= b; sleep(1); } if (a > 0) printf("\tLast %d bytes sent\n", a); printf("\tBucket output successful.\n"); } } int main() { int op, pktSize, i, bktsize, n, delay; printf("bucket Size : "); scanf("%d", &bktsize); printf("Output Rate : "); scanf("%d", &op); printf("No of packets : "); scanf("%d", &n); for (i = 0; i < n; i++) { delay = (rand() % 10); sleep(delay); pktSize = rand() % 1000; printf("Packet no %d\tPacket size = %d\n", i, pktSize); bktInput(pktSize, op, bktsize); } return 0; }

OUTPUT
./a.out bucket Size : 512 Output Rate : 128 No of packets : 3 Packet no 0 Packet size = 886
17

Bucket overflow. Packet no 1 Packet size = 915 Bucket overflow. Packet no 2 Packet size = 335 128 bytes outputted 128 bytes outputted Last 79 bytes sent Bucket output successful.

Viva Questions.
1. Describe bind(), listen(), accept(),connect(), send() and recv(). 2. Differentiate between Connectionless & connection oriented connection. 3. Differentiate between flow control and congestion control. 4. Differentiate between Leaky bucket and Token bucket. 5. Differentiate between logical and physical address. 6. Differentiate between Point-to-Point Connection and End-toEnd connections. 7. Differentiate between Prims and Kruskals algorithm. 8. Differentiate between route, bridge, switch and hub. 9. Differentiate between Simulation and Emulation. 10. Differentiate between TCP and UDP. 11. Differentiate between TCP/IP Layers and OSI Layers 12. Explain mkfifo(), open(), close() with parameters. 13. How do you classify congestion control algorithms? 14. How do you classify cryptographic algorithms? 15. How do you classify routing algorithms? Give examples for each. 16. How do you generate busty traffic? 17. How do you generate multiple traffics across different sender receiver pairs? 18. How do you implement Leaky bucket?
18

19. How do you overcome count to infinity problem? 20. How do you setup Ethernet LAN? 21. How routers update distances to each of its neighbor? 22. Name few other Network simulators 23. What are advantages of simulation? 24. What are dispatcher, coordinator and nctunsclient? 25. What are drawbacks in distance vector algorithm? 26. What are ephemerical port number and well known port numbers? 27. What are functions of different layers? 28. What are key, ciphertext and plaintext? 29. What are protocols running in different layers? 30. What are Routing algorithms? 31. What are system calls? Mention few of them. 32. What are the other error detection algorithms? 33. What are the parameters of socket()? 34. What is a socket? 35. What is an IP address? 36. What is BER? 37. What is BSS? 38. What is collision? 39. What is cryptography? 40. What is difference between CRC and Hamming code? 41. What is encapsulation? 42. What is FTP? 43. What is generator matrix? 44. What is ICMP? What are uses of ICMP? Name few. 45. What is incoming throughput and outgoing throughput? 46. What is IPC? Name three techniques. 47. What is MAC address? 48. What is meant by bridge? 49. What is meant by congestion window?
19

50. What is meant by file descriptor? 51. What is meant by Gateway? 52. What is meant by hub? 53. What is meant by mobile host? 54. What is meant by NCTUns? 55. What is meant by port? 56. What is meant by router? 57. What is meant by subnet? 58. What is meant by switch? 59. What is meant by syndrome? 60. What is meant by traffic shaping? 61. What is MTU? 62. What is odd parity and even parity? 63. What is ping and telnet? 64. What is private key? 65. What is Protocol Stack? 66. What is public key? 67. What is simulation? 68. What is spanning tree? 69. What is the polynomial used in CRC-CCITT? 70. What is the use of adding header and trailer to frames? 71. Where Pirms algorithm does finds its use in Networks? 72. Which address gets affected if a system moves from one place to another place? 73. Which layer implements security for data? 74. Which layer imposes MTU? 75. Why fragmentation requires? 76. Why frame sorting is required? 77. Why Hamming code is called 7,4 code? 78. Why header is required? 79. Why IP address is required when we have MAC address?

20

Potrebbero piacerti anche