48 #include <visp3/core/vpUDPServer.h> 56 DataType() : double_val(0.0), int_val(0) {}
57 DataType(
const double dbl,
const int i) : double_val(dbl), int_val(i) {}
64 #ifdef VISP_HAVE_FUNC_INET_NTOP 69 std::string msg =
"", hostInfo =
"";
71 int res = server.receive(msg, hostInfo);
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;
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));
91 server.send(msg, tokens[1], atoi(tokens[2].c_str()));
96 int res = server.receive(msg, hostInfo, 5000);
98 std::cout <<
"Server received: " << msg <<
" from: " << hostInfo << std::endl;
99 std::cout <<
"Reply to the client: Echo: " << msg << std::endl;
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;
110 std::cerr <<
"Error server.receive()!" << std::endl;
116 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
120 std::cout <<
"This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
This class implements a basic (IPv4) User Datagram Protocol (UDP) server.
error that can be emited by ViSP classes.
const char * what() const