cisst-saw
Loading...
Searching...
No Matches
osaSocket Class Reference

#include <osaSocket.h>

Inheritance diagram for osaSocket:
cmnGenericObject

Public Types

enum  SocketTypes { UDP , TCP }

Public Member Functions

 osaSocket (SocketTypes type=TCP)
 Default constructor.
 ~osaSocket (void)
 Destructor.
int GetIdentifier (void) const
bool AssignPort (unsigned short port)
 Sets the port of a UDP server.
void SetDestination (const std::string &host, unsigned short port)
 Set the destination address for UDP or TCP socket.
void SetDestination (const osaIPandPort &ip_port)
 Set the destination address for UDP or TCP socket.
bool GetDestination (std::string &host, unsigned short &port) const
 Get the destination address for UDP or TCP socket. This is particularly useful for programs using UDP because the the Receive method automatically updates the destination based on the address from which the packet was received. Thus, subsequent Send commands will send to this address.
bool GetDestination (osaIPandPort &ip_port) const
 Get the destination address for UDP or TCP socket. This is particularly useful for programs using UDP because the the Receive method automatically updates the destination based on the address from which the packet was received. Thus, subsequent Send commands will send to this address.
bool Connect (void)
 Connect to the server; required for TCP sockets and should be used after SetDestination().
bool Connect (const std::string &host, unsigned short port)
 Connect to the server; required for TCP sockets; includes call to SetDestination().
bool Connect (const osaIPandPort &ip_port)
 Connect to the server; required for TCP sockets; includes call to SetDestination().
int Send (const char *bufsend, unsigned int msglen, double timeoutSec=0.0)
 Send a byte array via the socket.
int Send (const std::string &bufsend, double timeoutSec=0.0)
 Send a string via the socket.
int SendAsPackets (const char *bufsend, unsigned int msglen, unsigned int packetSize, double timeoutSec=0.0)
 Send a byte array via the socket, possibly in multiple packets based on the specified maximum packet_size. This method can be used with both UDP and TCP, though it is intended for UDP.
int SendAsPackets (const std::string &bufsend, unsigned int packetSize, double timeoutSec=0.0)
 Send a string via the socket, possibly in multiple packets based on the specified maximum packet_size. This method can be used with both UDP and TCP, though it is intended for UDP (only for reliable UDP connections, since there is no check for missing or out of sequence packets).
int Receive (char *bufrecv, unsigned int maxlen, double timeoutSec=0.0)
 Receive a byte array via the socket.
int ReceiveAsPackets (std::string &bufrecv, char *packetBuffer, unsigned int packetSize, double timeoutStartSec=0.0, double timeoutNextSec=0.0)
 Receive a string via the socket, possibly in multiple packets. This method can be used with both UDP and TCP, though it is intended for UDP (only for reliable UDP connections, since there is no check for missing or out of sequence packets).
bool Close (void)
 Close the socket.
bool IsConnected (void)
Public Member Functions inherited from cmnGenericObject
virtual ~cmnGenericObject (void)
virtual const cmnClassServicesBaseServices (void) const =0
bool ReconstructFrom (const cmnGenericObject &other)
std::string ToString (void) const
virtual void ToStream (std::ostream &outputStream) const
virtual void ToStreamRaw (std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const
virtual bool FromStreamRaw (std::istream &inputStream, const char delimiter=' ')
virtual void SerializeRaw (std::ostream &outputStream) const
virtual void DeSerializeRaw (std::istream &inputStream)
virtual cmnLogger::StreamBufTypeGetLogMultiplexer (void) const
virtual size_t ScalarNumber (void) const
virtual bool ScalarNumberIsFixed (void) const
virtual double Scalar (const size_t CMN_UNUSED(index)) const CISST_THROW(std
virtual std::string ScalarDescription (const size_t CMN_UNUSED(index), const std::string &CMN_UNUSED(userDescription)) const

Static Public Member Functions

static std::string GetLocalhostIP (void)
static int GetLocalhostIP (std::vector< std::string > &IPaddress)
 Retrieve IP address of the localhost as string from each network interface (which may be more than two).

Protected Member Functions

 osaSocket (void *socketFDPtr)
 osaSocketServer constructor (for use by osaSocketServer)
unsigned long GetIP (const std::string &host) const

Protected Attributes

SocketTypes SocketType
int SocketFD
bool Connected

Friends

class osaSocketServer

Member Enumeration Documentation

◆ SocketTypes

Enumerator
UDP 
TCP 

Constructor & Destructor Documentation

◆ osaSocket() [1/2]

osaSocket::osaSocket ( SocketTypes type = TCP)

Default constructor.

◆ ~osaSocket()

osaSocket::~osaSocket ( void )

Destructor.

◆ osaSocket() [2/2]

osaSocket::osaSocket ( void * socketFDPtr)
protected

osaSocketServer constructor (for use by osaSocketServer)

Parameters
Voidpointer is used to her avoid including WinSock2.h, the pointer is cast to proper socket in cpp file.

Member Function Documentation

◆ AssignPort()

bool osaSocket::AssignPort ( unsigned short port)

Sets the port of a UDP server.

◆ Close()

bool osaSocket::Close ( void )

Close the socket.

Returns
False if close fails

◆ Connect() [1/3]

bool osaSocket::Connect ( const osaIPandPort & ip_port)

Connect to the server; required for TCP sockets; includes call to SetDestination().

Parameters
ip_portServer's hostname or IP address (e.g. localhost, 127.0.0.1) and port number
Returns
true if the connection was successful

◆ Connect() [2/3]

bool osaSocket::Connect ( const std::string & host,
unsigned short port )

Connect to the server; required for TCP sockets; includes call to SetDestination().

Parameters
hostServer's hostname or IP address (e.g. localhost, 127.0.0.1)
portServer's port number
Returns
true if the connection was successful

◆ Connect() [3/3]

bool osaSocket::Connect ( void )

Connect to the server; required for TCP sockets and should be used after SetDestination().

Returns
true if the connection was successful

◆ GetDestination() [1/2]

bool osaSocket::GetDestination ( osaIPandPort & ip_port) const

Get the destination address for UDP or TCP socket. This is particularly useful for programs using UDP because the the Receive method automatically updates the destination based on the address from which the packet was received. Thus, subsequent Send commands will send to this address.

Parameters
ip_portReference for returning server's IP address (e.g. "127.0.0.1") and port number
Returns
true if destination address was returned

◆ GetDestination() [2/2]

bool osaSocket::GetDestination ( std::string & host,
unsigned short & port ) const

Get the destination address for UDP or TCP socket. This is particularly useful for programs using UDP because the the Receive method automatically updates the destination based on the address from which the packet was received. Thus, subsequent Send commands will send to this address.

Parameters
hostReference for returning server's IP address (e.g. "127.0.0.1")
portReference for returning server's port number
Returns
true if destination address was returned

◆ GetIdentifier()

int osaSocket::GetIdentifier ( void ) const
inline
Returns
Socket file descriptor

◆ GetIP()

unsigned long osaSocket::GetIP ( const std::string & host) const
protected
Returns
IP address (as a number) for the given host

◆ GetLocalhostIP() [1/2]

int osaSocket::GetLocalhostIP ( std::vector< std::string > & IPaddress)
static

Retrieve IP address of the localhost as string from each network interface (which may be more than two).

Parameters
IPaddressescontainer for IP address as string
Returns
Number of IP address retrieved with IPaddresses filled

◆ GetLocalhostIP() [2/2]

std::string osaSocket::GetLocalhostIP ( void )
static
Returns
The first IP address of the localhost as a string

◆ IsConnected()

bool osaSocket::IsConnected ( void )

\ brief Connection state (only works for TCP)

Returns
Returns true if the socket thinks it is connected

◆ Receive()

int osaSocket::Receive ( char * bufrecv,
unsigned int maxlen,
double timeoutSec = 0.0 )

Receive a byte array via the socket.

Parameters
bufrecvBuffer to store received data
maxlenMaximum number of bytes to receive
timeoutSecTimeout in seconds
Returns
Number of bytes received. 0 if timeout is reached and/or no data is received.

◆ ReceiveAsPackets()

int osaSocket::ReceiveAsPackets ( std::string & bufrecv,
char * packetBuffer,
unsigned int packetSize,
double timeoutStartSec = 0.0,
double timeoutNextSec = 0.0 )

Receive a string via the socket, possibly in multiple packets. This method can be used with both UDP and TCP, though it is intended for UDP (only for reliable UDP connections, since there is no check for missing or out of sequence packets).

Parameters
bufrecvString to store received data
packetBufferBuffer to store one packet
packetSizeMaximum packet size, should equal sizeof(packetBuffer)
timeoutStartSecTimeout for receiving first packet, in seconds
timeoutNextSecTimeout for receiving subsequent packets (after first), in seconds
Returns
Number of bytes received. 0 if timeout is reached and/or no data is received.
Note
It can be difficult to determine when a complete set of packets has been received. This function is intended primarily to receive the packet stream produced by SendAsPackets, so it terminates reception in the following cases: (1) when a received packet is smaller than the specified packetSize, (2) on any socket error, and (3) when a timeout occurs. The first condition is best, since it avoids the possibility of appending data from an unrelated packet. Thus, the caller can ensure that the total number of bytes transmitted is not a multiple of the packetSize (e.g., by appending an extra byte).

◆ Send() [1/2]

int osaSocket::Send ( const char * bufsend,
unsigned int msglen,
double timeoutSec = 0.0 )

Send a byte array via the socket.

Parameters
bufsendBuffer holding bytes to be sent
msglenNumber of bytes to send
timeoutSecis the longest time we should wait to send something
Returns
Number of bytes sent (-1 if error)
Note
Since this is a nonblocking call the socket might not be ready to send right away so a short timeout will help in cases when large amount of data is sent around. If the socket is not ready within the timeout then the connection will be closed

◆ Send() [2/2]

int osaSocket::Send ( const std::string & bufsend,
double timeoutSec = 0.0 )

Send a string via the socket.

Parameters
bufsendString to be sent
timeoutSecis the longest time we should wait to send something
Returns
Number of bytes sent (-1 if error)

◆ SendAsPackets() [1/2]

int osaSocket::SendAsPackets ( const char * bufsend,
unsigned int msglen,
unsigned int packetSize,
double timeoutSec = 0.0 )

Send a byte array via the socket, possibly in multiple packets based on the specified maximum packet_size. This method can be used with both UDP and TCP, though it is intended for UDP.

Parameters
bufsendBuffer holding bytes to be sent
packetSizeMaximum packet size
timeoutSecis the longest time we should wait to send something
Returns
Number of bytes sent (-1 if error)
Note
This function aborts if any error occurs and returns the number of bytes actually sent (if any).

◆ SendAsPackets() [2/2]

int osaSocket::SendAsPackets ( const std::string & bufsend,
unsigned int packetSize,
double timeoutSec = 0.0 )

Send a string via the socket, possibly in multiple packets based on the specified maximum packet_size. This method can be used with both UDP and TCP, though it is intended for UDP (only for reliable UDP connections, since there is no check for missing or out of sequence packets).

Parameters
bufsendString to be sent
packetSizeMaximum packet size
timeoutSecis the longest time we should wait to send something
Returns
Number of bytes sent (-1 if error)
Note
This function aborts if any error occurs and returns the number of bytes actually sent (if any).

◆ SetDestination() [1/2]

void osaSocket::SetDestination ( const osaIPandPort & ip_port)

Set the destination address for UDP or TCP socket.

Parameters
ip_portServer's hostname or IP address (e.g. localhost, 127.0.0.1) and port number

◆ SetDestination() [2/2]

void osaSocket::SetDestination ( const std::string & host,
unsigned short port )

Set the destination address for UDP or TCP socket.

Parameters
hostServer's hostname or IP address (e.g. localhost, 127.0.0.1)
portServer's port number

◆ osaSocketServer

friend class osaSocketServer
friend

Member Data Documentation

◆ Connected

bool osaSocket::Connected
protected

◆ SocketFD

int osaSocket::SocketFD
protected

◆ SocketType

SocketTypes osaSocket::SocketType
protected

The documentation for this class was generated from the following file: