Advertisement
cyberpunk7925

Untitled

Dec 12th, 2022
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.41 KB | None | 0 0
  1. PACKET TRACER-"https://gofile.io/d/8nOa4Q"
  2. TCP SOCKET PROGRAMMING
  3.  
  4. TCP SERVER
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <arpa/inet.h>
  11. int main(){
  12.   char *ip = "127.0.0.1";
  13.   int port = 5566;
  14.   int server_sock, client_sock;
  15.   struct sockaddr_in server_addr, client_addr;
  16.   socklen_t addr_size;
  17.   char buffer[1024];
  18.   int n;
  19.   server_sock = socket(AF_INET, SOCK_STREAM, 0);
  20.   if (server_sock < 0){
  21.     perror("[-]Socket error");
  22.     exit(1); }
  23.   printf("[+]TCP server socket created.\n");
  24.  memset(&server_addr, '\0', sizeof(server_addr));
  25.   server_addr.sin_family = AF_INET;
  26.   server_addr.sin_port = port;
  27.   server_addr.sin_addr.s_addr = inet_addr(ip);
  28.   n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
  29. if (n < 0){
  30.     perror("[-]Bind error");
  31.     exit(1);}
  32.   printf("[+]Bind to the port number: %d\n", port);
  33.   listen(server_sock, 5);printf("Listening...\n");
  34.  
  35.  
  36.  while(1){
  37.     addr_size = sizeof(client_addr);
  38.     client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);
  39.     printf("[+]Client connected.\n");
  40.     bzero(buffer, 1024);
  41.     recv(client_sock, buffer, sizeof(buffer), 0);
  42.     printf("Client: %s\n", buffer);
  43.     bzero(buffer, 1024);
  44.     strcpy(buffer, "HI, THIS IS SERVER. HAVE A NICE DAY!!!");
  45.     printf("Server: %s\n", buffer);
  46.     send(client_sock, buffer, strlen(buffer), 0);
  47.     close(client_sock);
  48.     printf("[+]Client disconnected.\n\n"); }
  49.   return 0;
  50. }
  51.  
  52. TCP CLIENT
  53.  
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #include <arpa/inet.h>
  59. int main(){
  60.   char *ip = "127.0.0.1";
  61.   int port = 5566;
  62.    int sock;
  63.   struct sockaddr_in addr;
  64.   socklen_t addr_size;
  65.   char buffer[1024];
  66.   int n;
  67.   sock = socket(AF_INET, SOCK_STREAM, 0);
  68.   if (sock < 0){
  69.     perror("[-]Socket error");
  70.     exit(1);
  71.   }
  72.   printf("[+]TCP server socket created.\n");
  73.   memset(&addr, '\0', sizeof(addr));
  74.   addr.sin_family = AF_INET;
  75.   addr.sin_port = port;
  76.   addr.sin_addr.s_addr = inet_addr(ip);
  77.   connect(sock, (struct sockaddr*)&addr, sizeof(addr));
  78.   printf("Connected to the server.\n");
  79.   bzero(buffer, 1024);
  80.   scanf("%s",buffer);
  81.   printf("enter message:\n");
  82.   printf("Client: %s\n", buffer);
  83.   send(sock, buffer, strlen(buffer), 0);
  84.  
  85.   bzero(buffer, 1024);
  86.   recv(sock, buffer, sizeof(buffer), 0);
  87.   printf("Server: %s\n", buffer);
  88.  
  89.   close(sock);
  90.   printf("Disconnected from the server.\n");
  91.  
  92.   return 0;
  93. }
  94.  
  95. EXECUTION :
  96. OPEN 2 TERMINALS IN SAME FOLDER
  97. first terminal
  98. RUN SERVER FIRST - gcc server.c -o server
  99. ./server
  100. 2nd terminal
  101. RUN client - gcc client.c -o client
  102. ./client
  103.  
  104.  
  105. UDP SERVER
  106.  
  107. #include <stdio.h>
  108. #include <stdlib.h>
  109. #include <string.h>
  110. #include <sys/socket.h>
  111. #include <sys/types.h>
  112. #include <netinet/in.h>
  113. #include <arpa/inet.h>
  114.  
  115. int main(int argc, char **argv){
  116.  
  117.   if (argc != 2){
  118.     printf("Usage: %s <port>\n", argv[0]);
  119.     exit(0);
  120.   }
  121.  
  122.   char *ip = "127.0.0.1";
  123.   int port = atoi(argv[1]);
  124.  
  125.   int sockfd;
  126.   struct sockaddr_in server_addr, client_addr;
  127.   char buffer[1024];
  128.   socklen_t addr_size;
  129.   int n;
  130.  
  131.   sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  132.   if (sockfd < 0){
  133.     perror("[-]socket error");
  134.     exit(1);
  135.   }
  136.  
  137.   memset(&server_addr, '\0', sizeof(server_addr));
  138.   server_addr.sin_family = AF_INET;
  139.   server_addr.sin_port = htons(port);
  140.   server_addr.sin_addr.s_addr = inet_addr(ip);
  141.  
  142.   n = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  143.   if (n < 0) {
  144.     perror("[-]bind error");
  145.     exit(1);
  146.   }
  147.  
  148.   bzero(buffer, 1024);
  149.   addr_size = sizeof(client_addr);
  150.   recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, &addr_size);
  151.   printf("[+]Data recv: %s\n", buffer);
  152.  
  153.   bzero(buffer, 1024);
  154.   strcpy(buffer, "Welcome to the UDP Server.");
  155.   sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, sizeof(client_addr));
  156.   printf("[+]Data send: %s\n", buffer);
  157.  
  158.   return 0;
  159. }
  160.  
  161.  
  162. UDP CLIENT
  163.  
  164. #include <stdio.h>
  165. #include <stdlib.h>
  166. #include <string.h>
  167. #include <sys/socket.h>
  168. #include <sys/types.h>
  169. #include <netinet/in.h>
  170. #include <arpa/inet.h>
  171.  
  172. int main(int argc, char **argv){
  173.  
  174.   if (argc != 2) {
  175.     printf("Usage: %s <port>\n", argv[0]);
  176.     exit(0);
  177.   }
  178.  
  179.   char *ip = "127.0.0.1";
  180.   int port = atoi(argv[1]);
  181.  
  182.   int sockfd;
  183.   struct sockaddr_in addr;
  184.   char buffer[1024];
  185.   socklen_t addr_size;
  186.  
  187.   sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  188.   memset(&addr, '\0', sizeof(addr));
  189.   addr.sin_family = AF_INET;
  190.   addr.sin_port = htons(port);
  191.   addr.sin_addr.s_addr = inet_addr(ip);
  192.  
  193.   bzero(buffer, 1024);
  194.   strcpy(buffer, "Hello, World!");
  195.   sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, sizeof(addr));
  196.   printf("[+]Data send: %s\n", buffer);
  197.  
  198.   bzero(buffer, 1024);
  199.   addr_size = sizeof(addr);
  200.   recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, &addr_size);
  201.   printf("[+]Data recv: %s\n", buffer);
  202.  
  203.   return 0;
  204. }
  205.  
  206. EXECUTION :
  207. OPEN 2 TERMINALS IN SAME FOLDER
  208. first terminal
  209. RUN SERVER FIRST - gcc server.c -o server
  210. ./server 4444
  211. 2nd terminal
  212. RUN client - gcc client.c -o client
  213. ./client 4444
  214.  
  215.  
  216. parity checking
  217.  
  218.  
  219. #include <stdio.h>
  220. #include<string.h>
  221. int main() {
  222.    char a[10] = {0};
  223.    gets(a);
  224.    int ones = 0 ;
  225.    for(int i = 0 ; i < sizeof(a); i++)
  226.    {
  227.        if(a[i] == '1')
  228.        {
  229.            ones = ones + 1 ;
  230.        }
  231.    }
  232.    printf("%d\n",ones);
  233.    if ( ones%2 == 0)
  234.    {
  235.        printf("even parity");
  236.    }
  237.    else
  238.    {
  239.        printf("odd parity");
  240.    }
  241.     return 0;
  242. }
  243.  
  244. leaky bucket algorithm
  245.  
  246. #include <stdio.h>
  247. #define max_size 10
  248. int main() {
  249.     int n,pac_size[max_size],capacity,rate,sent,remn=0;
  250.     printf("enter number of the packets:");
  251.     scanf("%d",&n);
  252.     printf("\nenter bucket input size/capacity: ");
  253.     scanf("%d",&capacity);
  254.     printf("\nenter bucket output size/rate: ");
  255.     scanf("%d",&rate);
  256.     printf("\nenter each packet size:");
  257.     for( int i = 0 ; i < n ; i++)
  258.     {
  259.  
  260.         scanf("%d\n",&pac_size[i]);
  261.     }
  262.     for(int i = 0 ; i < n ; i++)
  263.     {
  264.         printf("packet %d\n",i);
  265.         if(pac_size[i] < capacity)
  266.         {
  267.             printf("packet is accepted into the bucket\n");
  268.             if(pac_size[i] + remn <= rate)
  269.             {
  270.                 sent = pac_size[i] + remn;
  271.                 remn = 0 ;
  272.                 printf("sent = %d\n",sent);
  273.                 printf("remaning = %d",remn);
  274.             }
  275.             else
  276.             {
  277.                 sent = rate;
  278.                 remn = (pac_size[i]+remn)-sent;
  279.                 printf("sent = %d\n",sent);
  280.                 printf("remaning = %d\n",remn);
  281.             }
  282.         }
  283.         else
  284.         {
  285.            printf("packet of size  %d is not accepted as %d is greater than capacity\n",pac_size[i],capacity);
  286.         }
  287.     }
  288.     return 0 ;
  289. }
  290. //CHECKSUM IN C
  291. #include<stdio.h>
  292. int add(int,int);
  293. int com(int);
  294. void main()
  295. {
  296.  int i,j,dl,dil;
  297.  int data1[10],data2[10],newdata[10],comp[10],checksum[10];
  298.  printf("\n Enter the data length=");
  299.  scanf("%d",&dl);
  300.  printf("\n Enter the data1 : \n");
  301.  for(i=0;i<dl;i++)
  302.   scanf("%d",&data1[i]);
  303.   printf("\n Enter the data2 : \n");
  304.  for(i=0;i<dl;i++)
  305.   scanf("%d",&data2[i]);
  306.             for(i=dl-1;i>=0;i--)
  307.             {
  308.             newdata[i]=add(data1[i],data2[i]);
  309.             }
  310.  
  311.  printf("\n\n Data 1        : ");
  312.  for(i=0;i<dl;i++)
  313.             printf("%d",data1[i]);
  314.  printf("\n Data 2        : ");
  315.  for(i=0;i<dl;i++)
  316.             printf("%d",data2[i]);
  317.  
  318.  printf("\n\n The new data is : ");
  319.  for(i=0;i<dl;i++)
  320.  {
  321.    printf("%d",newdata[i]);
  322.  }
  323.   printf("\n Checksum : ");
  324.  for(i=0;i<dl;i++)
  325.  {
  326.    checksum[i]=com(newdata[i]);
  327.    printf("%d",checksum[i]);
  328.  }
  329.  
  330.  printf("\n\n Receiver Side : \n");
  331.  printf("\n Data : ");
  332.   for(i=0;i<dl;i++)
  333.   printf("%d",data1[i]);printf(" ");
  334.    for(i=0;i<dl;i++)
  335.   printf("%d",data2[i]);printf(" ");
  336.    for(i=0;i<dl;i++)
  337.   printf("%d",checksum[i]);
  338.  
  339.   printf("\n Addition : ");
  340.   for(i=dl-1;i>=0;i--)
  341.             {
  342.             newdata[i]=add(newdata[i],checksum[i]);
  343.             }
  344.   for(i=0;i<dl;i++)
  345.   {
  346.    printf("%d",newdata[i]);
  347.   }
  348.   printf("\n  Compliment : ");
  349.   for(i=0;i<dl;i++)
  350.   {
  351.    comp[i]=com(newdata[i]);
  352.    printf("%d",comp[i]);
  353.   }
  354. }
  355.  
  356. int add(int x, int y)
  357. {
  358. static int carry=0;
  359.  if(x==1 && y==1 && carry==0)
  360.  {
  361.  carry=1;
  362.   return 0;
  363.  }
  364.  else if(x==1 && y==1 && carry==1)
  365.  {
  366.  carry=1;
  367.  return 1;
  368.  }
  369.  else if(x==1 && y==0 && carry==0)
  370.   {
  371.   carry=0;
  372.   return 1;
  373.   }
  374.   else if(x==1 && y==0 && carry==1)
  375.   {
  376.   carry=1;
  377.   return 0;
  378.   }
  379.    else if(x==0 && y==1 && carry==0)
  380.   {
  381.   carry=0;
  382.   return 1;
  383.   }
  384.   else if(x==0 && y==1 && carry==1)
  385.   {
  386.   carry=1;
  387.   return 0;
  388.   }
  389.   else if(x==0 && y==0 && carry==0)
  390.   {
  391.   carry=0;
  392.   return 0;
  393.   }
  394.   else
  395.   {
  396.   carry=0;
  397.   return 1;
  398.   }
  399. }
  400. int com(int a)
  401. {
  402. if(a==0)
  403. return 1;
  404. else
  405. return 0;
  406. }
  407.  
  408. //CHECKSUM IN PYTHON
  409. //checksum - python
  410.  
  411. def findChecksum(SentMessage, k):
  412.  
  413.     # Dividing sent message in packets of k bits.
  414.     c1 = SentMessage[0:k]
  415.     c2 = SentMessage[k:2*k]
  416.     c3 = SentMessage[2*k:3*k]
  417.     c4 = SentMessage[3*k:4*k]
  418.  
  419.     # Calculating the binary sum of packets
  420.     Sum = bin(int(c1, 2)+int(c2, 2)+int(c3, 2)+int(c4, 2))[2:]
  421.  
  422.     # Adding the overflow bits
  423.     if(len(Sum) > k):
  424.         x = len(Sum)-k
  425.         Sum = bin(int(Sum[0:x], 2)+int(Sum[x:], 2))[2:]
  426.     if(len(Sum) < k):
  427.         Sum = '0'*(k-len(Sum))+Sum
  428.  
  429.     # Calculating the complement of sum
  430.     Checksum = ''
  431.     for i in Sum:
  432.         if(i == '1'):
  433.             Checksum += '0'
  434.         else:
  435.             Checksum += '1'
  436.     return Checksum
  437.  
  438. # Function to find the Complement of binary addition of
  439. # k bit packets of the Received Message + Checksum
  440.  
  441.  
  442.  
  443. # Driver Code
  444. SentMessage = "10010101011000111001010011101100"
  445. k = 8
  446. #ReceivedMessage = "10000101011000111001010011101001"
  447. ReceivedMessage = "10010101011000111001010011101000"
  448. # Calling the findChecksum() function
  449. Checksum = findChecksum(SentMessage, k)
  450.  
  451. # Calling the checkReceiverChecksum() function
  452. ReceiverChecksum = findChecksum(ReceivedMessage, k)
  453.  
  454. # Printing Checksum
  455. print("SENDER SIDE CHECKSUM: ", Checksum)
  456. print("RECEIVER SIDE CHECKSUM: ", ReceiverChecksum)
  457. if (Checksum == ReceiverChecksum):
  458.    
  459.     print("status:ACCEPTED")
  460. else:
  461.     print("status:ERROR DETECTED");
  462. //
  463.  
  464.  
  465.  
  466.  
  467.  
  468. PACKET TRACER-"https://gofile.io/d/8nOa4Q"
  469.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement