Visual Servoing Platform  version 3.4.0
vpUDPClient Class Reference

#include <vpUDPClient.h>

+ Inheritance diagram for vpUDPClient:

Public Member Functions

 vpUDPClient ()
 
 vpUDPClient (const std::string &hostname, int port)
 
virtual ~vpUDPClient ()
 
Inherited functionalities from vpUDPClient
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)
 

Protected Attributes

bool m_is_init
 

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(double dbl, 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 168 of file vpUDPClient.h.

Constructor & Destructor Documentation

vpUDPClient::vpUDPClient ( )

Default constructor.

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

Definition at line 66 of file vpUDPClient.cpp.

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 80 of file vpUDPClient.cpp.

References init().

vpUDPClient::~vpUDPClient ( )
virtual

Destructor.

Definition at line 92 of file vpUDPClient.cpp.

References m_is_init.

Member Function Documentation

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 116 of file vpUDPClient.cpp.

References vpException::fatalError, and m_is_init.

Referenced by vpUDPClient().

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());
Examples:
testUDPClient.cpp.

Definition at line 193 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

Referenced by vpForceTorqueAtiNetFTSensor::waitForNewData().

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 239 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

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 300 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

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

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 329 of file vpUDPClient.cpp.

References m_is_init, and vpException::notInitialized.

Member Data Documentation