Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpUDPClient Class Reference

#include <visp3/core/vpUDPClient.h>

+ Inheritance diagram for vpUDPClient:

Public Member Functions

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

Inherited functionalities from vpUDPClient

bool m_is_init
 
void init (const std::string &hostname, int port)
 
int receive (std::string &msg, int timeoutMs=0)
 
int receive (void *msg, size_t len, int timeoutMs=0)
 
int send (const std::string &msg)
 
int send (const void *msg, size_t len)
 

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>
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
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;
}
}
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
This class implements a basic (IPv4) User Datagram Protocol (UDP) client.
Definition: vpUDPClient.h:174

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 <cstring>
#include <iostream>
#include <visp3/core/vpUDPClient.h>
struct vpDataType_t {
double double_val;
int int_val;
vpDataType_t() : double_val(0.0), int_val(0) {}
vpDataType_t(double dbl, int i) : double_val(dbl), int_val(i) {}
};
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
int main() {
try {
std::string servername = "127.0.0.1";
unsigned int port = 50037;
vpUDPClient client(servername, port);
vpDataType_t 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 173 of file vpUDPClient.h.

Constructor & Destructor Documentation

◆ vpUDPClient() [1/2]

BEGIN_VISP_NAMESPACE vpUDPClient::vpUDPClient ( )

Default constructor.

Use connect() to establish the connexion with the server.

Definition at line 67 of file vpUDPClient.cpp.

◆ vpUDPClient() [2/2]

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

Create a (IPv4) UDP client.

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

Definition at line 81 of file vpUDPClient.cpp.

References init().

◆ ~vpUDPClient()

vpUDPClient::~vpUDPClient ( )
virtual

Destructor.

Definition at line 94 of file vpUDPClient.cpp.

Member Function Documentation

◆ init()

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

Initialize a (IPv4) UDP client.

Parameters
hostname: Server hostname or IP address.
port: Server port number.
Examples
testForceTorqueAtiNetFTSensor.cpp.

Definition at line 118 of file vpUDPClient.cpp.

References vpException::fatalError, and m_is_init.

Referenced by vpUDPClient().

◆ receive() [1/2]

int vpUDPClient::receive ( std::string &  msg,
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());

Definition at line 195 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

Referenced by vpForceTorqueAtiNetFTSensor::waitForNewData().

◆ receive() [2/2]

int vpUDPClient::receive ( void *  msg,
size_t  len,
int  timeoutMs = 0 
)

Receive data sent by the server.

Parameters
msg: A message to send over the network.
len: Message length.
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.

Definition at line 241 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

◆ send() [1/2]

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);

Definition at line 301 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

Referenced by vpForceTorqueAtiNetFTSensor::startStreaming(), and vpForceTorqueAtiNetFTSensor::stopStreaming().

◆ send() [2/2]

int vpUDPClient::send ( const void *  msg,
size_t  len 
)

Send data to the server.

Parameters
msg: Message to send.
len: Message length.
Returns
The message length / size of the byte array sent.

Definition at line 330 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

Member Data Documentation

◆ m_is_init