/*******************************************
* winSocket.cpp - Implementation of *
* winSocket Winsock API wrapper class *
* Ryan Lederman ryan@winprog.org *
* January 2002 *
* THIS CODE IS FREE *
* *
*******************************************/
#include "winSocket.h"
winSocket::winSocket() : m_hSocket(0),
m_wVersion(0),
m_ErrorNumber(0)
{
WSADATA wsaD; // Structure initialsed by WSAStartup
m_wVersion = MAKEWORD(2,2); // Version number requested
memset( m_LastError, 0, ERR_MAXLENGTH ); // Nullify last error
memset( &m_sockaddr, 0, sizeof( m_sockaddr ) ); // Nullify structures
memset( &m_rsockaddr, 0, sizeof( m_rsockaddr ) );
int result = WSAStartup( m_wVersion, &wsaD ); // Initialize Winsock
if( result != 0 ) // WSAStartup failed
{
set_LastError( "WSAStartup failed!", WSAGetLastError() );
return;
}
}
winSocket::~winSocket() { WSACleanup(); /* Un-initialise Winsock*/ }
int winSocket::Create( void )
{
/**************************************
* FUNCTION: Create *
* *
* PURPOSE: Initializes the underlying *
* SOCKET object for the class. *
* *
* RETURNS: ERR_WSAERROR upon error, *
* otherwise ERR_SUCCESS *
* *
***************************************/
if ( (m_hSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET )
{
set_LastError( "socket() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ERR_SUCCESS;
}
int winSocket::Close( void )
{
/****************************************
* FUNCTION: Close *
* *
* PURPOSE: Closes the underlying *
* SOCKET object. Does not destroy class *
* *
* RETURNS: ERR_WSAERROR upon error, *
* otherwise ERR_SUCCESS *
* *
*****************************************/
if ( closesocket( m_hSocket ) == SOCKET_ERROR )
{
set_LastError( "closesocket() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
memset( &m_sockaddr, 0, sizeof( sockaddr_in ) );
memset( &m_rsockaddr, 0, sizeof( sockaddr_in ) );
return ERR_SUCCESS;
}
int winSocket::Connect( char* strRemote, unsigned int iPort )
{
/*****************************************
* FUNCTION: Connect *
* *
* PURPOSE: Establishes TCP/IP connection *
* with remote host (strRemote) on port *
* # (iPort) *
* *
* RETURNS: ERR_BADPARAM for invalid *
* parameters, ERR_WSAERROR upon error, *
* otherwise ERR_SUCCESS *
* *
******************************************/
if( strlen( strRemote ) == 0 || iPort == 0 )
return ERR_BADPARAM;
hostent *hostEnt = NULL;
long lIPAddress = 0;
hostEnt = gethostbyname( strRemote );
if( hostEnt != NULL )
{
lIPAddress = ((in_addr*)hostEnt->h_addr)->s_addr;
m_sockaddr.sin_addr.s_addr = lIPAddress;
}
else
{
m_sockaddr.sin_addr.s_addr = inet_addr( strRemote );
}
m_sockaddr.sin_family = AF_INET;
m_sockaddr.sin_port = htons( iPort );
if( connect( m_hSocket, (SOCKADDR*)&m_sockaddr, sizeof( m_sockaddr ) ) == SOCKET_ERROR )
{
set_LastError( "connect() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ERR_SUCCESS;
}
int winSocket::Listen( int iQueuedConnections )
{
/*****************************************
* FUNCTION: Listen *
* *
* PURPOSE: Places the SOCKET in the *
* listening state. Requires that Bind() *
* be called previously. *
* *
* RETURNS: ERR_BADPARAM for invalid *
* parameters, ERR_WSAERROR upon error, *
* otherwise ERR_SUCCESS *
* *
******************************************/
if( iQueuedConnections == 0 )
return ERR_BADPARAM;
if( listen( m_hSocket, iQueuedConnections ) == SOCKET_ERROR )
{
set_LastError( "listen() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ERR_SUCCESS;
}
int winSocket::Send( SOCKET s, char* strData, int iLen )
{
/**********************************************
* FUNCTION: Send *
* *
* PURPOSE: Sends data (strData) to remote *
* side on socket s. *
* *
* RETURNS: ERR_BADPARAM for invalid *
* parameters, ERR_WSAERROR upon error, *
* otherwise ERR_SUCCESS *
* *
***********************************************/
if( strData == NULL || iLen == 0 )
return ERR_BADPARAM;
if( send( s, strData, iLen, 0 ) == SOCKET_ERROR )
{
set_LastError( "send() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ERR_SUCCESS;
}
int winSocket::Send( char* strData, int iLen )
{
/**********************************************
* FUNCTION: Send *
* *
* PURPOSE: Sends data (strData) to remote *
* side on an established TCP/IP connection. *
* Requires that Connect be called previously. *
* *
* RETURNS: ERR_BADPARAM for invalid *
* parameters, ERR_WSAERROR upon error, *
* otherwise ERR_SUCCESS *
* *
***********************************************/
if( strData == NULL || iLen == 0 )
return ERR_BADPARAM;
if( send( m_hSocket, strData, iLen, 0 ) == SOCKET_ERROR )
{
set_LastError( "send() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ERR_SUCCESS;
}
int winSocket::Receive( SOCKET s, char* strData, int iLen )
{
/***********************************************
* FUNCTION: Receive *
* *
* PURPOSE: Retreives data from incoming queue *
* and copies to (strData). (iLen) will contain *
* the length of data read in bytes *
* *
* RETURNS: ERR_BADPARAM for invalid *
* parameters, ERR_WSAERROR upon error, *
* otherwise passes what recv() returns. *
* *
***********************************************/
if( strData == NULL )
return ERR_BADPARAM;
int len = 0;
int ret = 0;
ret = recv( s, strData, iLen, 0 );
if ( ret == SOCKET_ERROR )
{
set_LastError( "recv() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ret;
}
int winSocket::Receive( char* strData, int iLen )
{
/***********************************************
* FUNCTION: Receive *
* *
* PURPOSE: Retreives data from incoming queue *
* and copies to (strData). (iLen) will contain *
* the length of data read in bytes *
* *
* RETURNS: ERR_BADPARAM for invalid *
* parameters, ERR_WSAERROR upon error, *
* otherwise passes wh