Advertisement
cyberpunk7925

Untitled

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