summaryrefslogtreecommitdiff
path: root/docs/tutorials/009/page02.html
blob: e2997a16409161c7ff12b04e009b1f09aaf46e3f (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<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>Let's take a look at our new <A HREF="server.cpp">server.cpp</A> where
we add in just a bit of code to examine the datagram contents before responding.

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

<P><TT>/*</TT>
<BR><TT>&nbsp;&nbsp; The actual datagram operations here are exactly the
same as those used in</TT>
<BR><TT>&nbsp;&nbsp; the previous tutorial.&nbsp; What we've added is some
logic that will prevent</TT>
<BR><TT>&nbsp;&nbsp; this server from responding to just any old datagram.&nbsp;
I'll limit my</TT>
<BR><TT>&nbsp;&nbsp; comments to those pieces of code.</TT>
<BR><TT>&nbsp;*/</TT><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>/*</TT>
<BR><TT>&nbsp;&nbsp; In order to be more selective, our server will be
started with a</TT>
<BR><TT>&nbsp;&nbsp; "signature".&nbsp; If none is given, we'll use the
one here instead.</TT>
<BR><TT>&nbsp;*/</TT>
<BR><TT>static const char *default_signature = "Hello World!";</TT><TT></TT>

<P><TT>int main (int argc, char *argv[])</TT>
<BR><TT>{</TT>
<BR><TT>&nbsp; ACE_INET_Addr local (PORT);</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>
<BR><TT>&nbsp; ACE_INET_Addr remote;</TT><TT></TT>

<P><TT>&nbsp; while (dgram.recv (buf, sizeof (buf), remote) != -1)</TT>
<BR><TT>&nbsp; {</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; /*</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; What did the client say?</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; */</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; ACE_DEBUG ((LM_DEBUG, "(%P|%t) Received (%s)
from (%s)\n", buf, remote.get_host_name ()));</TT><TT></TT>

<P><TT>&nbsp;&nbsp;&nbsp; /*</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Use a simple string-op to
decide if the client is one of our own.&nbsp; Of</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; course, you could have sent
numeric values or even a struct of data. For</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this simple exercise, however,
strings are just fine.</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp; */</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; if (ACE_OS::strcmp (buf, argc > 1 ? argv[1]
: default_signature))</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; {</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If the client
didn't say something we like then log it and move on.</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ACE_DEBUG ((LM_DEBUG,</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"(%P|%t) Client query does not match our signature (%s).&nbsp; Response
not sent.\n",</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
argc > 1 ? argv[1] : default_signature));</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; }</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; else</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; {</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; As before, we
respond to the client's query.</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</TT><TT></TT>

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

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

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

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

<P>Let's move on and see what changes the clients require...

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

</BODY>
</HTML>