COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
mcastclient.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 mcastclient.c:

Functions

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

Variables

struct sockaddr_in localSock
 
struct ip_mreq group
 
int sd
 
int datalen
 
char databuf [1024]
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)
45 {
46 /* Create a datagram socket on which to receive. */
47 sd = socket(AF_INET, SOCK_DGRAM, 0);
48 if(sd < 0)
49 {
50 perror("Opening datagram socket error");
51 exit(1);
52 }
53 else
54 printf("Opening datagram socket....OK.\n");
55 
56 /* Enable SO_REUSEADDR to allow multiple instances of this */
57 /* application to receive copies of the multicast datagrams. */
58 {
59 int reuse = 1;
60 if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
61 {
62 perror("Setting SO_REUSEADDR error");
63 close(sd);
64 exit(1);
65 }
66 else
67 printf("Setting SO_REUSEADDR...OK.\n");
68 }
69 
70 /* Bind to the proper port number with the IP address */
71 /* specified as INADDR_ANY. */
72 memset((char *) &localSock, 0, sizeof(localSock));
73 localSock.sin_family = AF_INET;
74 localSock.sin_port = htons(4321);
75 localSock.sin_addr.s_addr = INADDR_ANY;
76 if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock)))
77 {
78 perror("Binding datagram socket error");
79 close(sd);
80 exit(1);
81 }
82 else
83 printf("Binding datagram socket...OK.\n");
84 
85 /* Join the multicast group 226.1.1.1 on the local 203.106.93.94 */
86 /* interface. Note that this IP_ADD_MEMBERSHIP option must be */
87 /* called for each local interface over which the multicast */
88 /* datagrams are to be received. */
89 group.imr_multiaddr.s_addr = inet_addr("225.1.1.1"); //226.1.1.1
90 group.imr_interface.s_addr = inet_addr("127.0.0.1"); //203.106.93.94
91 if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
92 {
93 perror("Adding multicast group error");
94 close(sd);
95 exit(1);
96 }
97 else
98 printf("Adding multicast group...OK.\n");
99 
100 /* Read from the socket. */
101 while(1){
102 datalen = sizeof(databuf);
103 if(read(sd, databuf, datalen) < 0)
104 {
105 perror("Reading datagram message error");
106 close(sd);
107 exit(1);
108 }
109 else
110 {
111 printf("Reading datagram message...OK.\n");
112 printf("The message from multicast server is: \"%s\"\n", databuf);
113 }
114 }
115 return 0;
116 }
int datalen
Definition: mcastclient.c:41
struct sockaddr_in localSock
Definition: mcastclient.c:38
char databuf[1024]
Definition: mcastclient.c:42
int sd
Definition: mcastclient.c:40
struct ip_mreq group
Definition: mcastclient.c:39

Variable Documentation

struct sockaddr_in localSock
struct ip_mreq group
int sd
int datalen
char databuf[1024]