Advertisement
sgbtechsoluation

CN LAB

Dec 12th, 2021
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.78 KB | None | 0 0
  1. //CN LAB
  2. ======================================================================================================================
  3. 1) Write a program for error detecting code using CRC-CCITT (16- bits).
  4.  
  5. //Server
  6. #include<stdio.h>
  7. int main()
  8. {
  9.     int i,j,n=24,dw[24],cw[24],p[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
  10.     printf("Enter the 8 bit data word in binary\n");
  11.     for(i=0;i<8;i++)
  12.         scanf("%d",&dw[i]);
  13.     for(i=0;i<8;i++)
  14.         cw[i]=dw[i];
  15.     for(i=8;i<24;i++)
  16.         cw[i]=0;
  17.     for(i=0;i<8;i++)
  18.         if(cw[i]==1)
  19.             for(j=0;j<17;j++)
  20.                 cw[i+j]=cw[i+j]^p[j];
  21.     for(i=0;i<8;i++)
  22.         cw[i]=dw[i];
  23.     printf("The final code word is \n");
  24.     for(i=0;i<24;i++)
  25.         printf("%d",cw[i]);
  26.     return 0;
  27. }
  28.  
  29.  
  30. /*OUTPUT:
  31. Enter the 8 bit data word in binary
  32. 1 0 0 0 0 0 0 1
  33. The final code word is
  34. 100000011000000110101001
  35. */
  36. //Client
  37. #include<stdio.h>
  38. int main()
  39. {
  40.     int i,j,r[24],cw[24],p[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
  41.     printf("Enter the reviced code word in binary\n");
  42.     for(i=0;i<24;i++)
  43.         scanf("%d",&cw[i]);
  44.     for(i=0;i<8;i++)
  45.         r[i]=cw[i];
  46.     for(i=0;i<8;i++)
  47.         if(cw[i]==1)
  48.             for(j=0;j<17;j++)
  49.                 cw[i+j]=cw[i+j]^p[j];
  50.     for(i=0;i<24;i++)
  51.         if(cw[i]!=0)
  52.         {
  53.             printf("Error in recived data\n");
  54.             exit(0);
  55.         }
  56.     printf("The recived data word \n");
  57.     for(i=0;i<8;i++)
  58.         printf("%d",r[i]);
  59.     printf(" is correct\n");
  60.     return 0;
  61. }
  62. /*OUTPUT:
  63. 1)Enter the reviced code word in binary
  64. 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1
  65. The recived data word
  66. 10000001 is correct
  67.  
  68.  
  69. 2)Enter the reviced code word in binary
  70. 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1
  71. Error in recived data
  72.  
  73. */
  74.  
  75.  
  76. ==========================================================================================================================
  77.  
  78. 2) Write a program for Hamming code generation for error detection and correction.
  79.  
  80. #include<stdio.h>
  81. int main()
  82. {
  83.     int a0,a1,a2,a3,r0,r1,r2,s0,s1,s2;
  84.     printf("Enter the 4 bit data word in binary\n");
  85.     scanf("%d%d%d%d",&a3,&a2,&a1,&a0);
  86.     r0=a2^a1^a0;
  87.     r1=a2^a1^a3;
  88.     r2=a3^a1^a0;
  89.     printf("The final code word is \n");
  90.     printf("%d %d %d %d %d %d %d\n",a3,a2,a1,a0,r2,r1,r0);
  91.     printf("Enter the 7 bit code word in binary\n");
  92.     scanf("%d%d%d%d%d%d%d",&a3,&a2,&a1,&a0,&r2,&r1,&r0);
  93.     s0=a2^a1^a0^r0;
  94.     s1=a2^a1^a3^r1;
  95.     s2=a3^a1^a0^r2;
  96.     if(!(s0||s1||s2))
  97.     {  
  98.         printf("NO error\nRecived data word is \n");
  99.         printf("%d %d %d %d",a3,a2,a1,a0);
  100.     }
  101.     else if((!s2)&&(s1)&&(s0))
  102.     {
  103.         printf("Error at a2 bit and corrected data word is ");
  104.         printf("%d %d %d %d",a3,!a2,a1,a0);
  105.     }
  106.     else if((s2)&&(!s1)&&(s0))
  107.     {
  108.         printf("Error at a0 bit and corrected data word is ");
  109.         printf("%d %d %d %d",a3,a2,a1,!a0);
  110.     }
  111.     else if((s2)&&(s1)&&(!s0))
  112.     {
  113.         printf("Error at a3 bit and corrected data word is ");
  114.         printf("%d %d %d %d",!a3,a2,a1,a0);
  115.     }
  116.     else if(s0&&s1&&s2)
  117.     {
  118.         printf("Error at a1 bit and corrected data word is ");
  119.         printf("%d %d %d %d",a3,a2,!a1,a0);
  120.     }
  121.     return 0;
  122. }
  123. /*OUTPUT:
  124. 1)Enter the 4 bit data word in binary
  125. 0 1 0 0
  126. The final code word is
  127. 0 1 0 0 0 1 1
  128. Enter the 7 bit code word in binary
  129. 1 1 0 0 0 1 1
  130. Error at a3 bit and corrected data word is 0 1 0 0
  131.  
  132. 2)Enter the 4 bit data word in binary
  133. 0 1 0 0
  134. The final code word is
  135. 0 1 0 0 0 1 1
  136. Enter the 7 bit code word in binary
  137. 0 0 0 0 0 1 1
  138. Error at a2 bit and corrected data word is 0 1 0 0
  139.  
  140. 3)Enter the 4 bit data word in binary
  141. 0 1 0 0
  142. The final code word is
  143. 0 1 0 0 0 1 1
  144. Enter the 7 bit code word in binary
  145. 0 1 1 0 0 1 1
  146. Error at a1 bit and corrected data word is 0 1 0 0
  147.  
  148. 4)Enter the 4 bit data word in binary
  149. 0 1 0 0
  150. The final code word is
  151. 0 1 0 0 0 1 1
  152. Enter the 7 bit code word in binary
  153. 0 1 0 1 0 1 1
  154. Error at a0 bit and corrected data word is 0 1 0 0
  155.  
  156. 5)Enter the 4 bit data word in binary
  157. 0 1 0 0
  158. The final code word is
  159. 0 1 0 0 0 1 1
  160. Enter the 7 bit code word in binary
  161. 0 1 0 0 0 1 1
  162. NO error
  163. Recived data word is
  164. 0 1 0 0
  165. */
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. ==========================================================================================================================
  173.  
  174.  
  175.  
  176.  
  177.  
  178. 3) Write a program for even / odd parity checking on binary data.
  179. #include<stdio.h>
  180. int main()
  181. {
  182.     int dw[5],cw[5],i,p,s,ch;
  183.     printf("Sender side\n");
  184.     printf("Enter data word of 4 bits in binary\n");
  185.     for(i=0;i<4;i++)
  186.         scanf("%d",&dw[i]);
  187.     printf("Enter your choice\n1.Even dataword\n2.Odd dataword\n");
  188.     scanf("%d",&ch);
  189.     switch(ch)
  190.     {
  191.         case 1:p=dw[0]^dw[1]^dw[2]^dw[3];
  192.                 dw[4]=p;
  193.                 printf("Code word is\t");
  194.                 for(i=0;i<5;i++)
  195.                     printf("%d\t",dw[i]);
  196.                 printf("\n\nReceiver side\n");
  197.                 printf("Enter code word of 5 bits in binary\n");
  198.                 for(i=0;i<5;i++)
  199.                     scanf("%d",&cw[i]);
  200.                 s=cw[0]^cw[1]^cw[2]^cw[3]^cw[4];
  201.                 if(s==0)
  202.                 {
  203.                     printf("Code word is received correctly\n");
  204.                     printf("Data word is\t");
  205.                     for(i=0;i<4;i++)
  206.                         printf("%d\t",cw[i]);
  207.                     printf("\n");
  208.                 }
  209.                 else
  210.                     printf("Code word is incorrect\n");
  211.                                     break;
  212.         case 2:p=dw[0]^dw[1]^dw[2]^dw[3];
  213.                 dw[4]=!p;
  214.                 printf("Code word is\t");
  215.                 for(i=0;i<5;i++)
  216.                     printf("%d\t",dw[i]);
  217.                 printf("\n\nReceiver side\n");
  218.                 printf("Enter code word of 5 bits in binary\n");
  219.                 for(i=0;i<5;i++)
  220.                     scanf("%d",&cw[i]);
  221.                 s=!(cw[0]^cw[1]^cw[2]^cw[3]^cw[4]);
  222.                 if(s==0)
  223.                 {
  224.                     printf("Code word is received correctly\n");
  225.                     printf("Data word is\t");
  226.                     for(i=0;i<4;i++)
  227.                         printf("%d\t",cw[i]);
  228.                     printf("\n");
  229.                 }
  230.                 else
  231.                     printf("Code word is incorrect\n");
  232.                                     break;
  233.         default:printf("Invalid choice\n");
  234.                     return 0;
  235.     }
  236.     return 0;
  237. }
  238. /* 1) Sender side
  239. Enter data word of 4 bits in binary
  240. 1 0 1 0
  241. Enter your choice
  242. 1.Even dataword
  243. 2.Odd dataword
  244. 1
  245. Code word is    1       0       1       0       0      
  246.  
  247. Receiver side
  248. Enter code word of 5 bits in binary
  249. 1 0 1 0 0
  250. Code word is received correctly
  251. Data word is    1       0       1       0
  252.  
  253. 2)Sender side
  254. Enter data word of 4 bits in binary
  255. 1 1 0 1
  256. Enter your choice
  257. 1.Even dataword
  258. 2.Odd dataword
  259. 2
  260. Code word is    1       1       0       1       0      
  261.  
  262. Receiver side
  263. Enter code word of 5 bits in binary
  264. 1 1 0 1 0
  265. Code word is received correctly
  266. Data word is    1       1       0       1
  267.  
  268. 3)Sender side
  269. Enter data word of 4 bits in binary
  270. 1 1 0 0
  271. Enter your choice
  272. 1.Even dataword
  273. 2.Odd dataword
  274. 1
  275. Code word is    1       1       0       0       0      
  276.  
  277. Receiver side
  278. Enter code word of 5 bits in binary
  279. 1 1 1 0 0
  280. Code word is incorrect
  281.  
  282. 4)Sender side
  283. Enter data word of 4 bits in binary
  284. 1 1 1 0
  285. Enter your choice
  286. 1.Even dataword
  287. 2.Odd dataword
  288. 2
  289. Code word is    1       1       1       0       0      
  290.  
  291. Receiver side
  292. Enter code word of 5 bits in binary
  293. 1 1 0 0 0
  294. Code word is incorrect
  295. 5) Sender side
  296. Enter data word of 4 bits in binary
  297. 1 1 0 1
  298. Enter your choice
  299. 1.Even dataword
  300. 2.Odd dataword
  301. 3
  302. Invalid choice*/
  303.  
  304. ==========================================================================================================================
  305.  
  306. 4) Write a program to perform stuffing and destuffing on given information.
  307. #include<stdio.h>
  308. #include<string.h>
  309. int main()
  310. {
  311.     char a[50];
  312.     int ch,i,j=0,k;
  313.     printf("Enter your choice\n1.Stuffing\n2.Destuffing\n");
  314.     scanf("%d",&ch);
  315.     switch(ch)
  316.     {
  317.         case 1: printf("Enter data for stuffing\n");
  318.                 scanf("%s",a);
  319.                 for(i=0;i<strlen(a);i++)
  320.                 {
  321.                     if(a[i]=='0')
  322.                         j=0;
  323.                     if(a[i]=='1')
  324.                         j++;
  325.                     if(j==5)
  326.                     {   for(k=strlen(a);k>i;k--)
  327.                             a[k+1]=a[k];
  328.                         a[i+1]='0';
  329.                     }
  330.                    
  331.                 }
  332.                 printf("The stuffed data is = %s\n",a);
  333.                 break;
  334.         case 2:printf("Enter data for destuffing\n");
  335.                 scanf("%s",a);
  336.                 for(i=0;i<strlen(a);i++)
  337.                 {
  338.                     if(a[i]=='0')
  339.                         j=0;
  340.                     if(a[i]=='1')
  341.                         j++;
  342.                     if(j==5)
  343.                     {   for(k=i+1;k<=strlen(a);k++)
  344.                             a[k]=a[k+1];
  345.                         j=0;
  346.                     }
  347.                 }
  348.                 printf("The destuffed data is = %s\n",a);
  349.                 break;
  350.         default:printf("Invalid choice\n");
  351.     }
  352.     return 0;
  353. }
  354.  
  355.  
  356. /*OUTPUT:
  357. 1) Enter your choice
  358. 1.Stuffing
  359. 2.Destuffing
  360. 1
  361. Enter data for stuffing
  362. 11110111111011111
  363. The stuffed data is = 1111011111010111110
  364.  
  365. 2)Enter your choice
  366. 1.Stuffing
  367. 2.Destuffing
  368. 2
  369. Enter data for destuffing
  370. 0011111010111110111
  371. The destuffed data is = 00111111011111111
  372. */
  373.  
  374.  
  375.  
  376.  
  377. ==========================================================================================================================
  378.  
  379.  
  380.  
  381. 5) Write a program for distance vector algorithm to find suitable path for transmission.
  382. #include<stdio.h>
  383. struct node
  384. {
  385.     int dist[20],from[20];
  386. }rt[10];
  387. int main()
  388. {
  389.     int c[20][20],s,d,arr[20],n,i,j,s1,k,count=0;
  390.     printf("Enter the number of nodes\n");
  391.     scanf("%d",&n);
  392.     printf("Enter the cost of each node\n");
  393.     for(i=0;i<n;i++)
  394.     for(j=0;j<n;j++)
  395.     {
  396.         scanf("%d",&c[i][j]);
  397.         rt[i].dist[j]=c[i][j];
  398.         rt[i].from[j]=j;
  399.     }
  400.     for(i=0;i<n;i++)
  401.     {
  402.         printf("Routing information of routing %d\n",i);
  403.         printf("Source\tDestination\tVia\tCost\n");
  404.         for(j=0;j<n;j++)
  405.             printf("\n%d\t%d\t\t%d\t%d\n",i,j,rt[i].from[j],rt[i].dist[j]);
  406.     }
  407.    
  408.     do
  409.     {
  410.         count=0;
  411.         for(i=0;i<n;i++)
  412.         for(j=0;j<n;j++)
  413.         for(k=0;k<n;k++)
  414.         if(rt[i].dist[j]>(c[i][k]+rt[k].dist[j]))
  415.         {
  416.             rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
  417.             rt[i].from[j]=k;
  418.             count++;
  419.         }
  420.     }while(count!=0);
  421.     printf("\nAfter updating\n");
  422.     for(i=0;i<n;i++)
  423.     {
  424.         printf("Routing information of router %d\n",i);
  425.         printf("Source\tDestination\tVia\tCost\n");
  426.         for(j=0;j<n;j++)
  427.             printf("\n%d\t%d\t\t%d\t%d\n",i,j,rt[i].from[j],rt[i].dist[j]);
  428.     }
  429.     printf("Enter the source node\n");
  430.     scanf("%d",&s);
  431.     s1=s;
  432.     printf("Enter the desination node\n");
  433.     scanf("%d",&d);
  434.     printf("The main cost from router %d to router %d = %d",s,d,rt[s].dist[d]);
  435.     arr[0]=s;
  436.     i=1;
  437.     while(s!=d)
  438.     {
  439.         arr[i]=rt[s].from[d];
  440.         s=rt[s].from[d];
  441.         i++;
  442.     }
  443.     printf("The shortest path from router %d to router %d \n",s1,d);
  444.     for(j=0;j<i-1;j++)
  445.         printf("%d -->",arr[j]);
  446.     printf("%d\n",arr[j]);
  447.     return 0;
  448. }
  449. /*OOUTPUT:
  450.  Enter the number of nodes
  451. 4
  452. Enter the cost of each node
  453. 0 2 4 9999
  454. 2 0 4 3
  455. 4 4 0 5
  456. 9999 3 5 0
  457. Routing information of routing 0
  458. Source  Destination     Via     Cost
  459.  
  460. 0       0               0       0
  461.  
  462. 0       1               1       2
  463.  
  464. 0       2               2       4
  465.  
  466. 0       3               3       9999
  467. Routing information of routing 1
  468. Source  Destination     Via     Cost
  469.  
  470. 1       0               0       2
  471.  
  472. 1       1               1       0
  473.  
  474. 1       2               2       4
  475.  
  476. 1       3               3       3
  477. Routing information of routing 2
  478. Source  Destination     Via     Cost
  479.  
  480. 2       0               0       4
  481.  
  482. 2       1               1       4
  483.  
  484. 2       2               2       0
  485.  
  486. 2       3               3       5
  487. Routing information of routing 3
  488. Source  Destination     Via     Cost
  489.  
  490. 3       0               0       9999
  491.  
  492. 3       1               1       3
  493.  
  494. 3       2               2       5
  495.  
  496. 3       3               3       0
  497.  
  498. After updating
  499. Routing information of router 0
  500. Source  Destination     Via     Cost
  501.  
  502. 0       0               0       0
  503.  
  504. 0       1               1       2
  505.  
  506. 0       2               2       4
  507.  
  508. 0       3               1       5
  509. Routing information of router 1
  510. Source  Destination     Via     Cost
  511.  
  512. 1       0               0       2
  513.  
  514. 1       1               1       0
  515.  
  516. 1       2               2       4
  517.  
  518. 1       3               3       3
  519. Routing information of router 2
  520. Source  Destination     Via     Cost
  521.  
  522. 2       0               0       4
  523.  
  524. 2       1               1       4
  525.  
  526. 2       2               2       0
  527.  
  528. 2       3               3       5
  529. Routing information of router 3
  530. Source  Destination     Via     Cost
  531.  
  532. 3       0               1       5
  533.  
  534. 3       1               1       3
  535.  
  536. 3       2               2       5
  537.  
  538. 3       3               3       0
  539. Enter the source node
  540. 0
  541. Enter the desination node
  542. 3
  543. The main cost from router 0 to router 3 = 5The shortest path from router 0 to ro
  544. uter 3
  545. 0 -->1 -->3
  546. */ 
  547.  
  548.  
  549.  
  550. ==========================================================================================================================
  551.  
  552.  
  553.  
  554.  
  555.  
  556. 6) Write a program for congestion control using leaky bucket algorithm
  557. #include<stdio.h>
  558. int main()
  559. {
  560.     int i,t,c,b[50],op,sum=0,s=0,m;
  561.     printf("Enter the time in sec:");
  562.     scanf("%d",&t);
  563.     printf("\nEnter the capacity of bucket:");
  564.     scanf("%d",&c);
  565.     printf("\nEnter the incoming bandwidth:\nTime in sec\tBandwith in MBPS\n");
  566.     for(i=0;i<t;i++)
  567.     {
  568.         printf("\n%d\t\t",i+1);
  569.         scanf("%d",&b[i]);
  570.         if((sum+b[i])<=c)
  571.             sum+=b[i];
  572.         else
  573.             printf("\t packet %d is rejected",i+1);
  574.     }
  575.     printf("Enter the operate:");
  576.     scanf("%d",&op);
  577.     printf("\nOutput transfer of leaky bucket\nTime in sec\tBandwith in MBPS\n");
  578.     m=sum/op;
  579.     i=0;
  580.     for(i=0;i<t;i++)
  581.     {
  582.         if(i<m)
  583.         {
  584.             printf("%d\t\t%d\n",i+1,op);
  585.             s+=op;
  586.         }
  587.         else if(s<sum)
  588.         {
  589.             printf("%d\t\t%d\n",i+1,sum%op);
  590.             s+=sum%op;
  591.         }
  592.         else
  593.             printf("%d\t\t%d\n",i+1,0);
  594.     }
  595.     return 0;
  596. }
  597. /*OUTPUT:
  598. 1)Enter the time in sec:10
  599. Enter the capacity of bucket:30
  600. Enter the incoming bandwidth:
  601. Time in sec     Bandwith in MBPS
  602.  
  603. 1               12
  604.  
  605. 2               3
  606.  
  607. 3               2
  608.  
  609. 4               0
  610.  
  611. 5               50
  612.          packet 5 is rejected
  613. 6               0
  614.  
  615. 7               0
  616.  
  617. 8               0
  618.  
  619. 9               0
  620.  
  621. 10              0
  622. Enter the operate:2
  623.  
  624. Output transfer of leaky bucket
  625. Time in sec     Bandwith in MBPS
  626. 1               2
  627. 2               2
  628. 3               2
  629. 4               2
  630. 5               2
  631. 6               2
  632. 7               2
  633. 8               2
  634. 9               1
  635. 10              0
  636.  
  637. 2) Enter the time in sec:10
  638. Enter the capacity of bucket:40
  639. Enter the incoming bandwidth:
  640. Time in sec     Bandwith in MBPS
  641.  
  642. 1               15
  643.  
  644. 2               10
  645.  
  646. 3               0
  647.  
  648. 4               0
  649.  
  650. 5               5
  651.  
  652. 6               1
  653.  
  654. 7               0
  655.  
  656. 8               0
  657.  
  658. 9               0
  659.  
  660. 10              2
  661. Enter the operate:3
  662.  
  663. Output transfer of leaky bucket
  664. Time in sec     Bandwith in MBPS
  665. 1               3
  666. 2               3
  667. 3               3
  668. 4               3
  669. 5               3
  670. 6               3
  671. 7               3
  672. 8               3
  673. 9               3
  674. 10              3
  675.  
  676.  
  677. */
  678.  
  679. ==========================================================================================================================
  680. 7) Using TCP/IP sockets, write a client – server program to make the client send the file name and to make the server send back the contents of the requested file if present.
  681. //server.c
  682. #include<stdio.h>
  683. #include<string.h>
  684. #include<stdlib.h>
  685. #include<errno.h>
  686. #include<string.h>
  687. #include<fcntl.h>
  688. #include<sys/types.h>
  689. #include<sys/ioctl.h>
  690. #include<net/if_arp.h>
  691. #include<sys/socket.h>
  692. #include<unistd.h>
  693. #include<netinet/in.h>
  694. #include<netdb.h>
  695. #include<arpa/inet.h>
  696. #define MAX 25
  697. #define MAXBUFF 100
  698.  
  699. int main()
  700. {
  701.     int fd,n,b;
  702.     char buf1[MAXBUFF], buf[MAX];
  703.     int sersock,clisock;
  704.     struct sockaddr_in seraddr,cli_info;
  705.     socklen_t addr_size;
  706.     sersock=socket(AF_INET,SOCK_STREAM,0);
  707.     if(sersock!=-1)
  708.         printf("socket created\n");
  709.     else
  710.         printf("socket not created\n");
  711.     seraddr.sin_family=AF_INET;
  712.     seraddr.sin_port=htons(2500);
  713.     b=bind(sersock,(struct sockaddr*)&seraddr,sizeof(seraddr));
  714.     if(b==0)
  715.         printf("binded successfully\n");
  716.     else
  717.         printf("binding failed\n");
  718.     listen(sersock,5);
  719.     addr_size=sizeof(cli_info);
  720.     clisock=accept(sersock,(struct sockaddr*)&cli_info ,&addr_size);
  721.     n=read(clisock,buf,MAX);
  722.     buf[n]='\0';
  723.     printf("the filereceived by client is %s=\n",buf);
  724.     fd=open(buf,0,O_RDONLY);
  725.     if(fd<0)
  726.     {
  727.         printf("file open error\n");
  728.         strcpy(buf,"file open error");
  729.         n=strlen(buf);
  730.         buf[n]='\0';
  731.         write(clisock,buf,n);
  732.     }
  733.     while((n=read(fd,buf1,MAXBUFF))>0)
  734.     {
  735.         write(clisock,buf1,n);
  736.     }
  737.     printf("\n");
  738.     close(clisock);
  739.     return 0;
  740. }
  741. //client
  742. #include<stdio.h>
  743. #include<string.h>
  744. #include<stdlib.h>
  745. #include<errno.h>
  746. #include<string.h>
  747. #include<fcntl.h>
  748. #include<sys/types.h>
  749. #include<sys/ioctl.h>
  750. #include<net/if_arp.h>
  751. #include<sys/socket.h>
  752. #include<unistd.h>
  753. #include<netinet/in.h>
  754. #include<netdb.h>
  755. #define MAXBUFF 1000
  756. #define MAX 25
  757. int main()
  758. {
  759.     int n,clisock,c;
  760.     char buf1[MAXBUFF];
  761.     char buffer[1024];
  762.     socklen_t addr_size;
  763.     struct sockaddr_in seraddr;
  764.     clisock=socket(AF_INET,SOCK_STREAM,0);
  765.     seraddr.sin_family=AF_INET;
  766.     seraddr.sin_port=htons(2500);
  767.     addr_size=sizeof(seraddr);
  768.     c=connect(clisock,(struct sockaddr*)&seraddr,addr_size);
  769.     printf("c=%d",c);
  770.     if(c==0)
  771.         printf("connected to server\n");
  772.     else
  773.         printf("connection failed\n");
  774.     printf("enter the file name\n");
  775.     scanf("%s",buffer);
  776.     write(clisock,buffer,strlen(buffer));
  777.     printf("file contents are\n");
  778.     n=read(clisock,buf1,MAXBUFF);
  779.     write(1,buf1,n);
  780.     printf("\n");
  781.     return 0;
  782. }
  783. /*OUTPUT:
  784. gcc ser.c
  785. ./a.out
  786. socket created
  787. binded successfully
  788. the filereceived by client is abc.txt=
  789.  
  790. gcc cli.c
  791. ./a.out
  792. c=0connected to server
  793. enter the file name
  794. abc.txt
  795. file contents are
  796. hi hello
  797.        
  798. */                       
  799.  
  800.  
  801.  
  802. ==========================================================================================================================
  803.  
  804.  
  805. 9) Implement the  program 7 using the FIFO  IPC channels:
  806. //fifoserver.c
  807. #include<stdio.h>
  808. #include<sys/types.h>
  809. #include<sys/stat.h>
  810. #include<fcntl.h>
  811. #include<errno.h>
  812. #include<string.h>
  813. #include<stdlib.h>
  814. #include<unistd.h>
  815. #define FIFO1  "fifo1"
  816. #define FIFO2  "fifo2"
  817. #define PERMS 0666
  818. char fname[256];
  819. int main()
  820. {
  821.     int readfd,writefd,fd;
  822.     char buff[512];
  823.     ssize_t n;
  824.     if(mkfifo(FIFO1,PERMS)<0||mkfifo(FIFO2,PERMS)<0)
  825.     {
  826.         printf("cant create file\n");
  827.         exit(0);
  828.     }
  829.     printf("waiting for connection request\n");
  830.     readfd=open(FIFO1,O_RDONLY,0);
  831.     writefd=open(FIFO2,O_WRONLY,0);
  832.     printf("Connection Established.....\n");
  833.     read(readfd,fname,255);
  834.     printf("Client has requested the file %s\n",fname);
  835.     if((fd=open(fname,O_RDWR))<0)
  836.     {
  837.         strcpy(buff,"File does not exist..\n");
  838.         write(writefd,buff,strlen(buff));
  839.     }
  840.     else
  841.     {
  842.         while((n=read(fd,buff,512))>0)
  843.             write(writefd,buff,n);
  844.     }
  845.     close(readfd);
  846.     close(writefd);
  847.     unlink(FIFO1);
  848.     unlink(FIFO2);
  849.     return 0;
  850. }  
  851. //client
  852. #include<stdio.h>
  853. #include<sys/types.h>
  854. #include<sys/stat.h>
  855. #include<fcntl.h>
  856. #include<errno.h>
  857. #include<string.h>
  858. #include<stdlib.h>
  859. #include<unistd.h>
  860. #define FIFO1  "fifo1"
  861. #define FIFO2  "fifo2"
  862. #define PERMS 0666
  863. char fname[256];
  864. int main()
  865. {
  866.     int readfd,writefd,fd;
  867.     char buff[512];
  868.     ssize_t n;
  869.     if(mkfifo(FIFO1,PERMS)<0||mkfifo(FIFO2,PERMS)<0)
  870.     {
  871.         printf("cant create file\n");
  872.         exit(0);
  873.     }
  874.     printf("waiting for connection request\n");
  875.     readfd=open(FIFO1,O_RDONLY,0);
  876.     writefd=open(FIFO2,O_WRONLY,0);
  877.     printf("Connection Established.....\n");
  878.     read(readfd,fname,255);
  879.     printf("Client has requested the file %s\n",fname);
  880.     if((fd=open(fname,O_RDWR))<0)
  881.     {
  882.         strcpy(buff,"File does not exist..\n");
  883.         write(writefd,buff,strlen(buff));
  884.     }
  885.     else
  886.     {
  887.         while((n=read(fd,buff,512))>0)
  888.             write(writefd,buff,n);
  889.     }
  890.     close(readfd);
  891.     close(writefd);
  892.     unlink(FIFO1);
  893.     unlink(FIFO2);
  894.     return 0;
  895. }  
  896. /*OUTPUT:
  897. gcc fifoserver.c
  898. ./a.out
  899. waiting for connection request
  900. Connection Established.....
  901. Client has requested the file abc.txt
  902.  
  903. gcc fifoclient.c
  904. ./a.out
  905. Trying to connect to server...
  906. Connection Established.....
  907. Enter the file name to request from server
  908. abc.txt
  909. Waiting for server to reply..
  910. Contents of the file are...
  911. hi hello
  912.        
  913. */                       
  914.          
  915.        
  916.                          
  917.          
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927. ==========================================================================================================================
  928.  
  929.  
  930.  
  931.  
  932.  
  933. 10) Write a program for simple RSA algorithm to encrypt and decrypt the data.
  934.  
  935. #include<stdio.h>
  936. #include<math.h>
  937. int phi,d,c,m,e,n;
  938. void encrypt()
  939. {
  940.     int i,c=1;
  941.     for(i=0;i<e;i++)
  942.         c=c*m%n;
  943.     c=c%n;
  944.     printf("Encrypted data is = %d\n",c);
  945. }
  946. void decrypt()
  947. {
  948.     int i,m=1;
  949.     for(i=0;i<d;i++)
  950.         m=m*c%n;
  951.     m=m%n;
  952.     printf("Decrypted data is = %d\n",m);
  953. }  
  954. int main()
  955. {
  956.     int p,q,s;
  957.     printf("Enter two large numbers\n");
  958.     scanf("%d%d",&p,&q);
  959.     n=p*q;
  960.     phi=(p-1)*(q-1);
  961.     printf("phi(%d)=%d\n",n,phi);
  962.     printf("Enter the exponent value\n");
  963.     scanf("%d",&e);
  964.     d=e;
  965.     do
  966.     {
  967.         s=(d*e)%phi;
  968.         d++;
  969.     }while(s!=1);
  970.     d=d-1;
  971.     printf("Public key is {%d,%d}\n",e,n);
  972.     printf("Private key is {%d,%d}\n",d,n);
  973.     printf("Enter plain text\n");
  974.     scanf("%d",&m);
  975.     encrypt();
  976.     printf("Enter the cipher text\n");
  977.     scanf("%d",&c);
  978.     decrypt();
  979.     return(0);
  980. }
  981.        
  982.                          
  983.          
  984. /*OUTPUT:
  985. 1)Enter two large numbers
  986. 53
  987. 67
  988. phi(3551)=3432
  989. Enter the exponent value
  990. 47
  991. Public key is {47,3551}
  992. Private key is {3359,3551}
  993. Enter plain text
  994. 99
  995. Encrypted data is = 1793
  996. Enter the cipher text
  997. 1793
  998. Decrypted data is = 99
  999.  
  1000. 2) Enter two large numbers
  1001. 67
  1002. 71
  1003. phi(4757)=4620
  1004. Enter the exponent value
  1005. 23
  1006. Public key is {23,4757}
  1007. Private key is {1607,4757}
  1008. Enter plain text
  1009. 69
  1010. Encrypted data is = 610
  1011. Enter the cipher text
  1012. 610
  1013. Decrypted data is = 69
  1014. */
  1015.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement