46 #include <visp3/core/vpUDPServer.h>
55 vpDataType_t() : double_val(0.0), int_val(0) { }
56 vpDataType_t(
double dbl,
int i) : double_val(dbl), int_val(i) { }
62 #ifdef ENABLE_VISP_NAMESPACE
66 #ifdef VISP_HAVE_FUNC_INET_NTOP
71 std::string msg =
"", hostInfo =
"";
73 int res = server.receive(msg, hostInfo);
75 vpDataType_t data_type;
76 memcpy(&data_type.double_val, msg.c_str(),
sizeof(data_type.double_val));
77 memcpy(&data_type.int_val, msg.c_str() +
sizeof(data_type.double_val),
sizeof(data_type.int_val));
78 std::cout <<
"Server received double_val: " << data_type.double_val <<
" ; int_val: " << data_type.int_val
79 <<
" from: " << hostInfo << std::endl;
82 std::istringstream iss(hostInfo);
83 std::vector<std::string> tokens;
84 std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
85 std::back_inserter(tokens));
86 data_type.double_val += 1.5;
87 data_type.int_val += 2;
88 char data[
sizeof(data_type.double_val) +
sizeof(data_type.int_val)];
89 memcpy(data, &data_type.double_val,
sizeof(data_type.double_val));
90 memcpy(data +
sizeof(data_type.double_val), &data_type.int_val,
sizeof(data_type.int_val));
91 msg = std::string(data,
sizeof(data_type.double_val) +
sizeof(data_type.int_val));
93 server.send(msg, tokens[1], atoi(tokens[2].c_str()));
98 res = server.receive(msg, hostInfo, 5000);
100 std::cout <<
"Server received: " << msg <<
" from: " << hostInfo << std::endl;
101 std::cout <<
"Reply to the client: Echo: " << msg << std::endl;
104 std::istringstream iss(hostInfo);
105 std::vector<std::string> tokens;
106 std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(),
107 std::back_inserter(tokens));
108 server.send(
"Echo: " + msg, tokens[1], atoi(tokens[2].c_str()));
111 std::cout <<
"Receive timeout" << std::endl;
114 std::cerr <<
"Error server.receive()!" << std::endl;
121 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
125 std::cout <<
"This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
error that can be emitted by ViSP classes.
const char * what() const
This class implements a basic (IPv4) User Datagram Protocol (UDP) server.