45 #include <visp3/core/vpUDPClient.h> 53 DataType() : double_val(0.0), int_val(0) {}
54 DataType(
const double dbl,
const int i) : double_val(dbl), int_val(i) {}
58 int main(
int argc,
char **argv)
61 #ifdef VISP_HAVE_FUNC_INET_NTOP 63 std::string servername = std::string(
"127.0.0.1");
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]);
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]" 76 unsigned int port = 50037;
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;
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));
92 std::cout <<
"Receive from the server double_val: " << data_type.double_val <<
" ; int_val: " << data_type.int_val
98 std::cout <<
"Enter the message to send:" << std::endl;
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;
109 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
113 std::cout <<
"This test doesn't work on win XP where inet_ntop() is not available" << std::endl;
error that can be emited by ViSP classes.
const char * what() const
This class implements a basic (IPv4) User Datagram Protocol (UDP) client.