Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

socketClientExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 
00028 
00054 int main()
00055 {
00056   // The string to send to the server. 
00057   char *strToSend="Hello Server";
00058   // The buffer in which to recieve the hello from the server
00059   char buff[100];
00060   // The size of the string the server sent
00061   size_t strSize;
00062 
00063   // The socket object
00064   ArSocket sock;
00065 
00066   // Initialize Aria.  It is especially important to do
00067   // this on Windows, because it will initialize Window's
00068   // sockets system. 
00069   Aria::init();
00070 
00071   // Connect to the server
00072   ArLog::log(ArLog::Normal, "socketClientExample: Connecting to localhost TCP port 7777...");
00073   if (sock.connect("localhost", 7777, ArSocket::TCP))
00074     ArLog::log(ArLog::Normal, "socketClientExample: Connected to server at localhost TCP port 7777.");
00075   else
00076   {
00077     ArLog::log(ArLog::Terse, "socketClientExample: Error connecting to server at localhost TCP port 7777: %s", sock.getErrorStr().c_str());
00078     return(-1);
00079   }
00080 
00081   // Read data from the socket. read() will block until
00082   // data is received. 
00083   strSize=sock.read(buff, sizeof(buff));
00084 
00085   // If the amount read is 0 or less, its an error condition.
00086   if (strSize > 0)
00087   {
00088     buff[strSize]='\0'; // Terminate the string with a NULL character:
00089     ArLog::log(ArLog::Normal, "socketClientExample: Server said: \"%s\"", buff);
00090   }
00091   else
00092   {
00093       ArLog::log(ArLog::Terse, "socketClientExample: Error in waiting/reading from the server.");
00094     return(-1);
00095   }
00096 
00097   // Send the string 'Hello Server' to the server. write() should
00098   // return the same number of bytes that we told it to write. Otherwise,
00099   // its an error condition.
00100   if (sock.write(strToSend, strlen(strToSend)) == strlen(strToSend))
00101     ArLog::log(ArLog::Normal, "socketClientExample: Said hello to the server.");
00102   else
00103   {
00104       ArLog::log(ArLog::Terse, "socketClientExample: Error sending hello string to the server.");
00105     return(-1);
00106   }
00107 
00108   // Now close the connection to the server
00109   sock.close();
00110   ArLog::log(ArLog::Normal, "socketClientExample: Socket to server closed.");
00111 
00112   // Uninitialize Aria and exit
00113   Aria::uninit();
00114   return(0);
00115 }

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0