Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testUDPClient.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 client.
32  */
33 
40 #include <cstdlib>
41 #include <cstring>
42 #include <iostream>
43 #include <visp3/core/vpUDPClient.h>
44 
45 namespace
46 {
47 struct vpDataType_t
48 {
49  double double_val;
50  int int_val;
51 
52  vpDataType_t() : double_val(0.0), int_val(0) { }
53  vpDataType_t(double dbl, int i) : double_val(dbl), int_val(i) { }
54 };
55 } // namespace
56 
57 int main(int argc, char **argv)
58 {
59 #ifdef ENABLE_VISP_NAMESPACE
60  using namespace VISP_NAMESPACE_NAME;
61 #endif
62 // inet_ntop() used in vpUDPClient is not supported on win XP
63 #ifdef VISP_HAVE_FUNC_INET_NTOP
64  try {
65  std::string servername = std::string("127.0.0.1");
66 
67  for (int i = 1; i < argc; i++) {
68  if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
69  servername = std::string(argv[i + 1]);
70  }
71  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
72  std::cout << argv[0] << " [--ip <address> (default: 127.0.0.1)] [--help] [-h]"
73  << "\n";
74  return EXIT_SUCCESS;
75  }
76  }
77 
78  unsigned int port = 50037;
79  vpUDPClient client(servername, port);
80 
81  // Send custom data type
82  vpDataType_t data_type(1234.56789, 123450);
83  char data[sizeof(data_type.double_val) + sizeof(data_type.int_val)];
84  memcpy(data, &data_type.double_val, sizeof(data_type.double_val));
85  memcpy(data + sizeof(data_type.double_val), &data_type.int_val, sizeof(data_type.int_val));
86  std::string msg(data, sizeof(data_type.double_val) + sizeof(data_type.int_val));
87  if (client.send(msg) != (int)sizeof(data_type.double_val) + sizeof(data_type.int_val))
88  std::cerr << "Error client.send()!" << std::endl;
89 
90  if (client.receive(msg)) {
91  data_type.double_val = *reinterpret_cast<const double *>(msg.c_str());
92  data_type.int_val = *reinterpret_cast<const int *>(msg.c_str() + sizeof(data_type.double_val));
93 
94  std::cout << "Receive from the server double_val: " << data_type.double_val << " ; int_val: " << data_type.int_val
95  << std::endl;
96  }
97 
98  // Send user message
99  while (true) {
100  std::cout << "Enter the message to send:" << std::endl;
101  msg.clear();
102  std::getline(std::cin, msg);
103  if (client.send(msg) != (int)msg.size())
104  std::cerr << "Error client.send()!" << std::endl;
105  if (client.receive(msg))
106  std::cout << "Receive from the server: " << msg << std::endl;
107  }
108 
109  return EXIT_SUCCESS;
110  }
111  catch (const vpException &e) {
112  std::cerr << "Catch an exception: " << e.what() << std::endl;
113  return EXIT_FAILURE;
114  }
115 #else
116  std::cout << "This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
117  (void)argc;
118  (void)argv;
119  return EXIT_SUCCESS;
120 #endif
121 }
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) client.
Definition: vpUDPClient.h:174