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