Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleNetworkClient.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /** @example ExampleNetworkClient.cpp
27  @relates CScommClient
28  @ingroup g_examples
29  @brief this is an example for a network client
30  @author MIP
31 */
32 
33 #ifdef WIN32
34 #include <windows.h>
35 #endif
36 
37 #ifndef WIN32
38 #include <unistd.h>
39 #endif
40 
41 #include <Base/Common/W32Compat.hh>
42 #include <NetworkComm/CScommClient.hh>
43 #include <cstdlib>
44 
45 
46 using namespace std;
47 int main (int argc, char* argv[])
48 {
49  if (argc<2) {
50  cout<<"usage:"<<argv[0]<<" serverName"<<endl;
51  exit(0);
52  }
53 
54  BIAS::CScommClient client(true);
55 
56  // if u dont give a name the hostname is taken
57  client.SetName(string("exampleClient"));
58 
59  client.SetVerbose(true); // to get some msgs
60  client.SetLog(true); // to get all messages
61  // client.SetDebugLevel(D_CS_ANALYZE);
62 
63  string msg="RPOSHEAD";
64  // register a msg which will be accepted from the server
65  client.RegisterMsg(msg, BIAS::CS_FLOAT, 2);
66  client.RegisterMsg("BINDATA",BIAS::CS_BINARY);
67 
68  // now connect the server
69  cout<<"Connection returned:"<<
70  client.ConnectServer(string(argv[1]))<<endl;
71 
72  // send a msg, this kind of MSG has to be registered at the server side
73  vector<float> floats;
74  floats.push_back(0.1f);
75  floats.push_back(0.2f);
76  // floats.push_back(0.3);
77 
78  vector<int> ints;
79  ints.push_back(-123);
80  client.SendMsg(msg,ints);
81 
82  floats.push_back(0.4f);
83  // this is a invalid msg now, because 4 floats are send,
84  // while only 3 are registered for this msg
85  //client.SendMsg(msg,floats);
86  std::vector<char> binData;
87  floats.resize(1); // and valid again
88  bool endlessLoop=true;
89  while (endlessLoop)
90  {
91 
92  biassleep(1);
93 
94  client.SendMsg(msg,ints);
95  if (client.GetData(msg, floats)==0) {
96  cout<<"New Data from server:"<<endl;
97  for (unsigned int i=0;i<floats.size();i++)
98  cout<<floats[i]<<" ";
99  cout<<endl;
100  }
101  if (client.GetData("BINDATA", binData)==0) {
102  cout<<"New binary Data from server:"<<endl;
103  for (unsigned int i=0;i<binData.size();i++)
104  cout<<binData[i];
105  cout<<endl;
106  }
107  }
108 }
109 
class for sending/receiving data between clients and servers
Definition: CScommClient.hh:54