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

this is an example for a network server

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 ExampleNetworkServer.cpp
@relates CScommServer
@ingroup g_examples
@brief this is an example for a network server
@author MIP
*/
#ifdef WIN32
#include <windows.h>
#endif
#ifndef WIN32
#include <unistd.h>
#endif
#include <NetworkComm/CScommServer.hh>
#include <Base/Common/W32Compat.hh>
using namespace std;
int main (int /*argc*/, char** /*argv*/)
{
server.SetVerbose(true); // to see some msg
// server.SetLog(true); // uncomment this to get all data send and received
// server.SetDebugLevel(D_CS_GETDATA);
//server.SetDebugLevel(D_CS_ANALYZE);
vector<float> floats;
floats.push_back(-3);
floats.push_back(-2);
floats.push_back(-1);
string msg="LPOSHEAD";
// register msg POSHEAD to be accepted
server.RegisterMsg(msg, BIAS::CS_FLOAT, 3);
msg="RPOSHEAD";
server.RegisterMsg(msg, BIAS::CS_INT, 1);
vector<int> ints,intMsg;
intMsg.push_back(-5);
intMsg.push_back(+6);
// now let the server accept connections
vector<float> recFloats;
std::vector<char> binData(8);
binData[0]=']';
binData[1]='b';
binData[2]='i';
binData[3]='n';
binData[4]='a';
binData[5]='r';
binData[6]='y';
binData[7]='[';
bool endless=true;
while (endless) {
// this msg will not be accepted by the exampleClient,
// because it is not registered
// server.SendMsg(msg,floats);
msg="LPOSHEAD";
if (server.GetData(msg, recFloats)==0) {
cout<<"New Data from client:"<<endl;
for (unsigned int i=0;i<recFloats.size();i++)
cout<<recFloats[i]<<" ";
cout<<endl;
}
biassleep(1);
msg="RPOSHEAD";
if (server.GetData(msg, ints)==0) {
cout<<"New Data from client:"<<endl;
for (unsigned int i=0;i<ints.size();i++)
cout<<ints[i]<<" ";
cout<<endl;
}
// this sends only to exampleClient
server.SendMsg("exampleClient","BINDATA",&binData[0],binData.size());
// this sends to all connected partners
server.SendMsg("RPOSHEAD",intMsg);
}
}