summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_2/ex05.html
blob: 1a88e7c8dbd0d679d2ceaec2cf74fd818fcdee11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Ambreen Ilyas">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]">
   <TITLE>Example 5</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; Chapter: "IPC SAP" (Interprocess Communication
Mechanisms in ACE).</FONT>
<BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT>
<BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; AUTHOR: Umar Syyid (usyyid@hns.com)</FONT>
<BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT>
<BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>

<P><FONT COLOR="#CC0000">//Example 5</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/SOCK_Dgram_Mcast.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/OS.h"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">DEFAULT_MULTICAST_ADDR
"224.9.9.2"</FONT>
<BR><FONT COLOR="#000099">#define</FONT> <FONT COLOR="#663366">TIMEOUT
5</FONT>

<P>class Reciever_Multicast{

<P>public:
<BR>Reciever_Multicast(int port):
<BR>&nbsp;mcast_addr_(port,DEFAULT_MULTICAST_ADDR),remote_addr_((u_short)0){
<BR>&nbsp;<FONT COLOR="#FF0000"> // Subscribe to multicast address.</FONT>
<BR>&nbsp; if (mcast_dgram_.subscribe (mcast_addr_) == -1){
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Error in subscribing to Multicast address
\n"));
<BR>&nbsp;exit(-1);
<BR>&nbsp;}
<BR>}

<P>~Reciever_Multicast(){
<BR>&nbsp;if(mcast_dgram_.unsubscribe()==-1)
<BR>&nbsp; ACE_DEBUG((LM_ERROR,?Error in unsubscribing from Mcast group\n?));
<BR>&nbsp;}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P><FONT COLOR="#FF0000">//Receive data from someone who is sending data
on the multicast group</FONT>
<BR><FONT COLOR="#FF0000">//address to do so it must use the multicast
datagram component</FONT>
<BR><FONT COLOR="#FF0000">//mcast_dgram_.</FONT>
<BR>int recv_multicast(){
<BR><FONT COLOR="#FF0000">&nbsp;//get ready to recieve data from the sender.</FONT>
<BR>&nbsp;if(mcast_dgram_.recv
<BR>&nbsp;&nbsp;&nbsp; (&amp;mcast_info,sizeof (mcast_info),remote_addr_)==-1)
<BR>&nbsp;&nbsp;&nbsp; return -1;
<BR>&nbsp;ACE_DEBUG ((LM_DEBUG, "(%P|%t) Received multicast from %s:%d.\n",
<BR>&nbsp;remote_addr_.get_host_name(), remote_addr_.get_port_number()));
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"Successfully receieved %d\n", mcast_info));
<BR>&nbsp;return 0;
<BR>&nbsp; }

<P>private:
<BR>&nbsp;ACE_INET_Addr mcast_addr_;
<BR>&nbsp;ACE_INET_Addr remote_addr_;
<BR>&nbsp;ACE_SOCK_Dgram_Mcast mcast_dgram_;
<BR>&nbsp;int mcast_info;
<BR>};
<BR>&nbsp;
<BR>&nbsp;
<BR>int main(int argc, char*argv[]){
<BR>&nbsp;Reciever_Multicast m(2000);
<BR>&nbsp;<FONT COLOR="#FF0000">//Will run forever</FONT>
<BR>&nbsp;while(m.recv_multicast()!=-1) {
<BR>&nbsp; ACE_DEBUG((LM_DEBUG,"Multicaster succesful \n"));
<BR>&nbsp;}
<BR>&nbsp;
<BR>&nbsp;ACE_DEBUG((LM_ERROR,"Multicaster failed \n"));
<BR>&nbsp;exit(-1);
<BR>}

<P>&nbsp;<A HREF="ex06.html">Next Example</A>
<BR>&nbsp;
</BODY>
</HTML>