cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
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. More...
 
 ~osaSocket (void)
 Destructor. More...
 
int GetIdentifier (void) const
 
bool AssignPort (unsigned short port)
 Sets the port of a UDP server. More...
 
void SetDestination (const std::string &host, unsigned short port)
 Set the destination address for UDP or TCP socket. More...
 
void SetDestination (const osaIPandPort &ip_port)
 Set the destination address for UDP or TCP socket. More...
 
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. More...
 
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. More...
 
bool Connect (void)
 Connect to the server; required for TCP sockets and should be used after SetDestination() More...
 
bool Connect (const std::string &host, unsigned short port)
 Connect to the server; required for TCP sockets; includes call to SetDestination() More...
 
bool Connect (const osaIPandPort &ip_port)
 Connect to the server; required for TCP sockets; includes call to SetDestination() More...
 
int Send (const char *bufsend, unsigned int msglen, double timeoutSec=0.0)
 Send a byte array via the socket. More...
 
int Send (const std::string &bufsend, double timeoutSec=0.0)
 Send a string via the socket. More...
 
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. More...
 
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). More...
 
int Receive (char *bufrecv, unsigned int maxlen, double timeoutSec=0.0)
 Receive a byte array via the socket. More...
 
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). More...
 
bool Close (void)
 Close the socket. More...
 
bool IsConnected (void)
 
- Public Member Functions inherited from cmnGenericObject
virtual ~cmnGenericObject (void)
 
virtual const
cmnClassServicesBase
Services (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 throw (std::out_of_range)
 
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) More...
 

Protected Member Functions

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

Protected Attributes

SocketTypes SocketType
 
int SocketFD
 
bool Connected
 

Friends

class osaSocketServer
 

Member Enumeration Documentation

Enumerator
UDP 
TCP 

Constructor & Destructor Documentation

osaSocket::osaSocket ( SocketTypes  type = TCP)

Default constructor.

osaSocket::~osaSocket ( void  )

Destructor.

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

bool osaSocket::AssignPort ( unsigned short  port)

Sets the port of a UDP server.

bool osaSocket::Close ( void  )

Close the socket.

Returns
False if close fails
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
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
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
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
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
int osaSocket::GetIdentifier ( void  ) const
inline
Returns
Socket file descriptor
unsigned long osaSocket::GetIP ( const std::string &  host) const
protected
Returns
IP address (as a number) for the given host
static std::string osaSocket::GetLocalhostIP ( void  )
static
Returns
The first IP address of the localhost as a string
static 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
bool osaSocket::IsConnected ( void  )

\ brief Connection state (only works for TCP)

Returns
Returns true if the socket thinks it is connected
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.
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).
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
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)
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).
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).
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
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

Friends And Related Function Documentation

friend class osaSocketServer
friend

Member Data Documentation

bool osaSocket::Connected
protected
int osaSocket::SocketFD
protected
SocketTypes osaSocket::SocketType
protected

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