hawxgamer

multicast sender and receiver toggle

Oct 10th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. #define MAX_LEN 20
  11.  
  12. static int sock;
  13. static int flag_on = 1;
  14. static struct sockaddr_in multicast_addr;
  15. static char message_received[MAX_LEN+1];
  16. static int msgrecv_len;
  17. static struct ip_mreq mc_req;
  18. static char* multicast_ip="224.5.6.7";
  19. static unsigned short multicast_port=5006;
  20. static struct sockaddr_in from_addr;
  21. static unsigned int from_len;
  22.  
  23. static char message_to_send[MAX_LEN];
  24. static unsigned int send_len;
  25. static unsigned char multicast_ttl=1;
  26. static struct sockaddr_in multicast_addr;
  27.  
  28. int sender();
  29. int receiver();
  30.  
  31. int sender()
  32. {
  33. /* create a socket */
  34. if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  35. {
  36. perror("Socket creation failed");
  37. exit(1);
  38. }
  39.  
  40. /* set the TTL (time to live/hop count) for the send */
  41. if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &multicast_ttl, sizeof(multicast_ttl))) < 0)
  42. {
  43. perror("setsockopt() failed");
  44. exit(1);
  45. }
  46.  
  47.  
  48. memset(&multicast_addr, 0, sizeof(multicast_addr));
  49. multicast_addr.sin_family = AF_INET;
  50. multicast_addr.sin_addr.s_addr = inet_addr(multicast_ip);
  51. multicast_addr.sin_port = htons(multicast_port);
  52.  
  53.  
  54. //printf("Type the message below (Press Enter to send, ctrl-C to quit):\n");
  55. printf("NOW IN STATE SENDER!\n");
  56.  
  57. memset(message_to_send, 0, sizeof(message_to_send));
  58.  
  59. //while (fgets(message_to_send, MAX_LEN, stdin))
  60. while(1)
  61. {
  62. //message_to_send="batman";
  63. strcpy(message_to_send,"Hallo\n\0");
  64. printf("message to send: %s \n", message_to_send);
  65. send_len = strlen(message_to_send);
  66.  
  67. if ((sendto(sock, message_to_send, send_len, 0,
  68. (struct sockaddr *) &multicast_addr,
  69. sizeof(multicast_addr))) != send_len)
  70. {
  71. perror("Error in number of bytes");
  72. exit(1);
  73. }
  74.  
  75. memset(message_to_send, 0, sizeof(message_to_send));
  76. break;
  77. }
  78. printf("//switch to receiver()\n");
  79. close(sock);
  80. receiver();
  81. return 0;
  82. }
  83.  
  84. int receiver()
  85. {
  86. printf("NOW IN STATE RECEIVER()\n");
  87.  
  88.  
  89. if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  90. {
  91. perror("socket() failed");
  92. exit(1);
  93. }
  94.  
  95. /* set reuse port to on to allow multiple binds per host */
  96. if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
  97. sizeof(flag_on))) < 0)
  98. {
  99. perror("setsockopt() failed");
  100. exit(1);
  101. }
  102.  
  103. /* construct a multicast address structure */
  104. memset(&multicast_addr, 0, sizeof(multicast_addr));
  105. multicast_addr.sin_family = AF_INET;
  106. multicast_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  107. multicast_addr.sin_port = htons(multicast_port);
  108.  
  109. /* bind to multicast address to socket */
  110. if ((bind(sock, (struct sockaddr *) &multicast_addr, sizeof(multicast_addr))) < 0)
  111. {
  112. perror("bind() failed");
  113. exit(1);
  114. }
  115.  
  116. /* construct an IGMP join request structure */
  117. mc_req.imr_multiaddr.s_addr = inet_addr(multicast_ip);
  118. mc_req.imr_interface.s_addr = htonl(INADDR_ANY);
  119.  
  120. /* send an ADD MEMBERSHIP message via setsockopt */
  121. if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0)
  122. {
  123. perror("setsockopt() failed");
  124. exit(1);
  125. }
  126.  
  127. while(1)
  128. {
  129. memset(message_received, 0, sizeof(message_received));
  130. from_len = sizeof(from_addr);
  131. memset(&from_addr, 0, from_len);
  132.  
  133. /* block waiting to receive a packet */
  134. if ((msgrecv_len = recvfrom(sock, message_received, MAX_LEN, 0, (struct sockaddr*)&from_addr, &from_len)) < 0)
  135. {
  136. perror("recvfrom() failed");
  137. break;
  138. }
  139.  
  140. printf("Received %d bytes from %s: ", msgrecv_len, inet_ntoa(from_addr.sin_addr));
  141. printf("%s", message_received);
  142. if(strcmp(message_received,"haha"))
  143. break;
  144.  
  145. }
  146.  
  147. printf("//switch to sender()\n");
  148. close(sock);
  149. sender();
  150. return 0;
  151. }
  152.  
  153. int main(int argc, char *argv[])
  154. {
  155. /*
  156. if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  157. {
  158. perror("socket() failed");
  159. exit(1);
  160. }
  161.  
  162. set the TTL (time to live/hop count) for the send
  163. if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &multicast_ttl, sizeof(multicast_ttl))) < 0)
  164. {
  165. perror("setsockopt() failed");
  166. exit(1);
  167. }
  168.  
  169. set reuse port to on to allow multiple binds per host
  170. if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
  171. sizeof(flag_on))) < 0)
  172. {
  173. perror("setsockopt() failed");
  174. exit(1);
  175. }
  176.  
  177. bind to multicast address to socket
  178. if ((bind(sock, (struct sockaddr *) &multicast_addr, sizeof(multicast_addr))) < 0)
  179. {
  180. perror("bind() failed");
  181. exit(1);
  182. }
  183. *
  184. * */
  185.  
  186. sender();
  187. //receiver();
  188.  
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment