COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
mcastserver.c File Reference
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for mcastserver.c:

Functions

int main (int argc, char *argv[])
 

Variables

struct in_addr localInterface
 
struct sockaddr_in groupSock
 
int sd
 
char databuf [1024] = "Multicast test message lol!"
 
int datalen = sizeof(databuf)
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)
44 {
45 /* Create a datagram socket on which to send. */
46 sd = socket(AF_INET, SOCK_DGRAM, 0);
47 if(sd < 0)
48 {
49  perror("Opening datagram socket error");
50  exit(1);
51 }
52 else
53  printf("Opening the datagram socket...OK.\n");
54 
55 /* Initialize the group sockaddr structure with a */
56 /* group address of 225.1.1.1 and port 5555. */
57 memset((char *) &groupSock, 0, sizeof(groupSock));
58 groupSock.sin_family = AF_INET;
59 groupSock.sin_addr.s_addr = inet_addr("225.1.1.1");
60 groupSock.sin_port = htons(4321);
61 
62 /* Disable loopback so you do not receive your own datagrams.
63 {
64 char loopch = 0;
65 if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof(loopch)) < 0)
66 {
67 perror("Setting IP_MULTICAST_LOOP error");
68 close(sd);
69 exit(1);
70 }
71 else
72 printf("Disabling the loopback...OK.\n");
73 }
74 */
75 
76 /* Set local interface for outbound multicast datagrams. */
77 /* The IP address specified must be associated with a local, */
78 /* multicast capable interface. */
79 localInterface.s_addr = inet_addr("127.0.0.1"); //203.106.93.94
80 if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
81 {
82  perror("Setting local interface error");
83  exit(1);
84 }
85 else
86 {
87  printf("Setting the local interface...OK\n");
88 /* Send a message to the multicast group specified by the*/
89 /* groupSock sockaddr structure. */
90 /*int datalen = 1024;*/
91 
92 int i;
93 
94 while(1){
95 
96 if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0)
97 {
98  perror("Sending datagram message error");
99 }
100 else {
101  printf("Sending datagram message...OK\n");
102 }
103 
104  usleep(1000000);
105 }
106 
107 } // end of else
108 
109 /* Try the re-read from the socket if the loopback is not disable
110 if(read(sd, databuf, datalen) < 0)
111 {
112 perror("Reading datagram message error\n");
113 close(sd);
114 exit(1);
115 }
116 else
117 {
118 printf("Reading datagram message from client...OK\n");
119 printf("The message is: %s\n", databuf);
120 }
121 */
122 
123 return 0;
124 }
struct sockaddr_in groupSock
Definition: mcastserver.c:38
int i
Definition: rw_test.cpp:37
int sd
Definition: mcastserver.c:39
struct in_addr localInterface
Definition: mcastserver.c:37
char databuf[1024]
Definition: mcastserver.c:40
int datalen
Definition: mcastserver.c:41

Variable Documentation

struct in_addr localInterface
struct sockaddr_in groupSock
int sd
char databuf[1024] = "Multicast test message lol!"
int datalen = sizeof(databuf)