⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blocksock.cpp

📁 本源码为移动公司话费查询中间件TUXEDO使用的实例
💻 CPP
字号:
// blocksock.cpp (CSocketException, CKNSocket, CHttpBlockingSocket)

#ifndef _WIN32
	#include <sys/select.h>
#endif

#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif

#include "blocksock.h"

#ifndef WAIT_RECV_TIME
#define WAIT_RECV_TIME 60000
#endif //(#define WAIT_RECV_TIME 60000)
#define QLEN    5
//#include "struct.h"

/**************** Class CSocketException *****************/

/************************Class CKNSocket**************************/

void CKNSocket::Cleanup()
{
	// doesn't throw an exception because it's called in a catch block
	if(m_hSocket <=0)//== 0)
		return;

#ifndef _WIN32
	close(m_hSocket);
//	shutdown(m_hSocket,SHUT_RDWR);
#else
	closesocket(m_hSocket);
#endif //_WIN32

	m_hSocket = 0;
}

void CKNSocket::Create(int nType /* = SOCK_STREAM */)
{
	assert(m_hSocket == 0);
	if((m_hSocket = socket(AF_INET, nType, 0))  <= 0)
	{ //== INVALID_SOCKET)
		#ifdef _DEBUG
				throw new CSocketException("Create");
		#else
				throw KN_SocketError;
		#endif
	}
}

void CKNSocket::Bind(LPCSOCKADDR psa)
{
	assert(m_hSocket != 0);
	int nRet = bind(m_hSocket, psa, sizeof(sockaddr_in));
	if(nRet!=0)
	{
		#ifdef _DEBUG
				throw new CSocketException("Bind");
		#else
				throw KN_SocketBindErr;
		#endif
	}
	return;
}

void CKNSocket::Listen()
{
	assert(m_hSocket != 0);
	int nRet = listen(m_hSocket, QLEN);
	if(nRet!=0)
	{
		//== SOCKET_ERROR) {
		#ifdef _DEBUG
				throw new CSocketException("Listen");
		#else
				throw KN_SocketError;
		#endif
	}
}

int CKNSocket::Accept(CKNSocket& sConnect, LPCSOCKADDR psa)
{
	assert(m_hSocket != 0);
#ifndef _WIN32
	socklen_t nLengthAddr = sizeof(sockaddr_in);
#else
	int nLengthAddr = sizeof(sockaddr_in);
#endif //_WIN32

	sConnect.m_hSocket = accept(m_hSocket, psa, &nLengthAddr);
	if(sConnect  < 0){//== INVALID_SOCKET) {
		// no exception if the listen was canceled
//		if(WSAGetLastError() != WSAEINTR) {
#ifdef _DEBUG
		throw new CSocketException("Accept");
#else
		throw KN_SocketError;
//		}
#endif
		return false;
	}
	return true;
}

void CKNSocket::Close()
{
	if (0 == m_hSocket)
		return;

#ifndef _WIN32
	if(close(m_hSocket) < 0 ){//== SOCKET_ERROR) {
#else
		if(closesocket(m_hSocket) != SOCKET_ERROR){
#endif //_WIN32
#ifdef _DEBUG
		throw new CSocketException("Close");
#else
		throw KN_SocketError;
#endif
	}
	m_hSocket = 0;
}

int CKNSocket::Connect(LPCSOCKADDR psa)
{
	assert(m_hSocket != 0);
	// should timeout by itself
	int nret=0;
	nret=connect(m_hSocket, psa, sizeof(sockaddr_in));
	if(  nret< 0 )
	{
		return 1;
	}
	else
		return 0;
}

int CKNSocket::Write(const char* pch, const int nSize, const int nSecs)
{
	int nBytesSent = 0;
	int nBytesThisTime;
	do {
		nBytesThisTime = Send(pch, nSize - nBytesSent, nSecs);
		nBytesSent += nBytesThisTime;
		pch += nBytesThisTime;
	} while(nBytesSent < nSize);
	return nBytesSent;
}

int CKNSocket::Send(const char* pch, const int nSize, const int nSecs)
{
/*	FILE*  f = NULL;
	f = fopen("d:\\connerr.txt","a+");
	fseek(f,0,SEEK_END);
	fprintf(f, "%s\r\n",pch);
	fflush(f);
	fclose(f);
*/	
//	assert(m_hSocket != 0);
	if(m_hSocket == 0){
#ifdef _DEBUG
		printf("Send m_hSocket == 0 !\n");
		throw new CSocketException("Send");
#else
		printf("Send m_hSocket == 0 !\n");
		throw KN_SocketSendErr;
#endif
	}

	// returned value will be less than nSize if client cancels the reading
	fd_set fd ;//= {1, m_hSocket};
	struct timeval *ptv = NULL;//= {nSecs, 0};

	FD_ZERO(&fd);
	FD_SET(m_hSocket,&fd);

	if(nSecs > 0)
	{
		ptv = new struct timeval;
		ptv->tv_sec = nSecs /1000;
		ptv->tv_usec = nSecs - (ptv->tv_sec)*1000;
	}
	else
		ptv = 0;

	if(select(m_hSocket + 1, 0, &fd, 0, ptv) == 0) {
#ifdef _DEBUG
		printf("Send Error !\n");
		throw new CSocketException("Send");
#else
		printf("Send timeout !\n");
		throw KN_SocketTimeoutErr;
#endif
	}
	if(ptv)
		delete ptv;

	int nBytesSent;
	if((nBytesSent = send(m_hSocket, pch, nSize, 0)) <= 0 ){//SOCKET_ERROR) {
#ifdef _DEBUG
		throw new CSocketException("Send");
#else
		throw KN_SocketSendErr;
#endif
	}
//	printf(pch);
	return nBytesSent;
}

int CKNSocket::Receive(char* pch, const int nSize, const int nSecs)
{
//	assert(m_hSocket != 0);
	if(m_hSocket == 0)
#ifdef _DEBUG
		throw new CSocketException("Receive:m_hSocket == 0");
#else
		throw KN_SocketError;
#endif

	fd_set fd ;//= {1, m_hSocket};
	struct timeval *ptv = NULL;//= {nSecs, 0};

	FD_ZERO(&fd);
	FD_SET(m_hSocket,&fd);

	if(nSecs > 0)
	{
		ptv = new struct timeval;
		ptv->tv_sec = nSecs /1000;
		ptv->tv_usec = nSecs - (ptv->tv_sec)*1000;
	}
	else
		ptv = 0;

	int retcode;
	retcode = select(m_hSocket+1, &fd, 0, 0, ptv);
//	retcode = 10;
	if(ptv)
		delete ptv;
//	if(retcode == 0) 
//		return 0;
//	else 
		if(retcode < 0) {
#ifdef _DEBUG
		throw new CSocketException("Receive: retcode <= 0");
#else
		throw KN_SocketTimeoutErr;
#endif
	}
	else if (retcode == 0) {
#ifdef _DEBUG
		throw new CSocketException("Receive: retcode <= 0");
#else
		throw KN_SocketTimeoutErr;
#endif
	}

	int nBytesReceived;
	if((nBytesReceived = recv(m_hSocket, pch, nSize, 0)) <= 0 ){//== SOCKET_ERROR) {
#ifdef _DEBUG
		throw new CSocketException("Receive:recv");
#else
		throw KN_SocketRecvErr;
#endif
	}

	return nBytesReceived;
}

int CKNSocket::ReceiveDatagram(char* pch, const int nSize, LPCSOCKADDR psa, const int nSecs)
{
//	assert(m_hSocket != 0);
	if(m_hSocket == 0)
#ifdef _DEBUG
		throw new CSocketException("ReceiveDatagram1");
#else
		throw KN_SocketError;
#endif

//	fd_set fd = {1, m_hSocket};
//	struct timeval tv = {nSecs, 0};
//	if(select(0, &fd, 0, 0, &tv) == 0) {
//		throw new CSocketException("Receive timeout");
//	}

	// input buffer should be big enough for the entire datagram
#ifndef _WIN32
	socklen_t nFromSize = sizeof(sockaddr_in);
#else
	int nFromSize = sizeof(sockaddr_in);
#endif _WIN32

	int nBytesReceived = recvfrom(m_hSocket, pch, nSize, 0, psa, &nFromSize);
	if(nBytesReceived <= 0 ){//== SOCKET_ERROR) {
#ifdef _DEBUG
		printf("pch = %d, nSize = %d, nFromSize = %d\n", pch, nSize, nFromSize);
/////////// yyq change 10-10-11
//		return 0;
////////////////////////
		throw new CSocketException("ReceiveDatagram2");
#else
		throw KN_SocketError;
#endif
	}
	return nBytesReceived;
}

int CKNSocket::SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa, const int nSecs)
{
	if(m_hSocket == 0)
#ifdef _DEBUG
		throw new CSocketException("SendDatagram");
#else
		throw KN_SocketError;
#endif

	fd_set fd = {1, m_hSocket};
	struct timeval tv ;//= {nSecs, 0};
	tv.tv_sec = nSecs;
	tv.tv_usec = 0;
	if(select(0, 0, &fd, 0, &tv) == 0) {
#ifdef _DEBUG
		throw new CSocketException("SendDatagram");
#else
		throw KN_SocketError;
#endif
	}

	int nBytesSent = sendto(m_hSocket, pch, nSize, 0, psa, sizeof(sockaddr_in));
	if(nBytesSent <= 0 ){//SOCKET_ERROR) {
#ifdef _DEBUG
		throw new CSocketException("SendDatagram");
#else
		throw KN_SocketError;
#endif
	}
	return nBytesSent;
}

void CKNSocket::GetPeerAddr(LPCSOCKADDR psa)
{
	assert(m_hSocket != 0);
	// gets the address of the socket at the other end
#ifndef _WIN32
	socklen_t nLengthAddr = sizeof(sockaddr_in);
#else
	int nLengthAddr = sizeof(sockaddr_in);
#endif _WIN32

	if(getpeername(m_hSocket, psa, &nLengthAddr) < 0 ){//== SOCKET_ERROR) {
#ifdef _DEBUG
		throw new CSocketException("GetPeerAddr");
#else
		throw KN_SocketError;
#endif
	}
}

void CKNSocket::GetSockAddr(LPCSOCKADDR psa)
{
	assert(m_hSocket != 0);
	// gets the address of the socket at this end
#ifndef _WIN32
	socklen_t nLengthAddr = sizeof(sockaddr_in);
#else
	int nLengthAddr = sizeof(sockaddr_in);
#endif //_WIN32

	if(getsockname(m_hSocket, psa, &nLengthAddr) < 0 ){//== SOCKET_ERROR) {
#ifdef _DEBUG
		throw new CSocketException("GetSockAddr");
#else
		throw KN_SocketError;
#endif
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -