Guest User

igmp multicast test client

a guest
Nov 10th, 2010
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. /* Receiver/client multicast Datagram example. */
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>
  5. #include <netinet/in.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9.  
  10. struct sockaddr_in localSock;
  11. struct ip_mreq group;
  12. int sd;
  13. int datalen;
  14. char databuf[1024];
  15. struct tm *old, *new;
  16.        time_t tloc, newtloc;
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20. /* Create a datagram socket on which to receive. */
  21. sd = socket(AF_INET, SOCK_DGRAM, 0);
  22. if(sd < 0)
  23. {
  24. perror("Opening datagram socket error");
  25. exit(1);
  26. }
  27. else
  28. printf("Opening datagram socket....OK.\n");
  29.  
  30. /* Enable SO_REUSEADDR to allow multiple instances of this */
  31. /* application to receive copies of the multicast datagrams. */
  32. {
  33. int reuse = 1;
  34. if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
  35. {
  36. perror("Setting SO_REUSEADDR error");
  37. close(sd);
  38. exit(1);
  39. }
  40. else
  41. printf("Setting SO_REUSEADDR...OK.\n");
  42. }
  43.  
  44. /* Bind to the proper port number with the IP address */
  45. /* specified as INADDR_ANY. */
  46. memset((char *) &localSock, 0, sizeof(localSock));
  47. localSock.sin_family = AF_INET;
  48. localSock.sin_port = htons(5000);
  49. localSock.sin_addr.s_addr = INADDR_ANY;
  50. if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock)))
  51. {
  52. perror("Binding datagram socket error");
  53. close(sd);
  54. exit(1);
  55. }
  56. else
  57. printf("Binding datagram socket...OK.\n");
  58.  
  59. time(&tloc);
  60. old=localtime(&tloc);
  61. printf("%d:%d:%d- start time!\n", old->tm_hour, old->tm_min, old->tm_sec);
  62.  
  63. /* Join the multicast group 226.1.1.1 on the local 203.106.93.94 */
  64. /* interface. Note that this IP_ADD_MEMBERSHIP option must be */
  65. /* called for each local interface over which the multicast */
  66. /* datagrams are to be received. */
  67. group.imr_multiaddr.s_addr = inet_addr("232.32.0.34");
  68. //group.imr_interface.s_addr = inet_addr("192.168.1.250");
  69. group.imr_interface.s_addr = inet_addr("10.194.180.203");
  70. if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
  71. {
  72. perror("Adding multicast group error");
  73. close(sd);
  74. exit(1);
  75. }
  76. else
  77. printf("Adding multicast group...OK.\n");
  78.  
  79. /* Read from the socket. */
  80. datalen = sizeof(databuf);
  81. while(read(sd, databuf, datalen) > 0)
  82. /*{
  83. perror("Reading datagram message error");
  84. close(sd);
  85. exit(1);
  86. }
  87. else*/
  88. {
  89. //printf("Reading datagram message...OK.\n");
  90. //printf("The message from multicast server is: \"%s\"\n", databuf);
  91. printf(".");
  92. fflush(stdout);
  93. }
  94. time(&tloc);
  95. old=localtime(&tloc);
  96. printf("%d:%d:%d- end time!\n", old->tm_hour, old->tm_min, old->tm_sec);
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment