summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page04.html
blob: 61a2a0692b780449afcfc3dbe5e1e8ed841b20c6 (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
<!-- $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%">
The implementation of the Client object.  Only the open() method
      really does any work.  The other methods simply delegate their
      function to the Protocol_Stream.
<HR>
<PRE>

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

<font color=blue>#include</font> "<font color=green>Client_i.h</font>"
<font color=blue>#include</font> "<A HREF="../../../ace/Message_Block.h">ace/Message_Block.h</A>"
<font color=blue>#include</font> "<A HREF="../../../ace/INET_Addr.h">ace/INET_Addr.h</A>"
<font color=blue>#include</font> "<A HREF="../../../ace/SOCK_Connector.h">ace/SOCK_Connector.h</A>"

<font color=red>// Simple constructor just remembers the endpoint information for use by open.</font>
<font color=#008888>Client::Client</font>( u_short _port, const char * _server)
        : port_(_port), server_(_server)
{
    ;
}

<font color=red>/* Do nothing.  This should probably call close() if we can make sure
   that it's OK to close() multiple times.
*/</font>
<font color=#008888>Client::~Client</font>(void)
{
    ;
}

<font color=red>/* Open the connection to the server.  This is traditional ACE.  We
   simply construct an endpoint and use a connector to establish the
   link.
*/</font>
int <font color=#008888>Client::open</font>( void )
{
    ACE_INET_Addr addr(port_,server_);
    ACE_SOCK_Connector con;

    if( con.connect(peer(),addr) == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green><font color=#008888>ACE_SOCK_Connector::connect</font>()</font>"), -1);
    }

        <font color=red>// Something new here...  We have to use the protocol stream</font>
        <font color=red>// to ensure that our data is in the correct format when</font>
        <font color=red>// received by the server.  Thus, we open the stream and</font>
        <font color=red>// transfer ownership of the peer.</font>
    return stream().open( peer() );
}

<font color=red>// The remainder of the functions just delegate to the stream.</font>

int <font color=#008888>Client::close</font>( void )
{
    return stream().close();
}

int <font color=#008888>Client::put</font>( ACE_Message_Block * _message )
{
    return stream().put(_message,0);
}

int <font color=#008888>Client::get</font>( ACE_Message_Block * & _response )
{
    return stream().get(_response);
}
</PRE>
<HR>
<P>
Ok, that's it for the client.  We've seen a very simple main()
    followed by an equally simple Client object.
<P>
For a quick look back:
<UL>
<LI><A HREF="Makefile.client">client Makefile</A>
<LI><A HREF="client.cpp">client.cpp</A>
<LI><A HREF="Client_i.h">Client_i.h</A>
<LI><A HREF="Client_i.cpp">Client_i.cpp</A>
</UL>
<P>
Now we'll move on and examine the server counter-part of our client.
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page05.html">Continue This Tutorial</A>]</CENTER>