Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleNetworkClient.cpp

this is an example for a network client

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleNetworkClient.cpp
@relates CScommClient
@ingroup g_examples
@brief this is an example for a network client
@author MIP
*/
#ifdef WIN32
#include <windows.h>
#endif
#ifndef WIN32
#include <unistd.h>
#endif
#include <Base/Common/W32Compat.hh>
#include <NetworkComm/CScommClient.hh>
#include <cstdlib>
using namespace std;
int main (int argc, char* argv[])
{
if (argc<2) {
cout<<"usage:"<<argv[0]<<" serverName"<<endl;
exit(0);
}
BIAS::CScommClient client(true);
// if u dont give a name the hostname is taken
client.SetName(string("exampleClient"));
client.SetVerbose(true); // to get some msgs
client.SetLog(true); // to get all messages
// client.SetDebugLevel(D_CS_ANALYZE);
string msg="RPOSHEAD";
// register a msg which will be accepted from the server
client.RegisterMsg(msg, BIAS::CS_FLOAT, 2);
client.RegisterMsg("BINDATA",BIAS::CS_BINARY);
// now connect the server
cout<<"Connection returned:"<<
client.ConnectServer(string(argv[1]))<<endl;
// send a msg, this kind of MSG has to be registered at the server side
vector<float> floats;
floats.push_back(0.1f);
floats.push_back(0.2f);
// floats.push_back(0.3);
vector<int> ints;
ints.push_back(-123);
client.SendMsg(msg,ints);
floats.push_back(0.4f);
// this is a invalid msg now, because 4 floats are send,
// while only 3 are registered for this msg
//client.SendMsg(msg,floats);
std::vector<char> binData;
floats.resize(1); // and valid again
bool endlessLoop=true;
while (endlessLoop)
{
biassleep(1);
client.SendMsg(msg,ints);
if (client.GetData(msg, floats)==0) {
cout<<"New Data from server:"<<endl;
for (unsigned int i=0;i<floats.size();i++)
cout<<floats[i]<<" ";
cout<<endl;
}
if (client.GetData("BINDATA", binData)==0) {
cout<<"New binary Data from server:"<<endl;
for (unsigned int i=0;i<binData.size();i++)
cout<<binData[i];
cout<<endl;
}
}
}