Visual Servoing Platform  version 3.6.1 under development (2024-11-05)
testUDPServer.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test for UDP server.
32  */
33 
40 #include <cstdlib>
41 #include <cstring>
42 #include <iostream>
43 #include <iterator>
44 #include <sstream>
45 #include <vector>
46 #include <visp3/core/vpUDPServer.h>
47 
48 namespace
49 {
50 struct vpDataType_t
51 {
52  double double_val;
53  int int_val;
54 
55  vpDataType_t() : double_val(0.0), int_val(0) { }
56  vpDataType_t(double dbl, int i) : double_val(dbl), int_val(i) { }
57 };
58 } // namespace
59 
60 int main()
61 {
62 #ifdef ENABLE_VISP_NAMESPACE
63  using namespace VISP_NAMESPACE_NAME;
64 #endif
65 // inet_ntop() used in vpUDPClient is not supported on win XP
66 #ifdef VISP_HAVE_FUNC_INET_NTOP
67  try {
68  int port = 50037;
69  vpUDPServer server(port);
70 
71  std::string msg = "", hostInfo = "";
72  // Receive and send custom data type
73  int res = server.receive(msg, hostInfo);
74  if (res) {
75  vpDataType_t data_type;
76  memcpy(&data_type.double_val, msg.c_str(), sizeof(data_type.double_val));
77  memcpy(&data_type.int_val, msg.c_str() + sizeof(data_type.double_val), sizeof(data_type.int_val));
78  std::cout << "Server received double_val: " << data_type.double_val << " ; int_val: " << data_type.int_val
79  << " from: " << hostInfo << std::endl;
80 
81 // Get address and port
82  std::istringstream iss(hostInfo);
83  std::vector<std::string> tokens;
84  std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
85  std::back_inserter(tokens));
86  data_type.double_val += 1.5;
87  data_type.int_val += 2;
88  char data[sizeof(data_type.double_val) + sizeof(data_type.int_val)];
89  memcpy(data, &data_type.double_val, sizeof(data_type.double_val));
90  memcpy(data + sizeof(data_type.double_val), &data_type.int_val, sizeof(data_type.int_val));
91  msg = std::string(data, sizeof(data_type.double_val) + sizeof(data_type.int_val));
92 
93  server.send(msg, tokens[1], atoi(tokens[2].c_str()));
94  }
95 
96  // Receive and send message
97  while (true) {
98  res = server.receive(msg, hostInfo, 5000);
99  if (res) {
100  std::cout << "Server received: " << msg << " from: " << hostInfo << std::endl;
101  std::cout << "Reply to the client: Echo: " << msg << std::endl;
102 
103  // Get address and port
104  std::istringstream iss(hostInfo);
105  std::vector<std::string> tokens;
106  std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
107  std::back_inserter(tokens));
108  server.send("Echo: " + msg, tokens[1], atoi(tokens[2].c_str()));
109  }
110  else if (res == 0) {
111  std::cout << "Receive timeout" << std::endl;
112  }
113  else {
114  std::cerr << "Error server.receive()!" << std::endl;
115  }
116  }
117 
118  return EXIT_SUCCESS;
119  }
120  catch (const vpException &e) {
121  std::cerr << "Catch an exception: " << e.what() << std::endl;
122  return EXIT_FAILURE;
123  }
124 #else
125  std::cout << "This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
126  return EXIT_SUCCESS;
127 #endif
128 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
This class implements a basic (IPv4) User Datagram Protocol (UDP) server.
Definition: vpUDPServer.h:203