Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

wkgl::Socket Class Reference

This class allows you to send information and recieve information using a TCP/IP connection. More...

#include <Socket.h>

List of all members.

Public Methods

 Socket ()
 Creates an empty invalid socket, used by ServerSocket.

 Socket (const char *addr, UINT port, SFLAG addr_type=STYPE_IP)
 Creates a socket that will connect to the given remote computer.

 Socket (const Socket &s)
 Copy Constructor.

 Socket (SOCKET s)
 Creates a socket based on a winsock handle, used by ServerSocket.

 ~Socket (void)
virtual int connect ()
 Connects to a remote computer.

virtual int send (const char *buf, UINT len)
 Sends the given information across the socket.

virtual int recv (char *buf, UINT len)
 Recieves the given information from the remote computer.

virtual int close ()
 Disconnects the socket.

virtual int operator<< (const char *data)
 This operator allows you to send a null terminated string.

virtual int operator<< (char data)
 This operator allows you to send byte of data.

virtual int operator>> (char &c)
 This operator allows you to recieve a byte from the socket.

virtual int operator>> (char *&data)
 This operator allows you to recieve a pointer to bytes from the socket.

virtual BOOL isConnected ()
 Returns whether the socket is currently connected.


Static Public Methods

char * getLocalHost ()
 Returns the name of the local host.

const char * getLastError ()
 Returns a string representing the last error.

void reportError (const char *where, const char *desc, const char *code, BOOL winerror=TRUE)
 Creates a fomated string that represents an error.

void initWinSock ()
void cleanup ()
void addRef ()
void releaseRef ()
UINT getRefCount ()

Protected Attributes

SOCKET sock
 The handle to the winsock socket.

char * addr
 The address of the remote computer.

UINT port
 The port to connect to.

SFLAG addr_type
 The type of address contained in addr.


Static Protected Attributes

char lasterror [256] = {0}
 String representaion of the last error.

UINT ref_count = 0
 Counts all the references to Socket, used to calculate when to do clean-up.

BOOL winsock_init = FALSE
 Flag specifying weather winsock has been initialized or not.


Detailed Description

This class allows you to send information and recieve information using a TCP/IP connection.

Author:
Micheal Nooner. NOTE: Be sure to link to wsock32.lib or its equivelent. This class allows for a number of ways to construct a connection to a remote host. The most common way is to provide the address and port number of the host you want to connect to. For example:
   Socket s( "www.yahoo.com", 80, STYPE_HOSTNAME );
   . . .  //Do other buisness here.
   char buf[256];
   int len = s.recv( buf, 256 );
   if ( len > 0 )
     cout << buf << endl;
   . . . //Do other buisness here.
   s.close();
This example create a socket to yahoo and recieves the web page into buf. Alternatively you can specify an dot-form IP address. If you wish to recieve a connection from a host use the ServerSocket class, the accept method will return a socket so you can send and receive data just like any other connection.


Constructor & Destructor Documentation

Socket::Socket  
 

Creates an empty invalid socket, used by ServerSocket.

Socket::Socket const char *    addr,
UINT    port,
SFLAG    addr_type = STYPE_IP
 

Creates a socket that will connect to the given remote computer.

Parameters:
addr  The address of the host.
port  The port to connect to.
addr_type  The address type; either STYPE_IP or STYPE_HOSTNAME.

Socket::Socket const Socket &    s
 

Copy Constructor.

Parameters:
s  The socket to copy.

Socket::Socket SOCKET    s
 

Creates a socket based on a winsock handle, used by ServerSocket.

Parameters:
s  The winsock socket handle to use.

Socket::~Socket void   
 


Member Function Documentation

void Socket::addRef   [static]
 

void Socket::cleanup   [static]
 

int Socket::close   [virtual]
 

Disconnects the socket.

Returns:
SERROR_NOERROR if there was no error, otherwise it will return SERROR_NOCLOSE.

int Socket::connect   [virtual]
 

Connects to a remote computer.

Returns:
SERROR_NOERROR if there was no error otherwise it will return either SERROR_TYPE, SERROR_HOST, SERROR_NOCREATE, SERROR_CONNECT.

const char * Socket::getLastError   [static]
 

Returns a string representing the last error.

Returns:
The text found in Socket::lasterror.

char * Socket::getLocalHost   [static]
 

Returns the name of the local host.

For Example:

   Socket s( Socket::getLocalHost(), 8080, STYPE_HOSTNAME );
Returns:
the name of the local host.

UINT Socket::getRefCount   [static]
 

void Socket::initWinSock   [static]
 

BOOL Socket::isConnected   [virtual]
 

Returns whether the socket is currently connected.

int Socket::operator<< char    data [virtual]
 

This operator allows you to send byte of data.

Parameters:
data  the data to send
Returns:
SERROR_NOERROR if there was no error otherwise it will return either SERROR_SEND or SERROR_CONNECT.

int Socket::operator<< const char *    data [virtual]
 

This operator allows you to send a null terminated string.

Parameters:
data  the data to send
Returns:
SERROR_NOERROR if there was no error otherwise it will return either SERROR_SEND or SERROR_CONNECT.

int Socket::operator>> char *&    data [virtual]
 

This operator allows you to recieve a pointer to bytes from the socket.

When this method returns successfully data will point to a new memory location that you will have to delete.

Parameters:
data  The pointer that will point at the data.
Returns:
The number of bytes recieved if there was no error (zero bytes indicates you have reached the end of the buffer), otherwise it will return either SERROR_RECV or SERROR_CONNECT.

int Socket::operator>> char &    c [virtual]
 

This operator allows you to recieve a byte from the socket.

Parameters:
c  The character that will receive the byte.
Returns:
The number of bytes recieved if there was no error (zero bytes indicates you have reached the end of the buffer), otherwise it will return either SERROR_RECV or SERROR_CONNECT.

int Socket::recv char *    buf,
UINT    len
[virtual]
 

Recieves the given information from the remote computer.

Parameters:
buf  A buffer to recieve the bytes from the host.
len  The length of buf.
Returns:
The number of bytes recieved if there was no error (zero bytes indicates you have reached the end of the buffer), otherwise it will return either SERROR_RECV or SERROR_CONNECT.

void Socket::releaseRef   [static]
 

void Socket::reportError const char *    where,
const char *    desc,
const char *    code,
BOOL    winerror = TRUE
[static]
 

Creates a fomated string that represents an error.

Parameters:
where  The location of the error.
desc  A brief description of the error.
code  "SERROR_XXX"
winerror  If TRUE then the error being reported is a winsock error.

int Socket::send const char *    buf,
UINT    len
[virtual]
 

Sends the given information across the socket.

Parameters:
buf  The bytes to send to the host.
len  The length of buf.
Returns:
SERROR_NOERROR if there was no error otherwise it will return either SERROR_SEND or SERROR_CONNECT.


Member Data Documentation

char* wkgl::Socket::addr [protected]
 

The address of the remote computer.

SFLAG wkgl::Socket::addr_type [protected]
 

The type of address contained in addr.

char Socket::lasterror = {0} [static, protected]
 

String representaion of the last error.

UINT wkgl::Socket::port [protected]
 

The port to connect to.

UINT Socket::ref_count = 0 [static, protected]
 

Counts all the references to Socket, used to calculate when to do clean-up.

SOCKET wkgl::Socket::sock [protected]
 

The handle to the winsock socket.

BOOL Socket::winsock_init = FALSE [static, protected]
 

Flag specifying weather winsock has been initialized or not.


The documentation for this class was generated from the following files:
Generated on Tue Apr 29 03:18:59 2003 for GuiLib by doxygen1.2.18