www.pudn.com > ZJMailer > ZJSocket.cpp


// ZJSocket.cpp: implementation of the CZJSocket class. 
// 
////////////////////////////////////////////////////////////////////// 
// CZJSocket class Implementation files 
// 
// Author: ÕÅ ½Ü (codez) 
// 
// Finished At Oct 2nd, 2002. 
// 
//============================================================== 
// note: 
//   The CZJSocket class is made by codez, 
// you can modify and/or distribute this file,  
// but do not remove this header please. 
// 
// This file is provided "as is" with no expressed or implied 
// warranty. 
//============================================================== 
//  
// If there is any bugs, please let me know. 
// Thanks! 
// 
// 
// Contact me: 
// 
// Email: jay_zephyr2002@yahoo.com.cn 
// Home Page: http://www.xlrj.com/codez 
//============================================================== 
 
#include "stdafx.h" 
#include "ZJSocket.h" 
 
#include  // T2A Macro 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CZJSocket::CZJSocket() 
{ 
	m_socket = INVALID_SOCKET; 
	m_bConnected = false; 
} 
 
CZJSocket::~CZJSocket() 
{ 
	Close(); 
} 
 
bool CZJSocket::Create() 
{ 
	Close(); 
 
	m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
 
	return IsValidSocket(); 
} 
 
void CZJSocket::Close() 
{ 
	if (IsValidSocket()) 
	{ 
		closesocket(m_socket); 
		m_socket = INVALID_SOCKET; 
		m_bConnected = false; 
	} 
} 
 
bool CZJSocket::IsConnected() const 
{ 
	return m_bConnected; 
} 
 
bool CZJSocket::Connect(LPTSTR szHost, int nPort) 
{ 
	USES_CONVERSION; 
 
	ASSERT(szHost!=NULL); 
 
	SOCKADDR_IN server; 
	memset(&server, 0, sizeof(SOCKADDR_IN)); 
 
	LPSTR lpAddr = T2A(szHost); 
 
	if (IsValidSocket() && (m_bConnected==false)) 
	{ 
		server.sin_family = AF_INET; 
		server.sin_port = htons((u_short)nPort); 
		server.sin_addr.s_addr = inet_addr(lpAddr); 
 
		if (server.sin_addr.s_addr==INADDR_NONE) 
		{ 
			LPHOSTENT lphost; 
			lphost = gethostbyname(lpAddr); 
			if (lphost==NULL) 
			{ 
				Close(); 
				return false; 
			} 
 
			server.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; 
		} 
 
		if (connect(m_socket, (SOCKADDR *)&server, 
			sizeof(server))==SOCKET_ERROR) 
		{ 
			Close(); 
			return false; 
		} 
 
		m_bConnected = true; 
 
		return true; 
	} 
 
	return false; 
} 
 
bool CZJSocket::IsValidSocket() 
{ 
	return (m_socket==INVALID_SOCKET)?false:true; 
} 
 
bool CZJSocket::Send(LPTSTR szMsg, int len) 
{ 
	USES_CONVERSION; 
 
	if (IsValidSocket() && IsConnected()) 
	{ 
		return (send(m_socket, T2A(szMsg), len, 0)!=SOCKET_ERROR) ? true:false; 
	} 
 
	return false; 
} 
 
bool CZJSocket::Receive(LPTSTR szBuffer, int max_len) 
{ 
	USES_CONVERSION; 
 
	if (IsValidSocket() && IsConnected()) 
	{ 
		return (recv(m_socket, T2A(szBuffer), max_len, 0)!=SOCKET_ERROR) ? true:false; 
	} 
 
	return false; 
} 
 
bool CZJSocket::IsReady(bool &ready) 
{ 
	timeval timeout = {0,0}; 
	fd_set fd; 
 
	FD_ZERO(&fd); 
	FD_SET(m_socket, &fd); 
 
	int ret = select(0, &fd, NULL, NULL, &timeout); 
	if (ret == SOCKET_ERROR) 
		return false; 
	else 
		return (ready = ret ? true:false), true; 
} 
 
int CZJSocket::GetRetCode(TCHAR * szBuffer) const 
{ 
	USES_CONVERSION; 
 
	LPSTR szBuf = T2A(szBuffer); 
 
	if (lstrlen(szBuffer)<3) 
		return -1; 
 
	return ((szBuf[0]-0x30)*100 + 
			(szBuf[1]-0x30)*10 + 
			(szBuf[2]-0x30)); 
} 
 
bool CZJSocket::SendCRLF() 
{ 
	return Send("\r\n", 2); 
} 
 
int CZJSocket::SendRecv(LPTSTR szMsg, int len) 
{ 
	TCHAR szBuffer[1024]; 
 
	if (Send(szMsg, len)==false) 
		return -1; 
 
	if (Receive(szBuffer, 1024)==false) 
		return -1; 
 
	return GetRetCode(szBuffer); 
}