Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes

CxUtils::Socket Class Reference

Basic socket structure for creating network connections. All specific sockets inherit from this class (UDP, TCP). More...

#include <socket.h>

Inheritance diagram for CxUtils::Socket:
Inheritance graph
[legend]

List of all members.

Public Types

enum  Type {
  Undefined = 0, TCPListen, TCPServer, TCPClient,
  UDPServer, UDPClient
}

Public Member Functions

 Socket ()
 Default constructor, initializes any shared libraries such as winsock in Windows.
 Socket (const Socket &arg)
 Copy constructor, initializes any shared libraries such as winsock in Windows.
virtual ~Socket ()
 Destructor, checks for the deinitialization of winsock dll in Windows.
virtual void Shutdown ()
 Closes the socket, and clears service information.
virtual int Send (const Packet &packet) const
 Sends the data in the packet structure over the socket.
virtual int Send (const char *buffer, const unsigned int length) const
 Sends the data in the byte array over the socket.
int Recv (Packet &packet, const unsigned int receiveSize, const long int timeOutMilliseconds, IPAddress *ipAddress=NULL, unsigned short *port=NULL) const
 Receives data and puts it in the packet.
int Recv (char *buffer, const unsigned int length, const long int timeOutMilliseconds, IPAddress *ipAddress=NULL, unsigned short *port=NULL) const
 Receives data and puts it in the buffer.
bool IsValid () const
 Returns true if a valid socket has been created.
bool SetNetworkInterface (const int num)
 Sets the network interface to use for sending/receiving data on the socket.
bool SetNetworkInterface (const IP4Address &ipAddress)
 This method looks up the network connection number and then calls the other SetNetworkInterface method with it.
int GetSocket () const
 Returns the socket file descriptor data.
Type GetType () const
 Returns what type of socket the file descripter is.
int GetNetworkInterface () const
 Returns the interface number to use. -1 means any interface. Interface numbers are indexed with 0 being the first, 1 the second, etc.
Socketoperator= (const Socket &arg)
 Operator overloading for operator =.

Static Public Member Functions

static unsigned int GetHostAddresses (IP4Address::List &ipAddresses)
 Discovers the number of IP address for the different network connections on the host machine.
static bool IsHostAddress (const IP4Address &ipAddress)
 Checks to see if an IP address is on the local machine.
static int GetNetworkInterface (const IP4Address &ipAddress)
 This method looks up the network connection number based on the IP address.
static unsigned int GetNumNetworkConnections ()
 Discovers the number of physical device interfaces on host machine (number of NIC connections).
static int IsIncommingData (const Socket *sock, const long int timeOutMilliseconds=0)
 Checks the socket to see if there is any incomming data.

Protected Member Functions

virtual int SendFromBuffer (const char *buffer, const unsigned int length) const =0
virtual int RecvToBuffer (char *buffer, const unsigned int length, const long int timeOutMilliseconds=0, IPAddress *ipAddress=NULL, unsigned short *port=NULL) const =0

Static Protected Member Functions

static void InitializeSockets ()
 Makes sure any socket shared libraries have been loaded.

Protected Attributes

int mSocket
 Socket file descripter.
Type mSocketType
 Type of socket.
int mServiceLength
 Size of service data structure in bytes.
int mNetworkInterface
 Which network interface to use -1 = any, [0, n).
bool mGoodSocket
 Valid socket type.
sockaddr_in mService
 Type of socket and service being used.

Detailed Description

Basic socket structure for creating network connections. All specific sockets inherit from this class (UDP, TCP).

Definition at line 76 of file socket.h.


Member Enumeration Documentation

Enumerator:
Undefined 
TCPListen 
TCPServer 
TCPClient 
UDPServer 
UDPClient 

Definition at line 79 of file socket.h.


Constructor & Destructor Documentation

Socket::Socket (  )

Default constructor, initializes any shared libraries such as winsock in Windows.

Definition at line 67 of file socket.cpp.

Socket::Socket ( const Socket arg )

Copy constructor, initializes any shared libraries such as winsock in Windows.

Definition at line 102 of file socket.cpp.

Socket::~Socket (  ) [virtual]

Destructor, checks for the deinitialization of winsock dll in Windows.

Definition at line 138 of file socket.cpp.


Member Function Documentation

unsigned int Socket::GetHostAddresses ( IP4Address::List ipAddresses ) [static]

Discovers the number of IP address for the different network connections on the host machine.

Parameters:
[out]ipAddressesList of IP4 Addresses detected.
Returns:
Number of address found, 0 if no network interface present.

Definition at line 415 of file socket.cpp.

int Socket::GetNetworkInterface (  ) const

Returns the interface number to use. -1 means any interface. Interface numbers are indexed with 0 being the first, 1 the second, etc.

Definition at line 402 of file socket.cpp.

int Socket::GetNetworkInterface ( const IP4Address ipAddress ) [static]

This method looks up the network connection number based on the IP address.

Parameters:
ipAddressThe IP Address of an existing connection to get number of.
Returns:
-1 on failure, 0,N where N is the Nth network connection on the host.

Definition at line 342 of file socket.cpp.

unsigned int Socket::GetNumNetworkConnections (  ) [static]

Discovers the number of physical device interfaces on host machine (number of NIC connections).

Returns:
Number of address found.

Definition at line 484 of file socket.cpp.

int Socket::GetSocket (  ) const

Returns the socket file descriptor data.

Definition at line 368 of file socket.cpp.

Socket::Type Socket::GetType (  ) const

Returns what type of socket the file descripter is.

Returns:
Undefined, TCPClient, TCPListen, TCPServer, UDPClient, UDPServer.

Definition at line 389 of file socket.cpp.

void Socket::InitializeSockets (  ) [static, protected]

Makes sure any socket shared libraries have been loaded.

Definition at line 602 of file socket.cpp.

bool Socket::IsHostAddress ( const IP4Address ipAddress ) [static]

Checks to see if an IP address is on the local machine.

Parameters:
[in]ipAddressIP Address to check against list of local addresses.
Returns:
True if the IP address is part of the local host machine, otherwise false.

Definition at line 501 of file socket.cpp.

int Socket::IsIncommingData ( const Socket sock,
const long int  tms = 0 
) [static]

Checks the socket to see if there is any incomming data.

Parameters:
sockThe socket to check for incomming data.
tmsTime to wait in milliseconds. If 0, then method always returns 1 to support blocked receiving.
Returns:
0 if no data incomming, 1 if data is ready for reading.

Definition at line 533 of file socket.cpp.

bool Socket::IsValid (  ) const

Returns true if a valid socket has been created.

Definition at line 286 of file socket.cpp.

Socket & Socket::operator= ( const Socket arg )

Operator overloading for operator =.

Definition at line 586 of file socket.cpp.

int Socket::Recv ( char *  buffer,
const unsigned int  length,
const long int  timeOutMilliseconds,
IPAddress ipAddress = NULL,
unsigned short *  port = NULL 
) const

Receives data and puts it in the buffer.

Parameters:
[in]bufferBuffer to put received data in.
[in]lengthMaximum amount of data to receive in bytes.
[in]timeOutMillisecondsThe timeout in milliseconds, 0 = INFINITE.
[out]ipAddressIf the socket you are using supports retriving the IP address of the sender (UDP), then that value will be saved here if not NULL.
[out]portThe senders source port the sent from. Optional parameter set if not NULL, only works with UDP sockets.
Returns:
Number of bytes received, <= 0 on failure.

Definition at line 272 of file socket.cpp.

int Socket::Recv ( Packet packet,
const unsigned int  receiveSize,
const long int  timeOutMilliseconds,
IPAddress ipAddress = NULL,
unsigned short *  port = NULL 
) const

Receives data and puts it in the packet.

Parameters:
[in]packetThe packet data to put received data into.
[in]receiveSizeThe number of bytes to try and receive.
[in]timeOutMillisecondsThe timeout in milliseconds, 0 = INFINITE.
[out]ipAddressIf the socket you are using supports retriving the IP address of the sender (UDP), then that value will be saved here if not NULL.
[out]portThe senders source port the sent from. Optional parameter set if not NULL, only works with UDP sockets.
Returns:
Number of bytes received, < 0 on error.

Definition at line 232 of file socket.cpp.

virtual int CxUtils::Socket::RecvToBuffer ( char *  buffer,
const unsigned int  length,
const long int  timeOutMilliseconds = 0,
IPAddress ipAddress = NULL,
unsigned short *  port = NULL 
) const [protected, pure virtual]
int Socket::Send ( const Packet packet ) const [virtual]

Sends the data in the packet structure over the socket.

Parameters:
[in]packetThe packet data to send.
Returns:
Number of bytes sent, <= 0 on failure.

Definition at line 194 of file socket.cpp.

int Socket::Send ( const char *  buffer,
const unsigned int  length 
) const [virtual]

Sends the data in the byte array over the socket.

Parameters:
[in]bufferPointer to buffered data to transmit.
[in]lengthThe length of the buffer in bytes.
Returns:
Number of bytes sent, <= 0 on failure.

Definition at line 210 of file socket.cpp.

virtual int CxUtils::Socket::SendFromBuffer ( const char *  buffer,
const unsigned int  length 
) const [protected, pure virtual]
bool Socket::SetNetworkInterface ( const IP4Address ipAddress )

This method looks up the network connection number and then calls the other SetNetworkInterface method with it.

Parameters:
ipAddressThe IP Address of an existing connection to use.
Returns:
True on success, false on failure.

Definition at line 320 of file socket.cpp.

bool Socket::SetNetworkInterface ( const int  num )

Sets the network interface to use for sending/receiving data on the socket.

Parameters:
numInterface number to use. -1 is any, 0 the first, 1 the second, etc.
Returns:
True on success, false on failure.

Definition at line 303 of file socket.cpp.

void Socket::Shutdown (  ) [virtual]

Closes the socket, and clears service information.

Reimplemented in CxUtils::TcpClient, CxUtils::UdpClient, and CxUtils::UdpServer.

Definition at line 159 of file socket.cpp.


Member Data Documentation

bool CxUtils::Socket::mGoodSocket [protected]

Valid socket type.

Definition at line 139 of file socket.h.

Which network interface to use -1 = any, [0, n).

Definition at line 138 of file socket.h.

sockaddr_in CxUtils::Socket::mService [protected]

Type of socket and service being used.

Definition at line 140 of file socket.h.

Size of service data structure in bytes.

Definition at line 137 of file socket.h.

int CxUtils::Socket::mSocket [protected]

Socket file descripter.

Definition at line 134 of file socket.h.

Type of socket.

Definition at line 136 of file socket.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines