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

📄 cngp_api.cpp

📁 小灵通(网通)短信接口程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "StdAfx.h"

#include <io.h>

#include "smei.h"
#include "MD5CheckSum.h"

static SOCKET g_sktDeliver = INVALID_SOCKET;

static BOOL g_bLogin = FALSE;
/*
static char	g_szServerIP[16]="202.108.250.176";	// CNGP服务器地址
static int	g_nServerPort	= 9890;		// CNGP端口(单位:ms)
static int	g_nTimeOut		= 5000;		// CNGP超时时间(单位:ms)
static char	g_szUser[11]	= "Bjzhx";	// CNGP登录名
static char	g_szPwd[33]		= "1234";	// CNGP登录密码
*/

int GenerateSequenceId()
{
	static int nSeq = 1;
	char sSequenceId[11];

	sprintf(sSequenceId, "%s%02d", CTime::GetCurrentTime().Format("%m%d%H%M"), nSeq++);
	if (nSeq >= 100) nSeq = 1;
	return atol(sSequenceId);
}

int _recvwait(SOCKET s, char* lpBuf, int cbBuf, int nTimeOut)
{
	int nResult, nRecvBytes;

	for (nRecvBytes = 0; nRecvBytes < cbBuf; nRecvBytes += nResult) {
		fd_set  readfds;
		timeval tv = {nTimeOut, 0};
		FD_ZERO(&readfds);
		FD_SET(s, &readfds);
		nResult = select(0, &readfds, NULL, NULL, &tv);
		if (nResult == SOCKET_ERROR){
			printf("\nselect() failed, error code: %d\n", WSAGetLastError());
			return SP_ERROR_SOCKET_SELECTFAILED;
		}
		if (nResult == 0) return SP_ERROR_SOCKET_RECVTIMEOUT;
		nResult = recv(s, lpBuf + nRecvBytes, cbBuf - nRecvBytes, 0);
		if (nResult == SOCKET_ERROR){
			printf("\nrecv() failed, error code: %d\n", WSAGetLastError());
			return SP_ERROR_SOCKET_RECVERROR;
		}
		if (nResult == 0) return SP_ERROR_SOCKET_RECVERROR; // 多有可能是对方挂断了连接
	}
	
	return SP_ERROR_OK;
}

int _sendcmd(SOCKET s, unsigned int nTotalLength, unsigned int nCommandId, unsigned int nCommandStatus, unsigned int nSequenceId, void * lpvData, int lenData)
{
	int r;
	CNGP_HEAD head;

	head.nTotalLength = htonl(nTotalLength);
	head.nCommandId  = htonl(nCommandId);
	head.nCommandStatus  = htonl(nCommandStatus);
	head.nSequenceId  = htonl(nSequenceId);

	r = send(s, (char*)&head, sizeof(head), 0);
	if (r < 0) return SP_ERROR_SOCKET_SENDERROR;
	if (r != sizeof(head)) return SP_ERROR_SOCKET_SENDTIMEOUT;

	if (lpvData != NULL && lenData > 0){
		_ASSERT (lenData == (int)nTotalLength - (int)sizeof(head));
		r = send(s, (char*)lpvData, lenData, 0);
		if (r < 0) return SP_ERROR_SOCKET_SENDERROR;
		if (r != lenData) return SP_ERROR_SOCKET_SENDTIMEOUT;
	}

	return SP_ERROR_OK;
}

int _recvresult(SOCKET s, CNGP_HEAD* phead, LPVOID lpBody, int cbBody)
{
	int r, nRest;

	_ASSERT(phead != NULL);

	r = _recvwait(s, (char*)phead, sizeof(CNGP_HEAD), CNGP_RECV_TIMEOUT);
	if (r != SP_ERROR_OK){
		printf("\nCNGP head cannot be received Successfully due to failure of _recvresult(). It returns a code of %d.\n", r);
		return r;
	}

	phead->nTotalLength = ntohl(phead->nTotalLength);
	phead->nCommandId	= ntohl(phead->nCommandId);
	phead->nCommandStatus = ntohl(phead->nCommandStatus);
	phead->nSequenceId	= ntohl(phead->nSequenceId);

	nRest = phead->nTotalLength - sizeof(CNGP_HEAD);
	if (nRest > cbBody) return SP_ERROR_OVERFLOW;

	if (lpBody != NULL && cbBody > 0) {
		memset(lpBody, 0, cbBody);
		if (nRest > 0){
			r = _recvwait(s, (char*)lpBody, nRest, CNGP_RECV_TIMEOUT);
			if (r != SP_ERROR_OK){
				printf("\nCNGP body cannot be received Successfully due to failure of _recvresult(). It returns a code of %d.\n", r);
				return r;
			}
		}
	}

	return SP_ERROR_OK;
}

int _recvresult(SOCKET s, int nCommandId, LPVOID lpRecvBuf, int cbRecvBuf, int *pCommandStatus)
{
	int r;
	CNGP_HEAD head;

	r = _recvresult(s, &head, lpRecvBuf, cbRecvBuf);
	if (r == SP_ERROR_OK){
		if (head.nCommandId != (unsigned int)nCommandId) return SP_ERROR_INVALID_COMMAND;
		if (pCommandStatus != NULL) * pCommandStatus = head.nCommandStatus;
	}

	return r;
}

int _activetest(SOCKET hSocket)
{
	int nErrorCode = ERROR_SUCCESS;

	_ASSERT (hSocket != INVALID_SOCKET);

	// 发送ACTIVE_TEST给服务器
	if (_sendcmd(hSocket, sizeof(CNGP_HEAD), CNGP_ACTIVE_TEST, 0, GenerateSequenceId(), NULL, 0) != SP_ERROR_OK){
		// 发送数据失败,多因为网络质量不好
		nErrorCode = WSAGetLastError();
		printf("\n_activetest failed due to failure of _sendcmd(), error code: %d\n", nErrorCode);
	} else {
		int nCmdStatus;
		if (_recvresult(hSocket, CNGP_ACTIVE_TEST_RESP, NULL, 0, &nCmdStatus) != SP_ERROR_OK){
			// 接受数据失败,可能原因:网络质量不好或鉴权失败,或数据不对
			nErrorCode = WSAGetLastError();
			printf("\n_activetest failed due to failure of _recvresult(), error code: %d\n", nErrorCode);
			return nErrorCode;
		}
		if (nCmdStatus != 0) {
			nErrorCode = ERROR_NETWORK_BUSY;
			printf("\n_activetest failed due to a CNGP command status: %d\n", nCmdStatus);
			return nErrorCode;
		}
	}
	return nErrorCode;
}

void _logout(SOCKET& hSocket)
{
	if (hSocket != INVALID_SOCKET){
		if (_sendcmd(hSocket, sizeof(CNGP_HEAD), CNGP_EXIT, 0, GenerateSequenceId(), NULL, 0) != SP_ERROR_OK){
			// 发送数据失败,多因为网络质量不好
			printf("\n_Logout failed due to failure of _sendcmd(), error code: %d\n", WSAGetLastError());
		} else {
			int nCmdStatus;
			if (_recvresult(hSocket, CNGP_EXIT_RESP, NULL, 0, &nCmdStatus) != SP_ERROR_OK){
				// 接受数据失败,可能原因:网络质量不好或鉴权失败,或数据不对
				printf("\n_Logout failed due to failure of _recvresult(), error code: %d\n", WSAGetLastError());
			}
			if (nCmdStatus != 0){
				printf("\n收到EXIT_RESP...状态:%d", nCmdStatus);
			}
		}
		// 不管结果,直接关闭SOCKET
		closesocket(hSocket);
		hSocket = INVALID_SOCKET;
	}
	return;
}

SOCKET _login(LPCSTR Server, WORD Port, LPCTSTR SystemID,  LPCTSTR Password, int nLoginMode)
{
	SOCKET hSocket;
	sockaddr_in addr;
	int r, nCmdStatus;

	hSocket = socket(AF_INET, SOCK_STREAM, 0);
	if (hSocket == INVALID_SOCKET) {
		printf("\nsocket() failed, error code: %d\n", WSAGetLastError());
		return INVALID_SOCKET;
	}

	try {
		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = htons(Port);
		addr.sin_addr.S_un.S_addr = inet_addr(Server);
		if (connect(hSocket, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) throw (int)WSAGetLastError();

		CNGP_LOGIN_BODY login;
		CNGP_LOGIN_RESP_BODY loginresp;
		unsigned char szAuthClient[16];
		CString sTimeStamp;

		memset(&login, 0, sizeof(login));
		memset(&loginresp, 0, sizeof(loginresp));
		
		sTimeStamp = CTime::GetCurrentTime().Format("%m%d%H%M%S");

		unsigned char lpszOrgin[10 + 7 + 15 + 10 + 1], *p;
		p = lpszOrgin;
		memcpy(p, SystemID, strlen(SystemID)); 
		p += strlen(SystemID);
		memset(p, '\0', 7);
		p += 7;
		memcpy(p, Password, strlen(Password));
		p += strlen(Password);
		memcpy(p, sTimeStamp, 10);
		p += 10;
		_ASSERT( p - lpszOrgin == (int)(strlen(SystemID) + 7 + strlen(Password) + 10) );
		CMD5Checksum::GetMD5((BYTE*)(const char*)lpszOrgin,  strlen(SystemID) + 7 + strlen(Password) + 10, szAuthClient, 16);

		memcpy(login.sClientID, SystemID, sizeof(login.sClientID));
		memcpy(login.sAuthenticatorClient, szAuthClient, sizeof(login.sAuthenticatorClient));
		login.cLoginMode = nLoginMode;
		login.nTimeStamp = htonl(atoi(sTimeStamp));
		login.cVersion = '\x10';
		r = _sendcmd(hSocket, sizeof(CNGP_HEAD)+sizeof(login), CNGP_LOGIN, 0, GenerateSequenceId(), &login, sizeof(login));
		if (r != SP_ERROR_OK) throw (int)WSAGetLastError();

		r = _recvresult(hSocket, CNGP_LOGIN_RESP, &loginresp, sizeof(loginresp), &nCmdStatus);
		if (r != SP_ERROR_OK) throw (int)WSAGetLastError();

		if (nCmdStatus == 0) return hSocket;

		printf("\n登录失败,状态码:%d\n", nCmdStatus);
	}
	catch(int e){
		closesocket(hSocket);
		printf("\nLogin failed, error code: %d\n", e);
	}

	return INVALID_SOCKET;
}

int _delivery(SOCKET& hSocket, int nWaitSeconds)
{
	int nErrorCode = ERROR_SUCCESS; // 0
	int r, nCmdStatus;
	fd_set readfds;
	FILE *fp;
	char szFileName[_MAX_PATH], buf[1024];
	CNGP_HEAD head;
	CNGP_DELIVER_RESP_BODY deliver_resp;

	_ASSERT(hSocket != INVALID_SOCKET);

	FD_ZERO(&readfds);
	FD_SET(hSocket, &readfds);
	timeval tv = {nWaitSeconds,  0};
	r = select(0, &readfds, NULL, NULL, &tv);
	if (r == SOCKET_ERROR){
		nErrorCode = WSAGetLastError();
		printf("\nselect() failed, Error code: %d!\n", nErrorCode);

⌨️ 快捷键说明

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