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

📄 dbserverdlg.cpp

📁 数据库远程备份 功能:首先
💻 CPP
字号:
// DBServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DBServer.h"
#include "DBServerDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CDBServerDlg dialog

CDBServerDlg::CDBServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDBServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDBServerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDBServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDBServerDlg)
	DDX_Control(pDX, IDOK, m_OK);
	DDX_Control(pDX, IDCANCEL, m_Cancel);
	DDX_Control(pDX, IDC_IPADRESS, m_IPAdress);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDBServerDlg, CDialog)
	//{{AFX_MSG_MAP(CDBServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, On_DBServer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDBServerDlg message handlers

BOOL CDBServerDlg::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
	
	// 初始化阴影位图按钮
    m_OK.SetTextColor( RGB(0,0,0) );
	m_OK.SetToolTipText( "发送XML库文件" );  
	m_OK.SetShade( SHS_HARDBUMP, 10, 20, 5, RGB(55,55,255) );

	m_Cancel.SetTextColor( RGB(0,0,0) );
	m_Cancel.SetToolTipText( "退出" );  
	m_Cancel.SetShade( SHS_HARDBUMP, 10, 20, 5, RGB(55,55,255) );  	

	// 初始化查询本机IP
	QueryLocalIP();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDBServerDlg::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 CDBServerDlg::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 CDBServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//////////////////////////////////////////////////////////////////////////////
//名称:QueryLocalIP
//功能:查询本机IP及主机名
//作者:徐景周(jingzhou_xu@163.com)
//组织:未来工作室(Future Studio)
//日期:2005.05.30
/////////////////////////////////////////////////////////////////////////////
void CDBServerDlg::QueryLocalIP() 
{
	char szHostName[128];
	if( gethostname( szHostName, 128 ) == 0 )
	{
		struct hostent *pHost;
		pHost = gethostbyname( szHostName );
		for( int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; ++i )
		{
			CString strIP	= inet_ntoa( *(struct in_addr*)pHost->h_addr_list[i] );
			m_IPAdress.SetWindowText( strIP );
		}
	}
}

// 发送XML库文件到客户端
void CDBServerDlg::On_DBServer() 
{
	// 选择要发送的XML文件
	CFileDialog dlg( TRUE, "Xml", "*.xml", OFN_HIDEREADONLY, "*.xml||" );
	dlg.m_ofn.lpstrTitle = "选择要发送的XML格式库";
	if( IDCANCEL == dlg.DoModal() )
		return ;

	CString strXmlName	= dlg.GetPathName();

	// 发送文件到指定端口
	if( SendFileToRemoteRecipient( strXmlName ) )
		AfxMessageBox( strXmlName + "XML库文件发送成功!" );
	else
		AfxMessageBox( strXmlName + "XML库文件发送失败!" );
	
}

#define PRE_AGREED_PORT		8686		// 端口号
#define SEND_BUFFER_SIZE	4096		// 缓冲区大小

// 发送文件
BOOL CDBServerDlg::SendFileToRemoteRecipient(CString fName)
{

	AfxSocketInit( NULL );				
	CSocket sockSrvr; 
	sockSrvr.Create( PRE_AGREED_PORT );	// 指定端口创建socket
	sockSrvr.Listen();					// 侦听端口上的客户端
	CSocket sockConnection;
	sockSrvr.Accept( sockConnection );	// 用另一个socket接收连接
	
	// 是否成功
	BOOL bRet = TRUE;				

	int fileLength, cbLeftToSend;		// 文件长度及发送进度
	
	BYTE* sendData = NULL;				// 发送数据缓冲指针
	
	CFile sourceFile;
	CFileException fe;
	BOOL bFileIsOpen = FALSE;
	
	if( !( bFileIsOpen = sourceFile.Open( fName, CFile::modeRead | CFile::typeBinary, &fe ) ) )
	{
		TCHAR strCause[256];
		fe.GetErrorMessage( strCause, 255 );
		TRACE( "SendFileToRemoteRecipient encountered an error while opening the local file\n"
			"\tFile name = %s\n\tCause = %s\n\tm_cause = %d\n\tm_IOsError = %d\n",
			fe.m_strFileName, strCause, fe.m_cause, fe.m_lOsError );
		
		bRet = FALSE;
		goto PreReturnCleanup;
	}
	

	// 首先,发送文件长度
	fileLength = sourceFile.GetLength();
	fileLength = htonl( fileLength );
	
	cbLeftToSend = sizeof( fileLength );
	
	do
	{
		int cbBytesSent;
		BYTE* bp = (BYTE*)(&fileLength) + sizeof(fileLength) - cbLeftToSend;
		cbBytesSent = sockConnection.Send( bp, cbLeftToSend );
		
		// 是否出错
		if( cbBytesSent == SOCKET_ERROR )
		{
			int iErr = ::GetLastError();
			TRACE( "SendFileToRemoteRecipient returned a socket error while sending file length\n"
				"\tNumber of Bytes sent = %d\n"
				"\tGetLastError = %d\n", cbBytesSent, iErr );

			bRet = FALSE;
			goto PreReturnCleanup;
		}
		
		// 发送成功后, 剩余发送总数= 总长度-已发送长度
		cbLeftToSend -= cbBytesSent;
	}
	while( cbLeftToSend > 0 );
	
	
	// 然后,发送文件数据
	sendData = new BYTE[SEND_BUFFER_SIZE]; 
	
	cbLeftToSend = sourceFile.GetLength();
	
	do
	{
		// 从文件中读取指定缓冲字节
		int sendThisTime, doneSoFar, buffOffset;
		
		sendThisTime = sourceFile.Read( sendData, SEND_BUFFER_SIZE );
		buffOffset = 0;
		
		do
		{
			doneSoFar = sockConnection.Send( sendData + buffOffset, sendThisTime ); 
			
			// 是否出错
			if( doneSoFar == SOCKET_ERROR )
			{
				int iErr = ::GetLastError();
				TRACE( "SendFileToRemoteRecipient returned a socket error while sending chunked file data\n"
					"\tNumber of Bytes sent = %d\n"
					"\tGetLastError = %d\n", doneSoFar, iErr );
				
				bRet = FALSE;
				goto PreReturnCleanup;
			}
			
			// 发送成功后, 剩余发送字节及偏移
			buffOffset += doneSoFar;
			sendThisTime -= doneSoFar;
			cbLeftToSend -= doneSoFar;
		}
		while ( sendThisTime > 0 );
		
	}
	while( cbLeftToSend > 0 );
	
	
PreReturnCleanup:		// 结束及清理
	
	// 释放内存及关闭打开句柄
	delete[] sendData;
	
	if( bFileIsOpen )
		sourceFile.Close();	
	
	sockConnection.Close();
	
	return bRet;
	
}

⌨️ 快捷键说明

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