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

socketServerExample.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 
00060 int main()
00061 {
00062   // The string to send to the client. 
00063   char *strToSend="Hello Client";
00064   // The buffer in which to recieve the hello from the client
00065   char buff[100];
00066   // The size of the string the client sent
00067   size_t strSize;
00068 
00069   // The socket objects: one for accepting new client connections,
00070   // and another for communicating with a client after it connects.
00071   ArSocket serverSock, clientSock;
00072 
00073   // Initialize Aria.  This is especially essential on Windows,
00074   // because it will initialize Windows's sockets sytem.
00075   Aria::init();
00076 
00077   // Open the server socket
00078   if (serverSock.open(7777, ArSocket::TCP))
00079     ArLog::log(ArLog::Normal, "socketServerExample: Opened the server port.");
00080   else
00081   {
00082     ArLog::log(ArLog::Normal, "socketServerExample: Failed to open the server port: %s.",
00083        serverSock.getErrorStr().c_str());
00084     return(-1);
00085   }
00086 
00087   // Wait for a client to connect to us.
00088   if (serverSock.accept(&clientSock))
00089     ArLog::log(ArLog::Normal, "socketServerExample: Client has connected.");
00090   else
00091     ArLog::log(ArLog::Terse, "socketServerExample: Error in accepting a connection from the client: %s.",
00092        serverSock.getErrorStr().c_str());
00093 
00094   // Send the string 'Hello Client' to the client. write() should
00095   // return the same number of bytes that we told it to write. Otherwise,
00096   // its an error condition.
00097   if (clientSock.write(strToSend, strlen(strToSend)) == strlen(strToSend))
00098     ArLog::log(ArLog::Normal, "socketServerExample: Said hello to the client.");
00099   else
00100   {
00101     ArLog::log(ArLog::Normal, "socketServerExample: Error in sending hello string to the client.");
00102     return(-1);
00103   }
00104 
00105   // Read data from the client. read() will block until data is
00106   // received. 
00107   strSize=clientSock.read(buff, sizeof(buff));
00108 
00109   // If the amount read is 0 or less, its an error condition.
00110   if (strSize > 0)
00111   {
00112     // Terminate the string with a NULL character.
00113     buff[strSize]='\0';
00114     ArLog::log(ArLog::Normal, "socketServerExample: Client said: %s.", buff);
00115   }
00116   else
00117   {
00118     ArLog::log(ArLog::Normal, "socketServerExample: Error in waiting/reading the hello from the client.");
00119     return(-1);
00120   }
00121 
00122   // Now lets close the connection to the client
00123   clientSock.close();
00124   ArLog::log(ArLog::Normal, "socketServerExample: Socket to client closed.");
00125   
00126 
00127   // And lets close the server port
00128   serverSock.close();
00129     ArLog::log(ArLog::Normal, "socketServerExample: Server socket closed.");
00130 
00131 
00132   // Uninitialize Aria
00133   Aria::uninit();
00134 
00135   // All done
00136   return(0);
00137 }

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