Visual Servoing Platform  version 3.4.0
vpUDPServer Class Reference

#include <vpUDPServer.h>

Public Member Functions

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

Detailed Description

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

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 server's code, receiving a basic message and sending an echo message to the client:

#include <cstdlib>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
#include <visp3/core/vpUDPServer.h>
int main() {
try {
int port = 50037;
vpUDPServer server(port);
while (true) {
std::string msg = "", hostInfo = "";
int res = server.receive(msg, hostInfo, 5000);
if (res) {
std::cout << "Server received: " << msg << " from: " << hostInfo << std::endl;
std::cout << "Reply to the client: Echo: " << msg << std::endl;
//Get address and port
std::istringstream iss(hostInfo);
std::vector<std::string> tokens;
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(tokens));
server.send("Echo: " + msg, tokens[1], atoi(tokens[2].c_str()));
} else if (res == 0) {
std::cout << "Receive timeout" << std::endl;
} else {
std::cerr << "Error server.receive()!" << 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 <cstring>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
#include <visp3/core/vpUDPServer.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 {
int port = 50037;
vpUDPServer server(port);
std::string msg = "", hostInfo = "";
int res = server.receive(msg, hostInfo);
if (res) {
DataType data_type;
memcpy(&data_type.double_val, msg.c_str(),
sizeof(data_type.double_val)); memcpy(&data_type.int_val,
msg.c_str()+sizeof(data_type.double_val), sizeof(data_type.int_val));
std::cout << "Server received double_val: " << data_type.double_val << "
; int_val: " << data_type.int_val << " from: " << hostInfo << std::endl;
//Get address and port
std::istringstream iss(hostInfo);
std::vector<std::string> tokens;
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(tokens));
data_type.double_val += 1.5;
data_type.int_val += 2;
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)); msg = std::string(data,
sizeof(data_type.double_val)+sizeof(data_type.int_val));
server.send(msg, tokens[1], atoi(tokens[2].c_str()));
}
return EXIT_SUCCESS;
} catch (const vpException &e) {
std::cerr << "Catch an exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}
See also
vpUDPServer
Examples:
testUDPServer.cpp.

Definition at line 197 of file vpUDPServer.h.

Constructor & Destructor Documentation

vpUDPServer::vpUDPServer ( int  port)

Create a (IPv4) UDP server.

Parameters
port: Server port number.
Note
The server will listen to all the interfaces (see INADDR_ANY).

Definition at line 74 of file vpUDPServer.cpp.

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

Create a UDP server.

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

Definition at line 90 of file vpUDPServer.cpp.

vpUDPServer::~vpUDPServer ( )
virtual

Definition at line 100 of file vpUDPServer.cpp.

References vpException::fatalError.

Member Function Documentation

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

Receive data sent by a client.

Parameters
msg: ASCII message or byte data according to the data sent by the client.
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
See vpUDPClient::receive for an example.
Examples:
testUDPServer.cpp.

Definition at line 195 of file vpUDPServer.cpp.

int vpUDPServer::receive ( std::string &  msg,
std::string &  hostInfo,
int  timeoutMs = 0 
)

Receive data sent by a client.

Parameters
msg: ASCII message or byte data according to the data send by the client.
hostInfo: Information about the client ("client_name client_ip client_port").
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
See vpUDPClient::receive for an example.

Definition at line 213 of file vpUDPServer.cpp.

int vpUDPServer::send ( const std::string &  msg,
const std::string &  hostname,
int  port 
)

Send data to a client.

Parameters
msg: ASCII message or byte data.
hostname: Client hostname (hostname or ip address).
port: Client port number.
Returns
The message length / size of the byte array sent.
Note
See vpUDPClient::send for an example.
Examples:
testUDPServer.cpp.

Definition at line 288 of file vpUDPServer.cpp.

References vpException::fatalError.