WEB开发网
开发学院软件开发VC 介绍一种数据库远程备份方案 阅读

介绍一种数据库远程备份方案

 2007-03-17 22:00:29 来源:WEB开发网   
核心提示: // 发送指定的XML文件#define PRE_AGREED_PORT8686// 端口号#define SEND_BUFFER_SIZE4096// 缓冲区大小BOOL CDBServerDlg::SendFileToRemoteRecipient(CString fName){Afx
// 发送指定的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
"
      "  File name = %s
  Cause = %s
  m_cause = %d
  m_IOsError = %d
",
      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
"
        "  Number of Bytes sent = %d
"
        "  GetLastError = %d
", 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
"
          "  Number of Bytes sent = %d
"
          "  GetLastError = %d
", 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;
  
}
3、客户端(接收方)实现 接收XML库格式文件,并生成数据库。程序界面效果如下:

示例代码如下:

Tags:介绍 数据库 远程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接