📄 commclientdlg.cpp
字号:
// CommClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CommClient.h"
#include "CommClientDlg.h"
#include "Network.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CNetwork g_Socket; //socket对象
extern char g_SocketIP[16]; //socket服务器IP
extern int g_SocketPort; //socket服务器端口
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CCommClientDlg dialog
CCommClientDlg::CCommClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommClientDlg)
m_stdno = _T("");
m_userid = _T("");
m_username = _T("");
m_deptcode = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCommClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommClientDlg)
DDX_Text(pDX, IDC_EDIT_STDNO, m_stdno);
DDV_MaxChars(pDX, m_stdno, 10);
DDX_Text(pDX, IDC_EDIT_USERID, m_userid);
DDV_MaxChars(pDX, m_userid, 18);
DDX_Text(pDX, IDC_EDIT_USERNAME, m_username);
DDV_MaxChars(pDX, m_username, 8);
DDX_Text(pDX, IDC_EDIT_DEPARTCODE, m_deptcode);
DDV_MaxChars(pDX, m_deptcode, 2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCommClientDlg, CDialog)
//{{AFX_MSG_MAP(CCommClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCommClientDlg message handlers
BOOL CCommClientDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CCommClientDlg::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 CCommClientDlg::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 CCommClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCommClientDlg::OnButtonTest() //通信测试(同步短连接方式)
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_stdno.IsEmpty())
return;
char sendBuf[1024];
char recBuf[11];
//第1步:连接APPServer
strcpy(g_Socket.m_cIP,g_SocketIP);
g_Socket.m_usPort=g_SocketPort;
g_Socket.InitialSocket();
int i;
for(i=0;i<2;i++)
{
if(g_Socket.ConnectServer())
{
break;
}
Sleep(50);
}
if(i>=2)
{
AfxMessageBox("连接应用服务器失败,请检查系统配置! ");
return ;
}
TRACE0("连接应用服务器成功");
//第2步:构造发送数据并发送
SYSTEMTIME stLocal;
CString CurDateStr;
char oldSum[9]="",DesSum[9]="";
::GetLocalTime(&stLocal);
CurDateStr.Format("%u-%02u-%02u", stLocal.wYear, stLocal.wMonth, stLocal.wDay);
sprintf(sendBuf,"%-4s%-4s%-2s%-10s%-8s%-18s%-10s%-8s","64",\
"0001",m_deptcode,m_stdno,m_username,m_userid,CurDateStr,"00000000");
m_CheckSumObj.Get_CheckSum(sendBuf,64,DesSum);
TRACE1("SNEDBUF=%s",sendBuf);
g_Socket.SendString(sendBuf, 64);
g_Socket.GetString(recBuf,10);
recBuf[10]=0;
//得到传过来的原始校验和值
strncpy(oldSum,recBuf+2,8);
oldSum[8]=0;
//计算校验和,检查校验
m_CheckSumObj.Get_CheckSum(recBuf,10,DesSum);
TRACE3("recbuf=%s,oldSum=%s,DesSum=%s",recBuf,oldSum,DesSum);
if(strcmp(oldSum,DesSum)!=0)
{
recBuf[0]='N';
}
switch(recBuf[0])
{
case '0':
AfxMessageBox("业务成功! ");
break;
case '1':
AfxMessageBox("业务失败!数据库错误. ");
break;
case '2':
AfxMessageBox("该学号已经存在. ");
break;
case '3':
AfxMessageBox("发送数据校验错误. ");
break;
case 'N':
AfxMessageBox("返回数据校验错误. ");
break;
}
//第3步:关闭socket,释放socket库。注意此处休眠的使用(Sleep(1);不能少)。
Sleep(1);
g_Socket.CloseSock(); //关闭socket
TRACE0("CLOSED SOCKET");
}
void CCommClientDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -