summaryrefslogtreecommitdiff
path: root/docs/tutorials/009/page04.html
blob: 944158c3b2f0741b287fcaa3a23a712f9f498564 (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
<!-- $Id$ -->
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (X11; I; Linux 2.0.32 i486) [Netscape]">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 009</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

<CENTER><B><FONT SIZE=+2>ACE Tutorial 009</FONT></B></CENTER>

<CENTER><B><FONT SIZE=+2>Sending and receiving datagrams again</FONT></B></CENTER>


<P>
<HR WIDTH="100%">

<P>As you can see in <A HREF="broadcast_client.cpp">broadcast_client.cpp</A>,
there isn't enough difference to even comment on!&nbsp; Look back to the
Tutorial 8 version of this file.&nbsp; The only difference is the addition
of the timeout variable passed to recv().

<P>
<HR WIDTH="100%"><TT></TT>
<PRE>
<font color=red>// $Id$</font>

<font color=blue>#include</font> "<A HREF="../../../ace/SOCK_Dgram_Bcast.h">ace/SOCK_Dgram_Bcast.h</A>"
<font color=blue>#include</font> "<A HREF="../../../ace/INET_Addr.h">ace/INET_Addr.h</A>"

static const u_short PORT = ACE_DEFAULT_SERVER_PORT;

int
main (int argc, char *argv[])
{
  ACE_INET_Addr local ((u_short) 0);
  ACE_INET_Addr remote (PORT, INADDR_BROADCAST);
  ACE_SOCK_Dgram_Bcast dgram;

  if (dgram.open (local) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "<font color=green>%p\n</font>",
                       "<font color=green>open</font>"),
                      -1);
  char buf[BUFSIZ];

  sprintf (buf,
           argc > 1 ? argv[1] : "<font color=green>Hello World!</font>");
  if (dgram.send (buf,
                  <font color=#008888>ACE_OS::strlen</font> (buf) + 1,
                  remote) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "<font color=green>%p\n</font>",
                       "<font color=green>send</font>"),
                      -1);
  ACE_Time_Value timeout (2, 0);
  if (dgram.recv (buf,
                  sizeof (buf),
                  remote,
                  0,
                  &timeout) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "<font color=green>%p\n</font>",
                       "<font color=green>recv</font>"),
                      -1);
  ACE_DEBUG ((LM_DEBUG,
              "<font color=green>(%P|%t) The server at (%s) said (%s)\n</font>",
        remote.get_host_name (),
              buf));
  return 0;
}
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page05.html">Continue This Tutorial</A>]</CENTER>