summaryrefslogtreecommitdiff
path: root/docs/tutorials/009/page03.html
blob: b84f22bcba807ef787384d1e915798517a2b5820 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
<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>Our new <A HREF="directed_client.cpp">directed_client.cpp</A>&nbsp;
is very much like our previous one.&nbsp; The primary difference is the
addition of a timeout to the recv() call so that we can exit somewhat gracefully
if the server doesn't like what we have to say.

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

<P><TT>#include "ace/SOCK_Dgram.h"</TT>
<BR><TT>#include "ace/INET_Addr.h"</TT><TT></TT>

<P><TT>static const u_short PORT = ACE_DEFAULT_SERVER_PORT;</TT><TT></TT>

<P><TT>int main (int argc, char *argv[])</TT>
<BR><TT>{</TT>
<BR><TT>&nbsp; ACE_INET_Addr local ((u_short) 0);</TT>
<BR><TT>&nbsp; ACE_INET_Addr remote (PORT, argc > 1 ? argv[1] : "localhost");</TT>
<BR><TT>&nbsp; ACE_SOCK_Dgram dgram;</TT><TT></TT>

<P><TT>&nbsp; if (dgram.open (local) == -1)</TT>
<BR><TT>&nbsp; {</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"),
-1);</TT>
<BR><TT>&nbsp; }</TT><TT></TT>

<P><TT>&nbsp; char buf[512];</TT><TT></TT>

<P><TT>&nbsp; /*</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; In order to conform to the "protocol"
requried by the server,</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; we allow the user to specify a signature.&nbsp;
A default matching</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; the server's default is also available.</TT>
<BR><TT>&nbsp;&nbsp; */</TT>
<BR><TT>&nbsp; sprintf (buf, argc > 2 ? argv[2] : "Hello World!");</TT><TT></TT>

<P><TT>&nbsp; if (dgram.send (buf, strlen (buf) + 1, remote) == -1)</TT>
<BR><TT>&nbsp; {</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send"),
-1);</TT>
<BR><TT>&nbsp; }</TT><TT></TT>

<P><TT>&nbsp; /*</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; Because we may have sent a signature that
the server doesn't</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; honor, we have to have some way to get
out of the recv().</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; Most ACE objects that have potential for
infinite blocking</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; give you the option of providing a timeout.&nbsp;
recv() is no</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; exception.&nbsp; Here, we construct an
ACE_Time_Value representing</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; two seconds and no micro-seconds.&nbsp;
If recv() fails to get</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; a response within the two seconds, it
will return -1.</TT>
<BR><TT>&nbsp;&nbsp; */</TT>
<BR><TT>&nbsp; ACE_Time_Value timeout (2, 0);</TT>
<BR><TT>&nbsp; if (dgram.recv (buf, sizeof (buf), remote, 0, &amp;timeout)
== -1)</TT>
<BR><TT>&nbsp; {</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "recv"),
-1);</TT>
<BR><TT>&nbsp; }</TT><TT></TT>

<P><TT>&nbsp; ACE_DEBUG ((LM_DEBUG, "(%P|%t) The server said (%s)\n", buf));</TT><TT></TT>

<P><TT>&nbsp; return (0);</TT>
<BR><TT>}</TT>

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

<P>On the next page, we see that the directed_client gets similar upgrades.

<P>
<HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page04.html">Continue
This Tutorial</A>]</CENTER>

</BODY>
</HTML>