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

This class implements all methods specific to UDP sockets.

#include <CSocket.h>

Inheritance diagram for chai3d::cSocketUDP:

Public Member Functions

 cSocketUDP ()
 Constructor of cSocketUDP.
 
virtual ~cSocketUDP ()
 Destructor of cSocketUDP.
 
int listen (unsigned short a_lport, bool a_block=true, bool a_loopback=false)
 This method listens for incoming packets on a given port.
 
int broadcast (unsigned short a_port, const char *a_data, int a_length)
 This method broadcast a packet to a given port.
 
int broadcast (unsigned short a_port, const std::string &a_data)
 This method broadcast a packet to a given port.
 
int setPeer (const char *a_address, unsigned short a_port, bool a_block=true)
 This method assigns a peer (remote) socket to the current socket.
 
int connect (const char *a_address, unsigned short a_port, unsigned short a_localport, bool a_block=true)
 This method connects to a given remote socket.
 
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

◆ cSocketUDP()

chai3d::cSocketUDP::cSocketUDP ( )

Constructor of cSocketUDP.

◆ ~cSocketUDP()

chai3d::cSocketUDP::~cSocketUDP ( )
virtual

Destructor of cSocketUDP.

Member Function Documentation

◆ listen()

int chai3d::cSocketUDP::listen ( unsigned short  a_localPort,
bool  a_block = true,
bool  a_loopback = false 
)

This method opens a UDP socket from a specific local port for listening. Upon successful connection, the socket can be used for reading data from any client sending to it. Successful connections can be restricted to the incoming client by using cSocketUDP::setPeer().

Parameters
a_localPortLocal port number to listen on.
a_blockSet the BLOCKING attribute (true by default).
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.

◆ broadcast() [1/2]

int chai3d::cSocketUDP::broadcast ( unsigned short  a_port,
const char *  a_data,
int  a_length 
)

This methods send a UDP broadcast.

Parameters
a_portTarget broadcast port number.
a_dataBuffer containing the data to be sent.
a_lengthBuffer length in bytes.
Returns
0 on success, -1 otherwise.

◆ broadcast() [2/2]

int chai3d::cSocketUDP::broadcast ( unsigned short  a_port,
const std::string &  a_data 
)

This methods send a UDP broadcast.

Parameters
a_portTarget broadcast port number.
a_dataBuffer containing the data to be sent.
Returns
0 on success, -1 otherwise.

◆ setPeer()

int chai3d::cSocketUDP::setPeer ( const char *  a_address,
unsigned short  a_port,
bool  a_block = true 
)

This method connects a UDP socket already opened using Listen() to a specific peer IP/port. Upon successful connection, the socket can be used for both reading and sending data.

Parameters
a_addressRemote peer IP address.
a_portRemote peer port number.
a_blockSet the BLOCKING attribute (true by default).
Returns
0 on success, -1 otherwise.

◆ connect()

int chai3d::cSocketUDP::connect ( const char *  a_address,
unsigned short  a_port,
unsigned short  a_localPort,
bool  a_block = true 
)

This methods open a UDP socket to a specific peer IP/port and from a specific local port. Upon successful connection, the socket can be used for both reading and sending data.

Parameters
a_addressRemote peer IP address.
a_portRemote peer port number.
a_localPortLocal port number.
a_blockSet the BLOCKING attribute (true by default)
Returns
0 on success, -1 otherwise.

◆ getAttributes()

unsigned char chai3d::cSocketUDP::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::cSocketUDP::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: