Visual Servoing Platform  version 3.1.0
testUDPClient.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 http://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 DataType {
50  double double_val;
51  int int_val;
52 
53  DataType() : double_val(0.0), int_val(0) {}
54  DataType(const double dbl, const int i) : double_val(dbl), int_val(i) {}
55 };
56 }
57 
58 int main()
59 {
60  try {
61  std::string servername = "127.0.0.1";
62  unsigned int port = 50037;
63  vpUDPClient client(servername, port);
64 
65  // Send custom data type
66  DataType data_type(1234.56789, 123450);
67  char data[sizeof(data_type.double_val) + sizeof(data_type.int_val)];
68  memcpy(data, &data_type.double_val, sizeof(data_type.double_val));
69  memcpy(data + sizeof(data_type.double_val), &data_type.int_val, sizeof(data_type.int_val));
70  std::string msg(data, sizeof(data_type.double_val) + sizeof(data_type.int_val));
71  if (client.send(msg) != (int)sizeof(data_type.double_val) + sizeof(data_type.int_val))
72  std::cerr << "Error client.send()!" << std::endl;
73 
74  if (client.receive(msg)) {
75  data_type.double_val = *reinterpret_cast<const double *>(msg.c_str());
76  data_type.int_val = *reinterpret_cast<const int *>(msg.c_str() + sizeof(data_type.double_val));
77 
78  std::cout << "Receive from the server double_val: " << data_type.double_val << " ; int_val: " << data_type.int_val
79  << std::endl;
80  }
81 
82  // Send user message
83  while (true) {
84  std::cout << "Enter the message to send:" << std::endl;
85  std::string msg = "";
86  std::getline(std::cin, msg);
87  if (client.send(msg) != (int)msg.size())
88  std::cerr << "Error client.send()!" << std::endl;
89  if (client.receive(msg))
90  std::cout << "Receive from the server: " << msg << std::endl;
91  }
92 
93  return EXIT_SUCCESS;
94  } catch (const vpException &e) {
95  std::cerr << "Catch an exception: " << e.what() << std::endl;
96  return EXIT_FAILURE;
97  }
98 }
error that can be emited by ViSP classes.
Definition: vpException.h:71
const char * what() const
This class implements a basic (IPv4) User Datagram Protocol (UDP) client.
Definition: vpUDPClient.h:165