UDPServer

class UDPServer(*args, **kwargs)

Bases: pybind11_object

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 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 {
    int port = 50037;
    vpUDPServer server(port);

    std::string msg = "", hostInfo = "";
    int res = server.receive(msg, hostInfo);
    if (res) {
      vpDataType_t 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;
  }
}

Note

See vpUDPServer

Overloaded function.

  1. __init__(self: visp._visp.core.UDPServer, port: int) -> None

Create a (IPv4) UDP server.

Note

The server will listen to all the interfaces (see INADDR_ANY).

Parameters:
port

Server port number.

  1. __init__(self: visp._visp.core.UDPServer, hostname: str, port: int) -> None

Create a UDP server.

Parameters:
hostname

Server hostname or IP address.

port

Server port number.

Methods

__init__

Overloaded function.

receive

Overloaded function.

send

Send data to a client.

Inherited Methods

Operators

__doc__

__init__

Overloaded function.

__module__

Attributes

__annotations__

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: visp._visp.core.UDPServer, port: int) -> None

Create a (IPv4) UDP server.

Note

The server will listen to all the interfaces (see INADDR_ANY).

Parameters:
port

Server port number.

  1. __init__(self: visp._visp.core.UDPServer, hostname: str, port: int) -> None

Create a UDP server.

Parameters:
hostname

Server hostname or IP address.

port

Server port number.

receive(*args, **kwargs)

Overloaded function.

  1. receive(self: visp._visp.core.UDPServer, timeoutMs: int = 0) -> tuple[int, str]

Receive data sent by a client.

Note

See vpUDPClient::receive for an example.

Parameters:
timeoutMs

Timeout in millisecond (if zero, the call is blocking).

Returns:

A tuple containing:

  • The message length / size of the byte array sent received, or -1 if there is an error, or 0 if there is a timeout.

  • msg: ASCII message or byte data according to the data sent by the client.

  1. receive(self: visp._visp.core.UDPServer, timeoutMs: int = 0) -> tuple[int, str, str]

Receive data sent by a client.

Note

See vpUDPClient::receive for an example.

Parameters:
timeoutMs

Timeout in millisecond (if zero, the call is blocking).

Returns:

A tuple containing:

  • The message length / size of the byte array sent received, or -1 if there is an error, or 0 if there is a timeout.

  • 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”).

send(self, msg: str, hostname: str, port: int) int

Send data to a client.

Note

See vpUDPClient::send for an example.

Parameters:
msg: str

ASCII message or byte data.

hostname: str

Client hostname (hostname or ip address).

port: int

Client port number.

Returns:

The message length / size of the byte array sent.