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

📄 cptestdlg.cpp

📁 短信网关源代码
💻 CPP
字号:
// CpTestDlg.cpp : implementation file
//

#include "StdAfx.h"
#include "CpTest.h"
#include "CpTestDlg.h"
#include "smgapi.h"
#include "Winsock2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCpTestDlg dialog

CCpTestDlg::CCpTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCpTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCpTestDlg)
	m_iGwPort = 0;
	m_sSubmitMsg = _T("");
	m_sDeliverMsg = _T("");
	m_DstTelphone = _T("");
	m_iSmsSn = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_sock = 0;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCpTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCpTestDlg)
	DDX_Control(pDX, IDC_IPADDRESS1, m_ctlGwHost);
	DDX_Text(pDX, IDC_EDIT1, m_iGwPort);
	DDV_MinMaxInt(pDX, m_iGwPort, 0, 65535);
	DDX_Text(pDX, IDC_EDIT2, m_sSubmitMsg);
	DDX_Text(pDX, IDC_EDIT3, m_sDeliverMsg);
	DDX_Text(pDX, IDC_EDIT4, m_DstTelphone);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCpTestDlg, CDialog)
	//{{AFX_MSG_MAP(CCpTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, OnSubmit)
	ON_BN_CLICKED(IDC_BUTTON1, OnConnectGw)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCpTestDlg message handlers

BOOL CCpTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	WSADATA wsaData;
	WSAStartup( MAKEWORD(2, 2), &wsaData);

	m_ctlGwHost.SetAddress(127,0,0,1);
	m_iGwPort = 8020;
	m_DstTelphone = "13576997709";/*"13687915955"*/
	m_sSubmitMsg = "测试短信网关,短信号0";
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCpTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCpTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCpTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCpTestDlg::OnSubmit() 
{
	UpdateData(TRUE);

	TMsgHead rMsgHead;
	TSmgSubmitReq rSmgSubmitReq;
	
	memset(&rMsgHead,0,sizeof(rMsgHead));
	memset(&rSmgSubmitReq,0,sizeof(rSmgSubmitReq));

	rMsgHead.nPort = m_iGwPort;
	rMsgHead.nLinkId = m_sock;
	rMsgHead.nChannelId = 0;
	rMsgHead.nCmdId = SMG_SubmitReq;
	strcpy(rMsgHead.sLinkIp,m_strHostIp);

	rSmgSubmitReq.ucActionType = 0x02;
	rSmgSubmitReq.nClientSeqId = m_iSmsSn++;
	rSmgSubmitReq.nMsgCounts = 1;
	rSmgSubmitReq.nMsgNumber = 1;
	rSmgSubmitReq.ucRegister = 1;
	strcpy(rSmgSubmitReq.sServiceId,"8");
	rSmgSubmitReq.ucFeeUserType = 2;
	rSmgSubmitReq.sFeeAddr[0] = '\0';
	rSmgSubmitReq.ucMsgFmt = 15;
	strcpy(rSmgSubmitReq.sFeeType,"01");
	strcpy(rSmgSubmitReq.sFeeCode,"0");
	strcpy(rSmgSubmitReq.sSrcAddr,"044968");
	strcpy(rSmgSubmitReq.sDstAddr,m_DstTelphone.GetBuffer(0));
	strcpy(rSmgSubmitReq.sMsgContext,m_sSubmitMsg.GetBuffer(0));
	rSmgSubmitReq.ucMsgLen = strlen(rSmgSubmitReq.sMsgContext);
	
	struct TSmgPacket rPack;
	memcpy(&rPack.rMsgHead,&rMsgHead,sizeof(rMsgHead));
	memcpy(&rPack.rMsgBody,&rSmgSubmitReq,sizeof(rSmgSubmitReq));
	int ret = send(m_sock,(const char *)&rPack,sizeof(rSmgSubmitReq)+sizeof(rMsgHead),MSG_DONTROUTE);
	if(ret <= 0)
		MessageBox("Error,send");
	
}

void CCpTestDlg::OnConnectGw() 
{
	UpdateData(TRUE);

	if(m_sock > 0)
	{
		MessageBox("You had connect,error connect again.");
		return;
	}
	
	struct sockaddr_in dest;
	m_sock = socket(PF_INET,SOCK_STREAM,0);
	if(m_sock < 0)
	{
		int err = WSAGetLastError();
		MessageBox("Error! Can't create socket");
		m_sock = -1;
		return;
	}
	memset(&dest,0, sizeof(dest));
	dest.sin_family = PF_INET;
	dest.sin_port = htons(m_iGwPort);
	DWORD dstAddr;
	m_ctlGwHost.GetAddress(dstAddr);
	dest.sin_addr.S_un.S_addr = htonl(dstAddr);
	strcpy(m_strHostIp,inet_ntoa(dest.sin_addr));

	if(0 != connect(m_sock, (sockaddr *)&dest, sizeof(dest)))
	{
		closesocket(m_sock);
		m_sock = -1;
		MessageBox("Error! Can't connect server");
		return;
	}

	unsigned long flag = 0;
	int ret = ioctlsocket(m_sock,FIONBIO,&flag);
	if(ret != 0)
	{
			closesocket(m_sock);
			m_sock = -1;
			return;
	}


	SetTimer(1,500,0);


	TMsgHead rMsgHead;
	TSmgRegisterReq rSmgRegisterReq;

	memset(&rMsgHead,0,sizeof(rMsgHead));
	memset(&rSmgRegisterReq,0,sizeof(rSmgRegisterReq));

	rMsgHead.nChannelId = 0;
	rMsgHead.nLinkId = m_sock;
	rMsgHead.nCmdId = SMG_RegisterReq;
	rMsgHead.nPort = m_iGwPort;
	strcpy(rMsgHead.sLinkIp,m_strHostIp);
	
	strcpy(rSmgRegisterReq.sServiceNumber,"8");
	
	struct TSmgPacket rPack;
	memcpy(&rPack.rMsgHead,&rMsgHead,sizeof(rMsgHead));
	memcpy(&rPack.rMsgBody,&rSmgRegisterReq,sizeof(rSmgRegisterReq));
	int rett = send(m_sock,(const char *)&rPack,sizeof(rSmgRegisterReq)+sizeof(rMsgHead),MSG_DONTROUTE);
	if(rett <= 0)
		MessageBox("Error,send");
	
}

void CCpTestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	struct timeval tout = {0,0};

	switch (nIDEvent)
	{
	case 1:
		fd_set readset;
		FD_ZERO(&readset);
		FD_SET(m_sock,&readset);
		if(select(m_sock+1,&readset,NULL,NULL,&tout) > 0)
		{
			if(FD_ISSET(m_sock,&readset))
			{
				int nLen = recv(m_sock,(char *)&m_Buf,sizeof(m_Buf),0);
				ProcessSMPacket(&m_Buf,nLen);
			}
		}
		break;
	default:
		break;
	}
	CDialog::OnTimer(nIDEvent);
}

void CCpTestDlg::ProcessSMPacket(TSmgPacket *psmgPack,int nLen)
{
	switch(psmgPack->rMsgHead.nCmdId)
	{
	case	SMG_DeliverReq:
		m_sDeliverMsg = psmgPack->rMsgBody.rSmgDeliverReq.usMsgContent;
		UpdateData(FALSE);
		break;
	}
}

⌨️ 快捷键说明

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