📄 myserverdlg.cpp
字号:
// MyServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyServer.h"
#include "MyServerDlg.h"
#include "AddUserDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMyServerDlg dialog
CMyServerDlg::CMyServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyServerDlg)
m_iPort = 6789;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyServerDlg)
DDX_Control(pDX, IDC_LIST_User, m_listUser);
DDX_Control(pDX, IDC_CMB_IP, m_cmbIP);
DDX_Text(pDX, IDC_EDIT_Port, m_iPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyServerDlg, CDialog)
//{{AFX_MSG_MAP(CMyServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_Start, OnBTNStart)
ON_BN_CLICKED(IDC_BTN_AddUser, OnBTNAddUser)
ON_BN_CLICKED(IDC_BTN_DelUser, OnBTNDelUser)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyServerDlg message handlers
BOOL CMyServerDlg::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
// 连接数据库
try
{
CString sDSN="DRIVER=Microsoft Access Driver (*.mdb);SRVR='';DBQ=test.mdb;UID=sa;PWD= ";
m_Database.OpenEx(sDSN,CDatabase::noOdbcDialog);
m_Recordset.m_pDatabase=&m_Database;
}
catch(CDBException* pEx)
{
pEx->Delete();
pEx = NULL;
AfxMessageBox(" 系统无法启动,找不到数据库文件!");
}
// 初始化列表控件
int i = 0;
m_listUser.InsertColumn(i++, "账号", LVCFMT_LEFT, 50);
m_listUser.InsertColumn(i++, "密码", LVCFMT_LEFT, 50);
m_listUser.InsertColumn(i++, "姓名", LVCFMT_LEFT, 50);
m_listUser.InsertColumn(i++, "多媒体信息", LVCFMT_LEFT, 300);
m_listUser.SetExtendedStyle( LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
QueryUserList();
// 获取本机IP
char szHostName[MAX_PATH]="\0";
if( gethostname(szHostName, MAX_PATH) == 0 )
{
struct hostent* pHost;
int i = 0;
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
LPCSTR psz = inet_ntoa(*(struct in_addr*)pHost->h_addr_list[i]);
m_cmbIP.AddString(psz);
}
m_cmbIP.SetCurSel(0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyServerDlg::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 CMyServerDlg::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 CMyServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CMyServerDlg::OnAccept()
{
if (m_recvSocket.m_hSocket != INVALID_SOCKET)
{
m_recvSocket.Close();
}
m_recvSocket.m_pServerDlg = this;
BOOL bRet = FALSE;
bRet = m_listenSocket.Accept(m_recvSocket);
CString strIP;
UINT port = 0;
m_recvSocket.GetPeerName(strIP, port);
return bRet;
}
BOOL CMyServerDlg::OnReceive()
{
BYTE bBuffer[MAX_PATH];
memset(bBuffer, 0, MAX_PATH);
BYTE* pBuf = bBuffer;
int iLen = m_recvSocket.Receive(bBuffer, MAX_PATH); //接收数据
CString strUserID = CString((char*)pBuf); //分离出UserID和strUserPwd
pBuf += strUserID.GetLength() + 1;
CString strUserPwd = CString((char*)pBuf);
// 查询数据库
pBuf = bBuffer;
int& iResult = *(int*)pBuf;
pBuf += sizeof(int);
CString strTemp;
CString strSQL;
strSQL.Format("Select * From User Where UserID = '%s' ", strUserID);
BOOL bRet = m_Recordset.Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
if ( bRet && ( !m_Recordset.IsEOF() ) )
{
m_Recordset.GetFieldValue("UserPwd",strTemp);
if (strTemp == strUserPwd)
{
iResult = 2;
m_Recordset.GetFieldValue("UserName", strTemp);
iLen = strTemp.GetLength();
memcpy(pBuf, strTemp.GetBuffer(iLen), iLen);
pBuf += iLen + 1;
m_Recordset.GetFieldValue("FileUrl", strTemp);
iLen = strTemp.GetLength();
memcpy(pBuf, strTemp.GetBuffer(iLen), iLen);
pBuf += iLen + 1;
}
else
{
iResult = 1;
}
}
else
{
iResult = 0;
}
m_Recordset.Close();
m_recvSocket.Send(bBuffer, pBuf - bBuffer);
return TRUE;
}
void CMyServerDlg::OnBTNStart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_listenSocket.m_pServerDlg = this;
BOOL bRet = FALSE;
bRet = m_listenSocket.Create(m_iPort);
if (!bRet)
{
AfxMessageBox("创建网络端口失败。");
return;
}
bRet = m_listenSocket.Listen();
if (!bRet)
{
AfxMessageBox("侦听失败。");
return;
}
GetDlgItem(IDC_BTN_Start)->EnableWindow(FALSE); //设置按纽不可用
QueryUserList();
}
BOOL CMyServerDlg::QueryUserList()
{
// 先删除列表控件中以前的数据
m_listUser.DeleteAllItems();
// 向列表控件中增加数据
CString strTemp;
CString strSQL = "Select * from User order by UserID";
m_Recordset.Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
int i = 0;
while( !m_Recordset.IsEOF() )
{
m_listUser.InsertItem(i, "");
m_Recordset.GetFieldValue("UserID", strTemp);
m_listUser.SetItemText(i, 0, strTemp);
m_Recordset.GetFieldValue("UserPwd", strTemp);
m_listUser.SetItemText(i, 1, strTemp);
m_Recordset.GetFieldValue("UserName", strTemp);
m_listUser.SetItemText(i, 2, strTemp);
m_Recordset.GetFieldValue("FileUrl", strTemp);
m_listUser.SetItemText(i, 3, strTemp);
m_Recordset.MoveNext();
i++;
}
m_Recordset.Close();
return TRUE;
}
void CMyServerDlg::OnBTNAddUser()
{
// TODO: Add your control notification handler code here
CAddUserDlg dlg;
if (dlg.DoModal() != IDOK)
{
return;
}
CString strSQL;
strSQL.Format("Insert Into User Values('%s', '%s', '%s', '%s')",
dlg.m_strUserID, dlg.m_strUserPwd, dlg.m_strUserName, dlg.m_strFileUrl);
try
{
m_Database.ExecuteSQL(strSQL);
}
catch(CDBException* pEx)
{
AfxMessageBox(pEx->m_strError);
pEx->Delete();
pEx = NULL;
}
QueryUserList();
}
void CMyServerDlg::OnBTNDelUser()
{
// TODO: Add your control notification handler code here
POSITION pos = m_listUser.GetFirstSelectedItemPosition();
if (pos == NULL)
{
AfxMessageBox("请先选择要删除的账号。");
return;
}
int iSel = m_listUser.GetNextSelectedItem(pos);
CString strUserID = m_listUser.GetItemText(iSel, 0);
CString strSQL;
strSQL.Format("Delete From User Where UserID = '%s' ", strUserID);
try
{
m_Database.ExecuteSQL(strSQL);
}
catch(CDBException* pEx)
{
AfxMessageBox(pEx->m_strError);
pEx->Delete();
pEx = NULL;
}
QueryUserList();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -