My Project
Loading...
Searching...
No Matches
chai3d::cSocketTCP Class Reference

This class implements all methods specific to TCP sockets.

#include <CSocket.h>

Inheritance diagram for chai3d::cSocketTCP:

Public Member Functions

 cSocketTCP ()
 Constructor of cSocketTCP.
 
 cSocketTCP (int socket)
 Constructor of cSocketTCP using an already opened socket.
 
virtual ~cSocketTCP ()
 Destructor of cSocketTCP.
 
int connect (const char *a_address, unsigned short a_port, bool a_block=true, double a_timeout=-1.0)
 This method connects to a remote server.
 
int listen (unsigned short a_port, bool a_block=true, bool a_loopback=false)
 This method listens for incoming client connections.
 
cSocketTCP * accept (bool a_block=true, double a_timeout=-1.0)
 This method accepts connections from incoming client connection requests.
 
unsigned char getAttributes ()
 This method retrieves the socket attributes.
 
int setAttributes (unsigned char a_attributes)
 This method sets the socket attributes.
 
- Public Member Functions inherited from chai3d::cSocket
 cSocket ()
 
 cSocket (int a_socket)
 
virtual ~cSocket ()
 
int getLocalPort ()
 This method returns the local port number that the socket is connected to.
 
std::string getRemoteIP ()
 This method retrieves the remote IP address that the socket is connected to.
 
int getRemotePort ()
 This method returns the remote port number that the socket is connected to.
 
int close ()
 This method closes the socket.
 
int peek (char *a_output, unsigned int a_size)
 This method reads a copy of incoming data (without removing the data).
 
int peek (std::string &a_output)
 This method reads a copy of incoming data (without removing the data).
 
int read (char *a_output, unsigned int a_size)
 This method reads a incoming data.
 
int read (std::string &a_output)
 This method reads a incoming data.
 
int readLine (char *a_output, unsigned int a_size)
 This method reads an entire line of data (delimited by the '\n' character).
 
int readLine (std::string &a_output)
 This method reads an entire line of data (delimited by the '\n' character).
 
int send (const char *a_data, int a_length)
 This method sends data through the socket.
 
int send (std::string a_data)
 This method sends data through the socket.
 
double getTimeout ()
 This method retrieves the socket timeout in (s).
 
int setTimeout (double a_timeout)
 This method sets the socket timeout in (s).
 
int getSendSize ()
 This method retrieves the socket outgoing buffer size.
 
int setSendSize (int a_size)
 This method sets the socket outgoing buffer size.
 
int getRecvSize ()
 This method retrieves the socket incoming buffer size.
 
int setRecvSize (int a_size)
 This method sets the socket incoming buffer size.
 

Additional Inherited Members

- Static Public Member Functions inherited from chai3d::cSocket
static std::string getLocalIP ()
 This method retrieves the local IP address.
 
- Static Public Attributes inherited from chai3d::cSocket
static const unsigned int BUFFERSIZE = 2048
 This constant defines the default outgoing and incoming socket buffer sizes.
 
static const unsigned char BLOCKING = 0x01
 This constant defines the attribute bit to hold the socket BLOCKING attribute.
 
static const unsigned char REUSEADDR = 0x02
 This constant defines the attribute bit to hold the socket REUSEADDR attribute.
 
static const unsigned char NODELAY = 0x04
 This constant defines the attribute bit to hold the socket NODELAY attribute.
 
static const unsigned char DEFAULT_ATTR = 0x00 | BLOCKING | REUSEADDR | ~NODELAY
 This constant defines the default attributes for new sockets.
 
static const int WOULDBLOCK = -2
 This return constant is used to signify that a non-blocking socket would block.
 
static const int SHORTLINE = -3
 This return constant is used to signify that a buffer is too small to hold a line.
 
static const int TIMEOUT = -4
 This return constant is used to signify that a read operation on a socket has timed out.
 
- Protected Member Functions inherited from chai3d::cSocket
unsigned char getBaseAttributes ()
 This method retrieves the socket attributes shared between TCP and UDP.
 
int setBaseAttributes (unsigned char a_attributes)
 This method sets the socket attributes shared between TCP and UDP.
 
- Protected Attributes inherited from chai3d::cSocket
int m_socket
 Unique socket identifier.
 
unsigned char m_attributes
 Socket attributes.
 
char m_buffer [BUFFERSIZE]
 Socket read buffer.
 
unsigned long m_remoteIP
 Socket remote IP address.
 
unsigned int m_remotePort
 Socket remote port number.
 
unsigned long m_localIP
 Socket local IP address.
 
unsigned int m_localPort
 Socket local port number.
 

Constructor & Destructor Documentation

◆ cSocketTCP() [1/2]

chai3d::cSocketTCP::cSocketTCP ( )

Constructor of cSocketTCP.

◆ cSocketTCP() [2/2]

chai3d::cSocketTCP::cSocketTCP ( int  a_socket)

Destructor of cSocketTCP. This method creates a TCP socket object around an existing socket handle.

Parameters
a_socketA valid handle to an existing socket.

◆ ~cSocketTCP()

chai3d::cSocketTCP::~cSocketTCP ( )
virtual

Destructor of cSocketTCP.

Member Function Documentation

◆ connect()

int chai3d::cSocketTCP::connect ( const char *  a_address,
unsigned short  a_port,
bool  a_block = true,
double  a_timeout = -1.0 
)

This method attempts to connect to a remote TCP server peer at a give IP/port. If the connection is successful, the socket is available for reading and writing data to/from the peer. The system transparently chooses any available local port to initiate the connection to the peer.

Parameters
a_addressPeer remote IP address.
a_portPeer remote port number.
a_blockSet the BLOCKING attribute (true by default).
a_timeoutSet an optional timeout value in seconds. A negative value means that the call will never timeout.
Returns
0 on success, -1 otherwise.

◆ listen()

int chai3d::cSocketTCP::listen ( unsigned short  a_port,
bool  a_block = true,
bool  a_loopback = false 
)

This method configures the socket to listen on a given port for incoming connections from any TCP peers. Incoming connections must be received and configured by cSocketTCP::accept().

Parameters
a_portLocal port number to listen on.
a_blockSet the BLOCKING attribute of the subsequent cSocketTCP::accept() calls.
a_loopbackIf true, causes the socket to bind to the loopback adapter (127.0.0.1), which circumvents (most) firewall limitations and warnings.
Returns
0 on success, -1 otherwise.

◆ accept()

cSocketTCP * chai3d::cSocketTCP::accept ( bool  a_block = true,
double  a_timeout = -1.0 
)

This method listens for incoming TCP connections and spawns a new socket for communication once the communication is established. Calls to accept must be preceded by a call to cSocketTCP::listen(). If cSocketTCP::listen() was passed the BLOCKING attribute, each call to accept() waits for an incoming connection. Otherwise, accept() returns immediately with WOULDBLOCK if no connection requests are pending.

Parameters
a_blockSet the BLOCKING attribute of the returned TCP socket (true by default).
a_timeoutSet an optional timeout value in seconds. A negative value means that the call will never timeout. This parameters is ignored if the socket is non-blocking.
Returns
A pointer to a valid cSocketTCP object connected to the peer making the connection request if successful, NULL otherwise.

◆ getAttributes()

unsigned char chai3d::cSocketTCP::getAttributes ( )
virtual

This method retrieves the socket attributes.

Attributes are single bit flags contained in a single byte. The following attributes are available:

Returns
A single byte containing the socket attributes.

Implements chai3d::cSocket.

◆ setAttributes()

int chai3d::cSocketTCP::setAttributes ( unsigned char  a_attributes)
virtual

This method sets the socket attributes.

Attributes are single bit flags contained in a single byte. The following attributes are available:

Parameters
a_attributesSingle byte containing the desired socket attributes.
Returns
0 on success, -1 otherwise.

Implements chai3d::cSocket.


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