SHARE
TWEET

PAHeadsOrTailsNotes

a guest Dec 12th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * @file main.c
  3. * @brief Description
  4. * @date 2018-1-1
  5. * @author name of author
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <time.h>
  16. #include <inttypes.h>
  17.  
  18. #include "debug.h"
  19. #include "memory.h"
  20.  
  21. int main(int argc, char *argv[]){
  22.     /* Disable warnings */
  23.     (void)argc; (void)argv;
  24.  
  25.     /* Main code */
  26.     DEBUG("Main is empty -- add code here");
  27.  
  28.     srand(time(NULL));
  29.  
  30.     uint32_t flips_result = 0;
  31.  
  32.     uint8_t num_flips = 6;
  33.  
  34.     for (uint8_t i = 0; i < num_flips; ++i) {
  35.         int random_number = rand() % 2;
  36.         if(random_number != 0){
  37.             flips_result |= (1 << i);
  38.         }
  39.     }
  40.  
  41.     printf("flips_result: 0x%x\n", flips_result);
  42.  
  43.     for (uint8_t i = 0; i < num_flips; ++i) {
  44.         if ((flips_result & (1 << i)) == 0) {
  45.             printf("[%02hhu] Tails\n", i + 1);
  46.         }else{
  47.             printf("[%02hhu] Heads\n", i + 1);
  48.         }
  49.     }
  50.  
  51.  
  52.     ssize_t udp_read_bytes, udp_sent_bytes;
  53.  
  54.     uint8_t number_of_flips = 10;
  55.     if ((udp_sent_bytes = sendto(
  56.         udp_client_socket,
  57.         &number_of_flips,
  58.         sizeof(number_of_flips),
  59.         0,
  60.         (struct sockaddr *) &udp_server_endpoint,
  61.         udp_server_endpoint_length)) == -1)
  62.         ERROR(24, "Can't sendto server");
  63.     printf("ok.  (%d bytes sent)\n", (int)udp_sent_bytes);
  64.  
  65.     uint8_t received_number_of_flips = 0;
  66.     if ((udp_read_bytes = recvfrom(
  67.         udp_client_socket,
  68.         &received_number_of_flips,
  69.         sizeof(received_number_of_flips),
  70.         0,
  71.         (struct sockaddr *) &udp_server_endpoint,
  72.         &udp_server_endpoint_length)) == -1)
  73.         ERROR(25, "Can't recvfrom server");
  74.     printf("ok.  (%d bytes received)\n", (int)udp_read_bytes);
  75.  
  76. #define ERROR_CODE 0x00ffffff
  77.  
  78.     #define MIN_NUMBER_OF_FLIPS 1
  79.     #define MAX_NUMBER_OF_FLIPS 16
  80.  
  81.     if (received_number_of_flips < MIN_NUMBER_OF_FLIPS ||
  82.         received_number_of_flips > MAX_NUMBER_OF_FLIPS)
  83.     {
  84.         uint32_t error_code = ERROR_CODE;
  85.  
  86.         if (flips_result == ERROR_CODE)
  87.         {
  88.             fprintf(stderr, "Number of flips not supported\n");
  89.             exit(10);
  90.         }
  91.     }
  92.  
  93.     while(1){
  94.  
  95.         recvfrom
  96.  
  97.         sendto
  98.  
  99.     }
  100.  
  101.     close(socket)
  102.  
  103.     uint32_t flips_results_send = flips_result;
  104.  
  105.     flips_results_send = htonl(flips_results_send);
  106.  
  107.     if ((udp_sent_bytes = sendto(
  108.         udp_client_socket,
  109.         &flips_results_send,
  110.         sizeof(flips_results_send),
  111.         0,
  112.         (struct sockaddr *) &udp_server_endpoint,
  113.         udp_server_endpoint_length)) == -1)
  114.         ERROR(24, "Can't sendto server");
  115.     printf("ok.  (%d bytes sent)\n", (int)udp_sent_bytes);
  116.  
  117.     uint32_t received_flips_result = 0;
  118.     if ((udp_read_bytes = recvfrom(
  119.         udp_client_socket,
  120.         &received_flips_result,
  121.         sizeof(received_flips_result),
  122.         0,
  123.         (struct sockaddr *) &udp_server_endpoint,
  124.         &udp_server_endpoint_length)) == -1)
  125.         ERROR(25, "Can't recvfrom server");
  126.     printf("ok.  (%d bytes received)\n", (int)udp_read_bytes);
  127.  
  128.     received_flips_result = ntohl(received_flips_result);
  129.  
  130.     printf("%u\n", received_flips_result);
  131.  
  132.  
  133.  
  134.  
  135.     return 0;
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top