UDPClient¶
- class UDPClient(*args, **kwargs)¶
Bases:
pybind11_object
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 <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) {} }; 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; } }
Note
See vpUDPServer
Overloaded function.
__init__(self: visp._visp.core.UDPClient) -> None
Default constructor.
Use connect() to establish the connexion with the server.
__init__(self: visp._visp.core.UDPClient, hostname: str, port: int) -> None
Create a (IPv4) UDP client.
- Parameters:
- hostname
Server hostname or IP address.
- port
Server port number.
Methods
Overloaded function.
Initialize a (IPv4) UDP client.
Receive data sent by the server.
Send data to the server.
Inherited Methods
Operators
__doc__
Overloaded function.
__module__
Attributes
__annotations__
- __init__(*args, **kwargs)¶
Overloaded function.
__init__(self: visp._visp.core.UDPClient) -> None
Default constructor.
Use connect() to establish the connexion with the server.
__init__(self: visp._visp.core.UDPClient, hostname: str, port: int) -> None
Create a (IPv4) UDP client.
- Parameters:
- hostname
Server hostname or IP address.
- port
Server port number.
- receive(self, timeoutMs: int = 0) tuple[int, str] ¶
Receive data sent by the server.
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());
- send(self, msg: str) int ¶
Send data to the server.
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);