#include "StdAfx.h"
#include "winSocket.h"
#pragma warning(push)
#pragma warning(disable:4996)
namespace Yellow{
namespace net{
/*
constructor
*/
CYSocket::CYSocket()
: m_hSocket(0)
, m_wVersion(0)
, m_ErrorNumber(0)
{
WSADATA wsaD;
m_wVersion = MAKEWORD(2,2);
memset( m_LastError, 0, ERR_MAXLENGTH );
memset( &m_sockaddr, 0, sizeof( m_sockaddr ) );
memset( &m_rsockaddr, 0, sizeof( m_rsockaddr ) );
int result = WSAStartup( m_wVersion, &wsaD );
if( result != 0 )
{
set_LastError( "WSAStartup failed!", WSAGetLastError() );
return;
}
}
CYSocket::~CYSocket()
{
WSACleanup();
}
/******************************************************************************
功能描述:Initializes SOCKET object for the class
输入参数:
输出参数:
返 回 值:ERR_WSAERROR upon error otherwise ERR_SUCCESS
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Create( void )
{
if ( (m_hSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET )
{
set_LastError( "socket() failed", WSAGetLastError() );
return ERR_WSAERROR;
} // if ( (m_hSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET )
set_RecvTimeout( RECEIVETIMEOUT ) ;
set_SendTimeout( SENDTIMEOUT ) ;
return ERR_SUCCESS;
}
/******************************************************************************
功能描述:Closes SOCKET object. Does not destroy class
输入参数:
输出参数:
返 回 值:ERR_WSAERROR upon error otherwise ERR_SUCCESS
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Close( void )
{
if ( closesocket( m_hSocket ) == SOCKET_ERROR ) //close socket
{
set_LastError( "closesocket() failed", WSAGetLastError() );
return ERR_WSAERROR;
} // if ( closesocket( m_hSocket ) == SOCKET_ERROR ) //close socket
memset( &m_sockaddr, 0, sizeof( sockaddr_in ) );
memset( &m_rsockaddr, 0, sizeof( sockaddr_in ) );
m_hSocket = 0 ;
return ERR_SUCCESS;
}
/******************************************************************************
功能描述:establishes TCP/IP connection with remote host (strRemote) on port
输入参数:[in] char* strRemote remote IP
[in] unsigned int iPort remote port
输出参数:
返 回 值:ERR_BADPARAM for invalid parameters, ERR_WSAERROR upon error,
otherwise ERR_SUCCESS
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Connect( const char* strRemote, unsigned int iPort )
{
if( strlen( strRemote ) == 0 || iPort == 0 )
return ERR_BADPARAM;
hostent *hostEnt = NULL;
long lIPAddress = 0;
hostEnt = gethostbyname( strRemote ); //get host name
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;
}
/******************************************************************************
功能描述:Places the SOCKET in the listening state. Requires that Bind() be called previously.
输入参数:[in] iQueuedConnections
输出参数:
返 回 值:ERR_BADPARAM for invalid parameters, ERR_WSAERROR upon error,otherwise ERR_SUCCESS
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Listen( int iQueuedConnections )
{
if( iQueuedConnections == 0 )
return ERR_BADPARAM;
if( listen( m_hSocket, iQueuedConnections ) == SOCKET_ERROR )
{
set_LastError( "listen() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ERR_SUCCESS;
}
/******************************************************************************
功能描述:Sends data (strData) to remote side on socket s.
输入参数: [in] SOCEKT s connected SOCKET
[in] char* strData will be send data
[in] int iLen
输出参数:
返 回 值:ERR_BADPARAM for invalid parameters,
ERR_WSAERROR upon error, otherwise ERR_SUCCESS
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Send( SOCKET s, const char* strData, int iLen )
{
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;
}
/******************************************************************************
功能描述:Sends data (strData) to remote side on an established TCP/IP connection.
Requires that Connect be called previously.
输入参数: [in] char* strData will be send data
[in] int iLen data len
输出参数:
返 回 值: ERR_BADPARAM for invalid parameters,
ERR_WSAERROR upon error,otherwise ERR_SUCCESS
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Send( const char* strData, int iLen )
{
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;
}
/******************************************************************************
功能描述: Receive data from incoming queue and copies to (strData).
输入参数: [in] SOCKET s connected SOCKET handle
[in] char* strData will be receive data
[in] int iLen will contain the length of data read in bytes
输出参数:
返 回 值:ERR_BADPARAM for invalid parameters, ERR_WSAERROR upon error otherwise passes what recv() returns
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Receive( SOCKET s, char* strData, int iLen )
{
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;
}
/******************************************************************************
功能描述:receive data from incoming queue and copies to (strData).
输入参数: [in] char* strData will be receive data
[in] int iLen will contain the length of data read in bytes
输出参数:ERR_WSAERROR upon error otherwise ERR_SUCCESS
返 回 值:ERR_BADPARAM for invalid parameters, ERR_WSAERROR upon error,otherwise passes what recv() returns
其 它:
作 者:宁家宏
日 期:2009-8-4
*******************************************************************************/
int CYSocket::Receive( char* strData, int iLen )
{
if( strData == NULL )
return ERR_BADPARAM;
int len = 0;
int ret = 0;
ret = recv( m_hSocket, strData, iLen, 0 );
if ( ret == SOCKET_ERROR )
{
set_LastError( "recv() failed", WSAGetLastError() );
return ERR_WSAERROR;
}
return ret;
}
/**********************************************************************