Server

class Server(*args, **kwargs)

Bases: Network

This class represents a Transmission Control Protocol (TCP) server.

TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer.

Example of server’s code, receiving and sending basic message. It corresponds to the client used in the first example of vpClient class’ documentation.

#include <iostream>
#include <visp3/core/vpServer.h>

int main(int argc,const char** argv)
{
  int port = 35000;
  vpServer serv(port); //Launch the server on localhost
  serv.start();

  bool run = true;
  int val;

  while(run){
    serv.checkForConnections();

    if(serv.getNumberOfClients() > 0)
    {
      // Receiving a value from the first client
      if(serv.receive(&val) != sizeof(int))
        std::cout << "Error while receiving" << std::endl;
      else
        std::cout << "Received : " << val << std::endl;

      val = val+1;
      // Sending the new value to the first client
      if(serv.send(&val) != sizeof(int))
        std::cout << "Error while sending" << std::endl;
      else
        std::cout << "Sending : " << val << std::endl;
    }
  }

  return 0;
}

Example of server’s code, receiving a vpImage on request form. It correspond to the client used in the second example of vpClient class’ documentation.

#include <visp3/core/vpServer.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayX.h>

#include "vpRequestImage.h" //See vpRequest class documentation

int main(int argc,const char** argv)
{
  int port = 35000;

  std::cout << "Port: " << port << std::endl;
  vpServer serv(port);
  serv.start();

#if defined(VISP_HAVE_X11)
  vpDisplayX display;
#elif defined(VISP_HAVE_GDI) //Win32
  vpDisplayGDI display;
#endif

  vpImage<unsigned char> I;

  vpRequestImage reqImage(&I);
  serv.addDecodingRequest(&reqImage);

  bool run = true;

  while(run){
    serv.checkForConnections();

    if(serv.getNumberOfClients() > 0)
    {
      int index = serv.receiveAndDecodeRequestOnce();
      std::string id = serv.getRequestIdFromIndex(index);

      if(id == reqImage.getId())
      {
#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
        if (! display.isInitialised() )
          display.init(I, -1, -1, "Remote display");
#endif

        vpDisplay::display(I) ;
        vpDisplay::flush(I);

        // A click in the viewer to exit
        if ( vpDisplay::getClick(I, false) )
          run = false;
      }
    }
  }

  return 0;
}

Note

See vpClient

Note

See vpRequest

Note

See vpNetwork

Overloaded function.

  1. __init__(self: visp._visp.core.Server) -> None

Construct a server on the machine launching it.

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

  2. __init__(self: visp._visp.core.Server, adress_serv: str, port_serv: int) -> None

Construct a server on the machine at a given adress, with a specified port.

Parameters:
adress_serv

server’s adress.

port_serv

server’s port.

Methods

__init__

Overloaded function.

checkForConnections

Check if a client has connected or deconnected the server

getMaxNumberOfClients

Get the maximum number of clients that can be connected to the server.

getNumberOfClients

Get the number of clients connected to the server.

isStarted

Check if the server is started.

print

Overloaded function.

setMaxNumberOfClients

Set the maximum number of clients that can be connected to the server.

start

Enable the server to wait for clients (on the limit of the maximum limit).

Inherited Methods

removeDecodingRequest

Delete a decoding request from the emitter.

sendRequestTo

Send a request to a specific receptor.

sendRequest

Send a request to the first receptor in the list.

setTimeoutSec

Change the time the emitter spend to check if he receives a message from a receptor.

receiveAndDecodeRequestFrom

Receives and decode requests, from a specific emitter, until there is request to receive.

receiveAndDecodeRequestOnceFrom

Receives a message once (in the limit of the Maximum message size value), from a specific emitter.

sendAndEncodeRequestTo

Send and encode a request to a specific receptor.

receiveRequest

Receive requests until there is requests to receive.

setMaxSizeReceivedMessage

Change the maximum size that the emitter can receive (in request mode).

receiveAndDecodeRequestOnce

Receives a message once (in the limit of the Maximum message size value).

setVerbose

Set the verbose mode.

receiveRequestOnceFrom

Receives a message once (in the limit of the Maximum message size value), from a specific emitter.

setTimeoutUSec

Change the time the emitter spend to check if he receives a message from a receptor.

receiveRequestFrom

Receives requests, from a specific emitter, until there is request to receive.

sendAndEncodeRequest

Send and encode a request to the first receptor in the list.

getReceptorIndex

Get the receptor index from its name.

receiveAndDecodeRequest

Receives and decode requests until there is requests to receive.

getMaxSizeReceivedMessage

Get the maximum size that the emitter can receive (in request mode).

getRequestIdFromIndex

Get the Id of the request at the index ind.

receiveRequestOnce

Receives a message once (in the limit of the Maximum message size value).

Operators

__doc__

__init__

Overloaded function.

__module__

Attributes

__annotations__

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: visp._visp.core.Server) -> None

Construct a server on the machine launching it.

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

  2. __init__(self: visp._visp.core.Server, adress_serv: str, port_serv: int) -> None

Construct a server on the machine at a given adress, with a specified port.

Parameters:
adress_serv

server’s adress.

port_serv

server’s port.

checkForConnections(self) bool

Check if a client has connected or deconnected the server

Returns:

True if a client connected or deconnected, false otherwise OR server not started yet.

getMaxNumberOfClients(self) int

Get the maximum number of clients that can be connected to the server.

Returns:

Maximum number of clients.

getMaxSizeReceivedMessage(self) int

Get the maximum size that the emitter can receive (in request mode).

Returns:

Acutal max size value.

getNumberOfClients(self) int

Get the number of clients connected to the server.

Returns:

Number of clients connected.

getReceptorIndex(self, name: str) int

Get the receptor index from its name. The name can be either the IP, or its name on the network.

Parameters:
name: str

Name of the receptor.

Returns:

Index of the receptor, or -1 if an error occurs.

getRequestIdFromIndex(self, ind: int) str

Get the Id of the request at the index ind.

Parameters:
ind: int

Index of the request.

Returns:

Id of the request.

isStarted(self) bool

Check if the server is started.

Returns:

True if the server is started, false otherwise.

print(*args, **kwargs)

Overloaded function.

  1. print(self: visp._visp.core.Server) -> None

Print the connected clients.

  1. print(self: visp._visp.core.Network, id: str = ) -> None

Print the receptors.

Parameters:
id

Message to display before the receptor’s index.

receiveAndDecodeRequest(self) list[int]

Receives and decode requests until there is requests to receive.

Warning

Requests will be received but not decoded.

Note

See vpNetwork::receive()

Note

See vpNetwork::receiveRequestFrom()

Note

See vpNetwork::receiveRequestOnce()

Note

See vpNetwork::receiveRequestOnceFrom()

Note

See vpNetwork::receiveAndDecodeRequest()

Note

See vpNetwork::receiveAndDecodeRequestFrom()

Note

See vpNetwork::receiveAndDecodeRequestOnce()

Note

See vpNetwork::receiveAndDecodeRequestOnceFrom()

receiveAndDecodeRequestFrom(self, receptorEmitting: int) list[int]

Receives and decode requests, from a specific emitter, until there is request to receive.

Warning

Requests will be received but not decoded.

Note

See vpNetwork::receive()

Note

See vpNetwork::receiveRequest()

Note

See vpNetwork::receiveRequestOnce()

Note

See vpNetwork::receiveRequestOnceFrom()

Note

See vpNetwork::receiveAndDecodeRequest()

Note

See vpNetwork::receiveAndDecodeRequestFrom()

Note

See vpNetwork::receiveAndDecodeRequestOnce()

Note

See vpNetwork::receiveAndDecodeRequestOnceFrom()

Parameters:
receptorEmitting: int

Index of the receptor emitting the message

receiveAndDecodeRequestOnce(self) int

Receives a message once (in the limit of the Maximum message size value). This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request. If it represents an entire request, it decodes the request.

Returns:

The number of bytes received, -1 if an error occurred.

receiveAndDecodeRequestOnceFrom(self, receptorEmitting: int) int

Receives a message once (in the limit of the Maximum message size value), from a specific emitter. This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request. If it represents an entire request, it decodes the request.

Parameters:
receptorEmitting: int

Index of the receptor emitting the message.

Returns:

The number of bytes received, -1 if an error occurred.

receiveRequest(self) list[int]

Receive requests until there is requests to receive.

Warning

Requests will be received but not decoded.

Note

See vpNetwork::receive()

Note

See vpNetwork::receiveRequestFrom()

Note

See vpNetwork::receiveRequestOnce()

Note

See vpNetwork::receiveRequestOnceFrom()

Note

See vpNetwork::receiveAndDecodeRequest()

Note

See vpNetwork::receiveAndDecodeRequestFrom()

Note

See vpNetwork::receiveAndDecodeRequestOnce()

Note

See vpNetwork::receiveAndDecodeRequestOnceFrom()

receiveRequestFrom(self, receptorEmitting: int) list[int]

Receives requests, from a specific emitter, until there is request to receive.

Warning

Requests will be received but not decoded.

Note

See vpNetwork::receive()

Note

See vpNetwork::receiveRequest()

Note

See vpNetwork::receiveRequestOnce()

Note

See vpNetwork::receiveRequestOnceFrom()

Note

See vpNetwork::receiveAndDecodeRequest()

Note

See vpNetwork::receiveAndDecodeRequestFrom()

Note

See vpNetwork::receiveAndDecodeRequestOnce()

Note

See vpNetwork::receiveAndDecodeRequestOnceFrom()

Parameters:
receptorEmitting: int

Index of the receptor emitting the message

receiveRequestOnce(self) int

Receives a message once (in the limit of the Maximum message size value). This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request.

Returns:

The number of bytes received, -1 if an error occurred.

receiveRequestOnceFrom(self, receptorEmitting: int) int

Receives a message once (in the limit of the Maximum message size value), from a specific emitter. This message can represent an entire request or not. Several calls to this function might be necessary to get the entire request.

Parameters:
receptorEmitting: int

Index of the receptor emitting the message.

Returns:

The number of bytes received, -1 if an error occurred.

removeDecodingRequest(self, arg0: str) None

Delete a decoding request from the emitter.

Note

See vpNetwork::addDecodingRequest()

sendAndEncodeRequest(self, req: visp._visp.core.Request) int

Send and encode a request to the first receptor in the list.

Parameters:
req: visp._visp.core.Request

Request to send.

Returns:

The number of bytes that have been sent, -1 if an error occurred.

sendAndEncodeRequestTo(self, req: visp._visp.core.Request, dest: int) int

Send and encode a request to a specific receptor.

Parameters:
req: visp._visp.core.Request

Request to send.

dest: int

Index of the receptor receiving the request.

Returns:

The number of bytes that have been sent, -1 if an error occurred.

sendRequest(self, req: visp._visp.core.Request) int

Send a request to the first receptor in the list.

Parameters:
req: visp._visp.core.Request

Request to send.

Returns:

The number of bytes that have been sent, -1 if an error occurred.

sendRequestTo(self, req: visp._visp.core.Request, dest: int) int

Send a request to a specific receptor.

Parameters:
req: visp._visp.core.Request

Request to send.

dest: int

Index of the receptor receiving the request.

Returns:

The number of bytes that have been sent, -1 if an error occurred.

setMaxNumberOfClients(self, l: int) None

Set the maximum number of clients that can be connected to the server.

Note

See vpServer::getMaxNumberOfClients()

Parameters:
l: int

Maximum number of clients.

setMaxSizeReceivedMessage(self, s: int) None

Change the maximum size that the emitter can receive (in request mode).

Note

See vpNetwork::getMaxSizeReceivedMessage()

Parameters:
s: int

new maximum size value.

setTimeoutSec(self, sec: int) None

Change the time the emitter spend to check if he receives a message from a receptor. Initially this value is set to 10usec.

Note

See vpNetwork::setTimeoutUSec()

Parameters:
sec: int

new value in second.

setTimeoutUSec(self, usec: int) None

Change the time the emitter spend to check if he receives a message from a receptor. Initially this value is set to 10usec.

Note

See vpNetwork::setTimeoutSec()

Parameters:
usec: int

new value in micro second.

setVerbose(self, mode: bool) None

Set the verbose mode.

Parameters:
mode: bool

Change the verbose mode. True to turn on, False to turn off.

start(self) bool

Enable the server to wait for clients (on the limit of the maximum limit).

Returns:

True if the server has started, false otherwise.