Visual Servoing Platform version 3.5.0
testUDPServer.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 server.
33 *
34 *****************************************************************************/
35
42#include <cstdlib>
43#include <cstring>
44#include <iostream>
45#include <iterator>
46#include <sstream>
47#include <vector>
48#include <visp3/core/vpUDPServer.h>
49
50namespace
51{
52struct DataType {
53 double double_val;
54 int int_val;
55
56 DataType() : double_val(0.0), int_val(0) {}
57 DataType(double dbl, int i) : double_val(dbl), int_val(i) {}
58};
59}
60
61int main()
62{
63// inet_ntop() used in vpUDPClient is not supported on win XP
64#ifdef VISP_HAVE_FUNC_INET_NTOP
65 try {
66 int port = 50037;
67 vpUDPServer server(port);
68
69 std::string msg = "", hostInfo = "";
70 // Receive and send custom data type
71 int res = server.receive(msg, hostInfo);
72 if (res) {
73 DataType data_type;
74 memcpy(&data_type.double_val, msg.c_str(), sizeof(data_type.double_val));
75 memcpy(&data_type.int_val, msg.c_str() + sizeof(data_type.double_val), sizeof(data_type.int_val));
76 std::cout << "Server received double_val: " << data_type.double_val << " ; int_val: " << data_type.int_val
77 << " from: " << hostInfo << std::endl;
78
79 // Get address and port
80 std::istringstream iss(hostInfo);
81 std::vector<std::string> tokens;
82 std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
83 std::back_inserter(tokens));
84 data_type.double_val += 1.5;
85 data_type.int_val += 2;
86 char data[sizeof(data_type.double_val) + sizeof(data_type.int_val)];
87 memcpy(data, &data_type.double_val, sizeof(data_type.double_val));
88 memcpy(data + sizeof(data_type.double_val), &data_type.int_val, sizeof(data_type.int_val));
89 msg = std::string(data, sizeof(data_type.double_val) + sizeof(data_type.int_val));
90
91 server.send(msg, tokens[1], atoi(tokens[2].c_str()));
92 }
93
94 // Receive and send message
95 while (true) {
96 res = server.receive(msg, hostInfo, 5000);
97 if (res) {
98 std::cout << "Server received: " << msg << " from: " << hostInfo << std::endl;
99 std::cout << "Reply to the client: Echo: " << msg << std::endl;
100
101 // Get address and port
102 std::istringstream iss(hostInfo);
103 std::vector<std::string> tokens;
104 std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
105 std::back_inserter(tokens));
106 server.send("Echo: " + msg, tokens[1], atoi(tokens[2].c_str()));
107 } else if (res == 0) {
108 std::cout << "Receive timeout" << std::endl;
109 } else {
110 std::cerr << "Error server.receive()!" << std::endl;
111 }
112 }
113
114 return EXIT_SUCCESS;
115 } catch (const vpException &e) {
116 std::cerr << "Catch an exception: " << e.what() << std::endl;
117 return EXIT_FAILURE;
118 }
119#else
120 std::cout << "This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
121 return EXIT_SUCCESS;
122#endif
123}
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) server.
Definition: vpUDPServer.h:198