SocketClient.cpp
上传用户:oadesign
上传日期:2013-12-25
资源大小:265k
文件大小:6k
- // SocketClient.cpp : implementation file
- //
- #include "stdafx.h"
- #include "NetDownMTR.h"
- #include "SocketClient.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #pragma comment(lib, "wsock32.lib")
- /////////////////////////////////////////////////////////////////////////////
- // CSocketClient
- CSocketClient::CSocketClient ()
- : m_hEvtEndModule ( NULL )
- , m_bConnected ( FALSE )
- {
- }
- CSocketClient::~CSocketClient()
- {
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- #if 0
- BEGIN_MESSAGE_MAP(CSocketClient, CSocket)
- //{{AFX_MSG_MAP(CSocketClient)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- #endif // 0
- /////////////////////////////////////////////////////////////////////////////
- // CSocketClient member functions
- BOOL CSocketClient::Connect(LPCTSTR lpszHost, USHORT nPort)
- {
- if ( !lpszHost || strlen(lpszHost) <= 0 || nPort < 1 )
- return FALSE;
- Close ();
- if ( !Create () ) return FALSE;
- m_bConnected = CSocket::Connect ( lpszHost, nPort );
- if ( !m_bConnected )
- Log ( L_WARNING, "Connect to [%s:%d] failed", lpszHost, nPort );
- return m_bConnected;
- }
- CString CSocketClient::GetDigitStrAtHead ( LPCTSTR lpszStr )
- {
- if ( !lpszStr ) return "";
- CString csStr = lpszStr;
- csStr.TrimLeft(); csStr.TrimRight();
- CString csDigitStr;
- for ( int i=0; isdigit ( (int)csStr[i] ); i++ )
- {
- csDigitStr += csStr[i];
- }
- return csDigitStr;
- }
- //
- // return : ------------------------------------------
- // > 0 - 回应代码
- // = 0 - 未读到数据
- // = -1 - 网络出错
- //
- int CSocketClient::GetResponse ( CString *pcsResponseStr/*=NULL*/, BOOL bBlock/*=TRUE*/ )
- {
- if ( pcsResponseStr ) *pcsResponseStr = "";
- ASSERT ( m_hSocket != INVALID_SOCKET );
- CString csOneLine = GetOneLine ( m_csResponseHistoryString );
- if ( csOneLine.IsEmpty () )
- {
- char szRecvBuf[NET_BUFFER_SIZE] = {0};
- int nRet = Receive ( szRecvBuf, sizeof(szRecvBuf), bBlock );
- if ( nRet <= 0 )
- {
- if ( nRet < 0 ) Log ( L_WARNING, "Receive response data failed" );
- return nRet;
- }
- m_csResponseHistoryString += szRecvBuf;
- csOneLine = GetOneLine ( m_csResponseHistoryString );
- if ( csOneLine.IsEmpty () ) return -1;
- }
- if ( pcsResponseStr ) *pcsResponseStr = csOneLine;
- CString csRetCode = GetDigitStrAtHead ( csOneLine );
- // TRACE ( "收到服务器回应:%sn", csOneLine );
- return atoi ( csRetCode );
- }
- BOOL CSocketClient::GetResponse ( int nVerifyCode, CString *pcsResponseStr/*=NULL*/ )
- {
- CString csResponseStr;
- int nResponseCode = GetResponse ( &csResponseStr );
- if ( pcsResponseStr ) *pcsResponseStr = csResponseStr;
- if ( nResponseCode != nVerifyCode )
- {
- CString csMsg;
- csMsg.Format ( "Receive error response code : %s", csResponseStr );
- return FALSE;
- }
- return TRUE;
- }
- BOOL CSocketClient::SendString(LPCTSTR lpszData, ...)
- {
- // 格式化
- char szDataBuf[NET_BUFFER_SIZE] = {0};
- va_list va;
- va_start (va, lpszData);
- _vsnprintf ( szDataBuf, sizeof(szDataBuf)-1, (const char*)lpszData, va);
- va_end(va);
- TRACE ( "n发送字符串:------------>>>n%snn", szDataBuf );
- return Send ( szDataBuf, strlen(szDataBuf) );
- }
- BOOL CSocketClient::Send(char *data, int size)
- {
- ASSERT ( m_hEvtEndModule && m_hEvtEndModule != INVALID_HANDLE_VALUE );
- ASSERT ( m_hSocket != INVALID_SOCKET );
- if ( !data || size < 1 ) return TRUE;
- int nRemainBytes = size;
- int nSentTotalBytes = 0;
- int nSendFailedCount = 0;
- while ( nRemainBytes > 0 )
- {
- int nSentBytes = CSocket::Send ( data+nSentTotalBytes, nRemainBytes );
- if ( nSentBytes < 0 )
- {
- nSendFailedCount ++;
- if ( nSendFailedCount > 10 )
- {
- Log ( L_WARNING, "Send net data block failed" );
- m_bConnected = FALSE;
- return FALSE;
- }
- else
- {
- SLEEP_RETURN ( 100 );
- }
- }
- else
- {
- nRemainBytes -= nSentBytes;
- nSentTotalBytes += nSentBytes;
- nSendFailedCount = 0;
- }
- }
- return TRUE;
- }
- void CSocketClient::SetEventOfEndModule(HANDLE hEvtEndModule)
- {
- m_hEvtEndModule = hEvtEndModule;
- ASSERT ( m_hEvtEndModule && m_hEvtEndModule != INVALID_HANDLE_VALUE );
- }
- // 从类似 "(192,168,0,2,4,31)" 字符串中得到IP地址和端口号
- //
- BOOL CSocketClient::GetIPAndPortByPasvString(LPCTSTR lpszPasvString, OUT CString &csIP, OUT USHORT &nPort)
- {
- if ( !lpszPasvString ) return FALSE;
- char *p = strchr ( lpszPasvString, '(' );
- if ( !p ) return FALSE;
- CString csPasvStr = p+1, csTemp;
- int nPosStart = 0, nPosEnd = 0;
- int nMultiple = 0, nMantissa = 0;
- for ( int i=0; ; i++ )
- {
- nPosEnd = csPasvStr.Find ( ",", nPosStart );
- if ( nPosEnd < 0 )
- {
- if ( i == 5 )
- {
- nPosEnd = csPasvStr.Find ( ")", nPosStart );
- csTemp = csPasvStr.Mid ( nPosStart, nPosEnd-nPosStart );
- nMantissa = atoi ( csTemp );
- break;
- }
- else return FALSE;
- }
- csTemp = csPasvStr.Mid ( nPosStart, nPosEnd-nPosStart );
- csTemp.TrimLeft(); csTemp.TrimRight();
- if ( i < 4 )
- {
- if ( !csIP.IsEmpty () ) csIP += ".";
- csIP += csTemp;
- }
- else if ( i == 4 )
- {
- nMultiple = atoi ( csTemp );
- }
- else return FALSE;
- nPosStart = nPosEnd + 1;
- }
- nPort = nMultiple*256 + nMantissa;
- return TRUE;
- }
- //
- // return : -----------------------------------------------------------
- // >= 0 - 收到的字节数
- // -1 - 失败
- //
- int CSocketClient::Receive(char *szBuf, int size, BOOL bBlock/*=TRUE*/)
- {
- if ( !szBuf || size < 0 ) return -1;
- int nReadSize = 0;
- if ( bBlock )
- {
- nReadSize = CSocket::Receive ( szBuf, size );
- if ( nReadSize <= 0 ) nReadSize = -1;
- }
- else
- {
- nReadSize = CAsyncSocket::Receive ( szBuf, size );
- if ( nReadSize < 0 )
- {
- if ( WSAEWOULDBLOCK == GetLastError () )
- nReadSize = 0;
- else
- nReadSize = -1;
- }
- }
- if ( nReadSize == -1 )
- {
- m_bConnected = FALSE;
- }
- return nReadSize;
- }
- void CSocketClient::Disconnect()
- {
- // ShutDown ( both );
- Close ();
- m_bConnected = FALSE;
- }