Visual Servoing Platform  version 3.1.0
vpUDPClient Class Reference

#include <visp3/core/vpUDPClient.h>

Public Member Functions

 vpUDPClient (const std::string &hostname, const int port)
 
 ~vpUDPClient ()
 
int receive (std::string &msg, const int timeoutMs=0)
 
int send (const std::string &msg)
 

Detailed Description

This class implements a basic (IPv4) User Datagram Protocol (UDP) client.

More information here, here or here:

This User Datagram Protocol (UDP) is defined to make available a datagram mode of packet-switched computer communication in the environment of an interconnected set of computer networks. This protocol assumes that the Internet Protocol (IP) [1] is used as the underlying protocol.

This protocol provides a procedure for application programs to send messages to other programs with a minimum of protocol mechanism. The protocol is transaction oriented, and delivery and duplicate protection are not guaranteed. Applications requiring ordered reliable delivery of streams of data should use the Transmission Control Protocol (TCP) [2].

Example of a client's code, sending a basic message and receiving the server answer:

#include <cstdlib>
#include <iostream>
#include <visp3/core/vpUDPClient.h>
int main() {
try {
std::string servername = "127.0.0.1";
unsigned int port = 50037;
vpUDPClient client(servername, port);
while (true) {
std::cout << "Enter the message to send:" << std::endl;
std::string msg = "";
std::getline(std::cin, msg);
if (client.send(msg) != (int) msg.size())
std::cerr << "Error client.send()!" << std::endl;
if (client.receive(msg))
std::cout << "Receive from the server: " << msg << std::endl;
}
return EXIT_SUCCESS;
} catch (const vpException &e) {
std::cerr << "Catch an exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}

If you want to send a complex data type, you can either send the ASCII representation or send directly the byte data. In the last case, you should have to handle that both the server and the client have the same data type representation. Be careful also with the endianness of the network / host.

Here an example using a structure of data, assuming that both the server and the client have the same architecture (probably you should write your own serialization / deserialization functions for the data you want to send / receive):

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <visp3/core/vpUDPClient.h>
struct DataType {
double double_val;
int int_val;
DataType() : double_val(0.0), int_val(0) {}
DataType(const double dbl, const int i) : double_val(dbl), int_val(i) {}
};
int main() {
try {
std::string servername = "127.0.0.1";
unsigned int port = 50037;
vpUDPClient client(servername, port);
DataType data_type(1234.56789, 123450);
char data[sizeof(data_type.double_val)+sizeof(data_type.int_val)];
memcpy(data, &data_type.double_val, sizeof(data_type.double_val));
memcpy(data+sizeof(data_type.double_val), &data_type.int_val, sizeof(data_type.int_val));
std::string msg(data, sizeof(data_type.double_val)+sizeof(data_type.int_val));
if (client.send(msg) != (int) sizeof(data_type.double_val)+sizeof(data_type.int_val))
std::cerr << "Error client.send()!" << std::endl;
if (client.receive(msg)) {
data_type.double_val = *reinterpret_cast<const double *>(msg.c_str());
data_type.int_val
= *reinterpret_cast<const int *>(msg.c_str()+sizeof(data_type.double_val));
std::cout << "Receive from the server double_val: " << data_type.double_val
<< " ; int_val: " << data_type.int_val << std::endl;
}
return EXIT_SUCCESS;
} catch (const vpException &e) {
std::cerr << "Catch an exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}
See also
vpUDPServer
Examples:
testUDPClient.cpp.

Definition at line 165 of file vpUDPClient.h.

Constructor & Destructor Documentation

◆ vpUDPClient()

vpUDPClient::vpUDPClient ( const std::string &  hostname,
const int  port 
)

Create a (IPv4) UDP client.

Parameters
hostname: Server hostname or IP address.
port: Server port number.

Definition at line 60 of file vpUDPClient.cpp.

◆ ~vpUDPClient()

vpUDPClient::~vpUDPClient ( )

Definition at line 70 of file vpUDPClient.cpp.

References vpException::fatalError.

Member Function Documentation

◆ receive()

int vpUDPClient::receive ( std::string &  msg,
const int  timeoutMs = 0 
)

Receive data sent by the server.

Parameters
msg: ASCII message or byte data.
timeoutMs: Timeout in millisecond (if zero, the call is blocking).
Returns
The message length / size of the byte array sent received, or -1 if there is an error, or 0 if there is a timeout.
Note
To transform the ASCII representation of an integer:
int val = atoi(msg.c_str());
//or
std::istringstream ss(msg);
ss >> val;
To convert from a byte array to an integer:
int val = *reinterpret_cast<const int *>(msg.c_str());
Examples:
testUDPClient.cpp.

Definition at line 153 of file vpUDPClient.cpp.

◆ send()

int vpUDPClient::send ( const std::string &  msg)

Send data to the server.

Parameters
msg: ASCII message or byte data.
Returns
The message length / size of the byte array sent.
Note
To send the ASCII representation of an integer:
int val = 1024;
std::ostringstream os;
os << val;
server.send(os.str(), hostname, port);
To send directly the byte data (assuming the same integer representation on the server and the client):
int val = 1024;
char data[sizeof(val)];
memcpy(data, &val, sizeof(val));
std::string msg(data, sizeof(val)); //required to avoid the string being splitted with the first \0 character
server.send(msg, hostname, port);
Examples:
testUDPClient.cpp.

Definition at line 211 of file vpUDPClient.cpp.