summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/client.cpp
diff options
context:
space:
mode:
authorjcej <jcej@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1998-10-18 21:14:49 +0000
committerjcej <jcej@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1998-10-18 21:14:49 +0000
commit0704c9364c9225f5fd55be4fffbe9d2b20679843 (patch)
tree395c2683f72bb603e0066c44bc6215477d20d7ad /docs/tutorials/015/client.cpp
parent72996a22e7d8cf986d3538972c0232db7d294a5d (diff)
downloadATCD-0704c9364c9225f5fd55be4fffbe9d2b20679843.tar.gz
*** empty log message ***
Diffstat (limited to 'docs/tutorials/015/client.cpp')
-rw-r--r--docs/tutorials/015/client.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/docs/tutorials/015/client.cpp b/docs/tutorials/015/client.cpp
new file mode 100644
index 00000000000..dfab2188cbe
--- /dev/null
+++ b/docs/tutorials/015/client.cpp
@@ -0,0 +1,70 @@
+
+// $Id$
+
+/* The Client object will implement the nasty details of connecting to
+ communicating with the server
+*/
+#include "Client.h"
+
+int main(int argc, char *argv[])
+{
+ // How many messages will we send?
+ int mcount = argc > 1 ? ACE_OS::atoi(argv[1]) : 3;
+
+ // Construct a Client with our desired endpoint.
+ Client client(ACE_DEFAULT_SERVER_PORT,ACE_DEFAULT_SERVER_HOST);
+
+ // Attempt to open the connection to the server.
+ if( client.open() == -1 )
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "Client::open()"), -1);
+ }
+
+ // Send a few messages to the server and get some responses...
+ for( int i = 0 ; i < mcount ; ++i )
+ {
+ // Since we'll be using a Protocol Stream (even though we
+ // don't know that at this level) we require the use of
+ // ACE_Message_Block objects to send/receive data.
+ ACE_Message_Block * message = new ACE_Message_Block( 128 );
+
+ // Construct a silly message to send to the server.
+ // Notice that we're careful to add one to the strlen() so
+ // that we also send the end-of-string NULL character.
+ ACE_OS::sprintf (message->wr_ptr (), "This is message %d.", i);
+ message->wr_ptr (strlen (message->rd_ptr ())+1);
+
+ // client will take ownership of the message block so that
+ // we don't have to remember to release(). We *do* have
+ // to remember not to use it after put() since it may be
+ // released almost immediately.
+ client.put( message );
+
+ ACE_Message_Block * response;
+
+ // get() takes an ACE_Message_Block pointer reference. We then
+ // assume ownership of it and must release() when we're done.
+ if( client.get( response ) == -1 )
+ {
+ ACE_DEBUG ((LM_INFO, "(%P|%t) Failed to get response from server\n" ));
+ break;
+ }
+
+ ACE_DEBUG ((LM_INFO, "(%P|%t) The server's response: (%s)\n",
+ response->rd_ptr()));
+
+ // Now that we're through with the response we have to
+ // release() it to avoid memory leaks.
+ response->release();
+ }
+
+ ACE_DEBUG ((LM_INFO, "(%P|%t) Shutting down the stream\n" ));
+
+ // Before we exit, it's a good idea to properly close() the connection.
+ if( client.close() == -1 )
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "Client::close()"), -1);
+ }
+
+ return(0);
+}