summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page02.html
blob: bdf3a3cb44daca647228bc91fb9805331f9131ba (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
<!-- $Id$ -->
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 015</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

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

<CENTER><B><FONT SIZE=+2>Building a protocol stream</FONT></B></CENTER>

<P>
<HR WIDTH="100%">
We'll take a look first at the client application.  As usual, our goal
      is to keep the main() application as simple as possible and
      delegate the tricky stuff to another object.

<HR>
<PRE>

<font color=red>// $Id$</font>

<font color=red>/* The Client object will implement the nasty details of connecting to
   communicating with the server
*/</font>
<font color=blue>#include</font> "<font color=green>Client_i.h</font>"

int main(int argc, char *argv[])
{
        <font color=red>// How many messages will we send?</font>
    int mcount = argc > 1 ? <font color=#008888>ACE_OS::atoi</font>(argv[1]) : 3;

        <font color=red>// Construct a Client with our desired endpoint.</font>
    Client client(ACE_DEFAULT_SERVER_PORT,ACE_DEFAULT_SERVER_HOST);

        <font color=red>// Attempt to open the connection to the server.</font>
    if( client.open() == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green><font color=#008888>Client::open</font>()</font>"), -1);
    }

        <font color=red>// Send a few messages to the server and get some responses...</font>
    for( int i = 0 ; i &lt; mcount ; ++i )
    {
            <font color=red>// Since we'll be using a Protocol Stream (even though we</font>
            <font color=red>// don't know that at this level) we require the use of</font>
            <font color=red>// ACE_Message_Block objects to send/receive data.</font>
        ACE_Message_Block * message = new ACE_Message_Block( 128 );

            <font color=red>// Construct a silly message to send to the server.</font>
            <font color=red>// Notice that we're careful to add one to the strlen() so</font>
            <font color=red>// that we also send the end-of-string NULL character.</font>
        <font color=#008888>ACE_OS::sprintf</font> (message->wr_ptr (), "<font color=green>This is message %d.</font>", i);
        message->wr_ptr (strlen (message->rd_ptr ())+1);

            <font color=red>// client will take ownership of the message block so that</font>
            <font color=red>// we don't have to remember to release().  We *do* have</font>
            <font color=red>// to remember not to use it after put() since it may be</font>
            <font color=red>// released almost immediately.</font>
        client.put( message );

        ACE_Message_Block * response;

            <font color=red>// get() takes an ACE_Message_Block pointer reference.  We then</font>
            <font color=red>// assume ownership of it and must release() when we're done.</font>
        if( client.get( response ) == -1 )
        {
            ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) Failed to get response from server\n</font>" ));
            break;
        }

        ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) The server's response:  (%s)\n</font>",
                    response->rd_ptr()));

            <font color=red>// Now that we're through with the response we have to</font>
            <font color=red>// release() it to avoid memory leaks.</font>
        response->release();
    }

    ACE_DEBUG ((LM_INFO, "<font color=green>(%P|%t) Shutting down the stream\n</font>" ));

        <font color=red>// Before we exit, it's a good idea to properly close() the connection.</font>
    if( client.close() == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green><font color=#008888>Client::close</font>()</font>"), -1);
    }

    return(0);
}
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page03.html">Continue This Tutorial</A>]</CENTER>