Basic socket structure for creating network connections. All specific sockets inherit from this class (UDP, TCP). More...
#include <socket.h>
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. | |
Socket & | operator= (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. |
Basic socket structure for creating network connections. All specific sockets inherit from this class (UDP, TCP).
Definition at line 76 of file socket.h.
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.
unsigned int Socket::GetHostAddresses | ( | IP4Address::List & | ipAddresses ) | [static] |
Discovers the number of IP address for the different network connections on the host machine.
[out] | ipAddresses | List of IP4 Addresses detected. |
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.
ipAddress | The IP Address of an existing connection to get number of. |
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).
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.
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.
[in] | ipAddress | IP Address to check against list of local addresses. |
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.
sock | The socket to check for incomming data. |
tms | Time to wait in milliseconds. If 0, then method always returns 1 to support blocked receiving. |
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.
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.
[in] | buffer | Buffer to put received data in. |
[in] | length | Maximum amount of data to receive in bytes. |
[in] | timeOutMilliseconds | The timeout in milliseconds, 0 = INFINITE. |
[out] | ipAddress | If 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] | port | The senders source port the sent from. Optional parameter set if not NULL, only works with UDP sockets. |
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.
[in] | packet | The packet data to put received data into. |
[in] | receiveSize | The number of bytes to try and receive. |
[in] | timeOutMilliseconds | The timeout in milliseconds, 0 = INFINITE. |
[out] | ipAddress | If 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] | port | The senders source port the sent from. Optional parameter set if not NULL, only works with UDP sockets. |
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.
[in] | packet | The packet data to send. |
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.
[in] | buffer | Pointer to buffered data to transmit. |
[in] | length | The length of the buffer in bytes. |
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.
ipAddress | The IP Address of an existing connection to use. |
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.
num | Interface number to use. -1 is any, 0 the first, 1 the second, etc. |
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.
bool CxUtils::Socket::mGoodSocket [protected] |
int CxUtils::Socket::mNetworkInterface [protected] |
sockaddr_in CxUtils::Socket::mService [protected] |
int CxUtils::Socket::mServiceLength [protected] |
int CxUtils::Socket::mSocket [protected] |
Type CxUtils::Socket::mSocketType [protected] |