Visual Servoing Platform version 3.5.0
testUDPClient.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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
47namespace
48{
49struct DataType {
50 double double_val;
51 int int_val;
52
53 DataType() : double_val(0.0), int_val(0) {}
54 DataType(double dbl, int i) : double_val(dbl), int_val(i) {}
55};
56}
57
58int 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 }
69 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
70 std::cout << argv[0] << " [--ip <address> (default: 127.0.0.1)] [--help] [-h]"
71 << "\n";
72 return EXIT_SUCCESS;
73 }
74 }
75
76 unsigned int port = 50037;
77 vpUDPClient client(servername, port);
78
79 // Send custom data type
80 DataType data_type(1234.56789, 123450);
81 char data[sizeof(data_type.double_val) + sizeof(data_type.int_val)];
82 memcpy(data, &data_type.double_val, sizeof(data_type.double_val));
83 memcpy(data + sizeof(data_type.double_val), &data_type.int_val, sizeof(data_type.int_val));
84 std::string msg(data, sizeof(data_type.double_val) + sizeof(data_type.int_val));
85 if (client.send(msg) != (int)sizeof(data_type.double_val) + sizeof(data_type.int_val))
86 std::cerr << "Error client.send()!" << std::endl;
87
88 if (client.receive(msg)) {
89 data_type.double_val = *reinterpret_cast<const double *>(msg.c_str());
90 data_type.int_val = *reinterpret_cast<const int *>(msg.c_str() + sizeof(data_type.double_val));
91
92 std::cout << "Receive from the server double_val: " << data_type.double_val << " ; int_val: " << data_type.int_val
93 << std::endl;
94 }
95
96 // Send user message
97 while (true) {
98 std::cout << "Enter the message to send:" << std::endl;
99 msg.clear();
100 std::getline(std::cin, msg);
101 if (client.send(msg) != (int)msg.size())
102 std::cerr << "Error client.send()!" << std::endl;
103 if (client.receive(msg))
104 std::cout << "Receive from the server: " << msg << std::endl;
105 }
106
107 return EXIT_SUCCESS;
108 } catch (const vpException &e) {
109 std::cerr << "Catch an exception: " << e.what() << std::endl;
110 return EXIT_FAILURE;
111 }
112#else
113 std::cout << "This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
114 (void)argc;
115 (void)argv;
116 return EXIT_SUCCESS;
117#endif
118}
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * what() const
This class implements a basic (IPv4) User Datagram Protocol (UDP) client.
Definition: vpUDPClient.h:169