Basic Image AlgorithmS Library 2.8.0

ExampleNetworkClient.cpp

00001 /* 
00002 This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004 Copyright (C) 2003-2009    (see file CONTACT for details)
00005   Multimediale Systeme der Informationsverarbeitung
00006   Institut fuer Informatik
00007   Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010 BIAS is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU Lesser General Public License as published by
00012 the Free Software Foundation; either version 2.1 of the License, or
00013 (at your option) any later version.
00014 
00015 BIAS is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License
00021 along with BIAS; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 
00026 /** @example ExampleNetworkClient.cpp
00027     @relates CScommClient
00028     @ingroup g_examples    
00029     @brief  this is an example for a network client
00030     @author MIP
00031 */
00032 
00033 #ifdef WIN32
00034 #include <windows.h>
00035 #endif
00036 
00037 #ifndef WIN32
00038 #include <unistd.h>
00039 #endif
00040 
00041 #include <Base/Common/W32Compat.hh>
00042 #include <NetworkComm/CScommClient.hh>
00043 #include <cstdlib>
00044 
00045 
00046 using namespace std;
00047 int main (int argc, char* argv[]) 
00048 {
00049   if (argc<2) {
00050     cout<<"usage:"<<argv[0]<<" serverName"<<endl;
00051     exit(0);
00052   }
00053 
00054   BIAS::CScommClient client(true);
00055 
00056   // if u dont give a name the hostname is taken
00057   client.SetName(string("exampleClient"));
00058 
00059   client.SetVerbose(true); // to get some msgs
00060   client.SetLog(true); // to get all messages
00061   //  client.SetDebugLevel(D_CS_ANALYZE);
00062 
00063   string msg="RPOSHEAD";
00064   // register a msg which will be accepted from the server
00065   client.RegisterMsg(msg, BIAS::CS_FLOAT, 2);
00066   client.RegisterMsg("BINDATA",BIAS::CS_BINARY);
00067 
00068   // now connect the server
00069   cout<<"Connection returned:"<<
00070     client.ConnectServer(string(argv[1]))<<endl;
00071 
00072   // send a msg, this kind of MSG has to be registered at the server side
00073   vector<float> floats;
00074   floats.push_back(0.1f);
00075   floats.push_back(0.2f);
00076   //  floats.push_back(0.3);  
00077  
00078   vector<int> ints;
00079   ints.push_back(-123);  
00080   client.SendMsg(msg,ints);
00081 
00082   floats.push_back(0.4f);
00083   // this is a invalid msg now, because 4 floats are send,
00084   // while only 3 are registered for this msg
00085   //client.SendMsg(msg,floats);
00086   std::vector<char> binData;
00087   floats.resize(1); // and valid again
00088   bool endlessLoop=true;
00089   while (endlessLoop) 
00090   {
00091 
00092     biassleep(1);
00093 
00094     client.SendMsg(msg,ints);
00095     if (client.GetData(msg, floats)==0) {
00096       cout<<"New Data from server:"<<endl;
00097       for (unsigned int i=0;i<floats.size();i++)
00098         cout<<floats[i]<<" ";
00099       cout<<endl;
00100     }
00101     if (client.GetData("BINDATA", binData)==0) {
00102       cout<<"New binary Data from server:"<<endl;
00103       for (unsigned int i=0;i<binData.size();i++)
00104         cout<<binData[i];
00105       cout<<endl;
00106     }
00107   }
00108 }
00109 
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends