This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

synchronize between reader and writer sockets

By: leeyee on Apr 20th, 2012  |  syntax: C  |  size: 2.57 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // contruct controlling context of UDP peers
  2. CTRL_CTX *c_ctx = malloc(sizeof(CTRL_CTX));
  3. c_ctx->in_serving = 0;
  4. c_ctx->peerFirst = c_ctx->peerLast = NULL;
  5.  
  6. int ctrl_sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  7. int data_sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  8. char buffer[BUFFER_SIZE];
  9. ssize_t recsize;
  10. socklen_t udp_clilen;
  11.  
  12. struct sockaddr_in ctrl_saddr, data_saddr, udp_caddr;
  13. udp_clilen = sizeof(udp_caddr);
  14.  
  15. // construct and bind the controlling socket
  16. memset(&ctrl_saddr, 0, sizeof(ctrl_saddr));
  17. ctrl_saddr.sin_family = AF_INET;
  18. ctrl_saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  19. ctrl_saddr.sin_port = htons(7654);
  20.  
  21. if (bind(ctrl_sockfd,(struct sockaddr *) &ctrl_saddr, sizeof(ctrl_saddr)) == -1)
  22. {
  23.     perror("error: udp bind failed");
  24.     close(ctrl_sockfd);
  25.     exit(EXIT_FAILURE);
  26. }
  27.  
  28. // construct and bind the data tranmitting socket
  29. memset(&data_saddr, 0, sizeof(data_saddr));
  30. data_saddr.sin_family = AF_INET;
  31. data_saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  32. data_saddr.sin_port = htons(7655);
  33. if (bind(data_sockfd,(struct sockaddr *) &data_saddr, sizeof(data_saddr)) == -1)
  34. {
  35.     perror("error: udp bind failed");
  36.     close(data_sockfd);
  37.     exit(EXIT_FAILURE);
  38. }
  39.  
  40. /*
  41.  * initialize the file descriptor set
  42.  * to monitor the controlling socket use select()
  43.  */
  44. int rc;
  45. struct pollfd ctrl_pfd;
  46. ctrl_pfd.fd = ctrl_sockfd;
  47. ctrl_pfd.events = POLLRDNORM;
  48. int npfd = 1;
  49.        
  50. for (;;)
  51. {
  52.     rc = poll(&ctrl_pfd, npfd, -1);
  53.     if (rc < 0)
  54.     {
  55.         perror("error: poll failed");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.  
  59.     if (ctrl_pfd.revents & POLLRDNORM)
  60.     {
  61.         printf("controlling socket is available to read.\n");
  62.         recsize = recvfrom(ctrl_sockfd, (void *)buffer, BUFFER_SIZE, 0, (struct sockaddr *) &udp_caddr, &udp_clilen);
  63.         if (recsize < 0)
  64.         {
  65.             fprintf(stderr, "%s\n", strerror(errno));
  66.             exit(EXIT_FAILURE);
  67.         }
  68.         else if (strncmp(buffer, "REQ", 3) == 0)
  69.         {
  70.             // a new client, record its information
  71.             register_new_peer();
  72.         }
  73.         else if (strncmp(buffer, "STP", 3) == 0)
  74.         {
  75.             // a client finished, ask to stop
  76.             de_register_a_peer();
  77.         }
  78.         else
  79.             printf("no information of of the client is found.\n");
  80.     }
  81.     /* the data socket is used to send packets for clients
  82.      * that have a record in the control context
  83.      */
  84.     PEER_CTX *peer = c_ctx->peerFirst;
  85.     while (peer->peerNext != NULL)
  86.     {
  87.         // traverse the peers list, and send a packet
  88.         send_ncpkt(data_sockfd, (struct sockaddr *) &(peer->cli_addr), peer->cli_salen, s_ctx);
  89.         peer = peer->peerNext;
  90.     }
  91. }
clone this paste RAW Paste Data